Drupal 9 – update to PHP 8 module no longer works

I have updated my Drupal from 8 to 9 and used PHP 7.4. Now I have chosen the time to update to PHP 8. Everything runs so far, only with a self-built module I get now an Error message:

The website encountered an unexpected error. Please try again later. Error: Attempt to assign property "format" on null in copy_fields_from_model_contract_node_edit_node_prepare_form() (line 57 of modules/custom/copy_fields_from_model_contract_node_edit/copy_fields_from_model_contract_node_edit.module). 

This concerns exactly this line:

$node->body->format = 'full_html'; 

The code of my module is not elegant, but it did its job as desired. Basically, I copy from another node the content of the body field into a body field of another node. The code of the whole module looks like this:

<?php  use DrupalnodeNodeInterface; use DrupalCoreFormFormStateInterface;   /** * Implements hook_ENTITY_TYPE_prepare_form() for node entities. */ function copy_fields_from_model_contract_node_edit_node_prepare_form(NodeInterface $node, $operation, FormStateInterface $form_state) {  //TODO Build NID as array and go through with loop    $arr_vertraege = array(6766, 6769, 6768, 6775, 6767, 6776, 6777, 6778, 6779, 6780, 6781, 6782);    foreach ($arr_vertraege as $mustervertraege) {      $nid = $mustervertraege;     $node_storage = Drupal::entityTypeManager()->getStorage('node');     $node = $node_storage->load($nid);      if ($nid == $mustervertraege) {         $getcontract = $node->field_vertragsinhalt->value;     }    //TODO Works without ($operation == 'default')    if ($operation == 'edit') {     /*dpm("Modul an edit");*/     //TODO Shall always pull the current node and nid. But does not work yet with node creation. Error: Call to a member function id() on null in      $node = Drupal::routeMatch()->getParameter('node');     $nid = $node->id();     $node_storage = Drupal::entityTypeManager()->getStorage('node');     $node = $node_storage->load($nid);      if ($node->getType() == 'buch_anlegen') {       if ($node->field_vertrag_updaten->value == '1') {         //  dpm($node->field_vertrag_updaten->value);         //var_dump($node);          if ($mustervertraege == $arr_vertraege[0]) {           $node->field_vertragsinhalt->value = $getcontract;           $node->body->format = 'full_html';           // don't save the node yet, it will be saved after submitting         }       }       if ($node->field_vertrag_updaten_2->value == '1') {         //  dpm($node->field_vertrag_updaten->value);         //var_dump($node);          if ($mustervertraege == $arr_vertraege[1]) {           $node->field_vertragsinhalt_2->value = $getcontract;           $node->body->format = 'full_html';           // don't save the node yet, it will be saved after submitting         }       }       if ($node->field_vertrag_updaten_3->value == '1') {         //  dpm($node->field_vertrag_updaten->value);         //var_dump($node);          if ($mustervertraege == $arr_vertraege[2]) {           $node->field_vertragsinhalt_3->value = $getcontract;           $node->body->format = 'full_html';           // don't save the node yet, it will be saved after submitting         }       }       if ($node->field_vertrag_updaten_4->value == '1') {         //  dpm($node->field_vertrag_updaten->value);         //var_dump($node);          if ($mustervertraege == $arr_vertraege[3]) {           $node->field_vertragsinhalt_4->value = $getcontract;           $node->body->format = 'full_html';           // don't save the node yet, it will be saved after submitting         }       }       if ($node->field_vertrag_updaten_5->value == '1') {         //  dpm($node->field_vertrag_updaten->value);         //var_dump($node);          if ($mustervertraege == $arr_vertraege[4]) {           $node->field_vertragsinhalt_5->value = $getcontract;           $node->body->format = 'full_html';           // don't save the node yet, it will be saved after submitting         }       }       if ($node->field_vertrag_updaten_6->value == '1') {         //  dpm($node->field_vertrag_updaten->value);         //var_dump($node);          if ($mustervertraege == $arr_vertraege[5]) {           $node->field_vertragsinhalt_6->value = $getcontract;           $node->body->format = 'full_html';           // don't save the node yet, it will be saved after submitting         }       }       if ($node->field_vertrag_updaten_7->value == '1') {         //  dpm($node->field_vertrag_updaten->value);         //var_dump($node);          if ($mustervertraege == $arr_vertraege[6]) {           $node->field_vertragsinhalt_7->value = $getcontract;           $node->body->format = 'full_html';           // don't save the node yet, it will be saved after submitting         }       }       if ($node->field_vertrag_updaten_8->value == '1') {         //  dpm($node->field_vertrag_updaten->value);         //var_dump($node);          if ($mustervertraege == $arr_vertraege[7]) {           $node->field_vertragsinhalt_8->value = $getcontract;           $node->body->format = 'full_html';           // don't save the node yet, it will be saved after submitting         }       }       if ($node->field_vertrag_updaten_9->value == '1') {         //  dpm($node->field_vertrag_updaten->value);         //var_dump($node);          if ($mustervertraege == $arr_vertraege[8]) {           $node->field_vertragsinhalt_9->value = $getcontract;           $node->body->format = 'full_html';           // don't save the node yet, it will be saved after submitting         }       }       if ($node->field_vertrag_updaten_10->value == '1') {         //  dpm($node->field_vertrag_updaten->value);         //var_dump($node);          if ($mustervertraege == $arr_vertraege[9]) {           $node->field_vertragsinhalt_10->value = $getcontract;           $node->body->format = 'full_html';           // don't save the node yet, it will be saved after submitting         }       }       if ($node->field_vertrag_updaten_11->value == '1') {         //  dpm($node->field_vertrag_updaten->value);         //var_dump($node);          if ($mustervertraege == $arr_vertraege[10]) {           $node->field_vertragsinhalt_11->value = $getcontract;           $node->body->format = 'full_html';           // don't save the node yet, it will be saved after submitting         }       }       if ($node->field_vertrag_updaten_12->value == '1') {         //  dpm($node->field_vertrag_updaten->value);         //var_dump($node);          if ($mustervertraege == $arr_vertraege[11]) {           $node->field_vertragsinhalt_12->value = $getcontract;           $node->body->format = 'full_html';           // don't save the node yet, it will be saved after submitting         }       }     }   }   } } 

Does anyone have a tip for me? I would be very grateful.

Greetz Bavra

P.S.: Complete error message from the logs:

Message    Error: Attempt to assign property "format" on null in copy_fields_from_model_contract_node_edit_node_prepare_form() (line 57 of /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/modules/custom/copy_fields_from_model_contract_node_edit/copy_fields_from_model_contract_node_edit.module) #0 [internal function]: copy_fields_from_model_contract_node_edit_node_prepare_form(Object(DrupalnodeEntityNode), 'edit', Object(DrupalCoreFormFormState)) #1 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/core/lib/Drupal/Core/Entity/EntityForm.php(397): call_user_func_array('copy_fields_fro...', Array) #2 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/core/lib/Drupal/Core/Entity/EntityForm.php(129): DrupalCoreEntityEntityForm->prepareInvokeAll('node_prepare_fo...', Object(DrupalCoreFormFormState)) #3 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/core/lib/Drupal/Core/Entity/ContentEntityForm.php(287): DrupalCoreEntityEntityForm->init(Object(DrupalCoreFormFormState)) #4 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/core/lib/Drupal/Core/Entity/EntityForm.php(96): DrupalCoreEntityContentEntityForm->init(Object(DrupalCoreFormFormState)) #5 [internal function]: DrupalCoreEntityEntityForm->buildForm(Array, Object(DrupalCoreFormFormState)) #6 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/core/lib/Drupal/Core/Form/FormBuilder.php(531): call_user_func_array(Array, Array) #7 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/core/lib/Drupal/Core/Form/FormBuilder.php(278): DrupalCoreFormFormBuilder->retrieveForm('node_buch_anleg...', Object(DrupalCoreFormFormState)) #8 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/core/lib/Drupal/Core/Controller/FormController.php(73): DrupalCoreFormFormBuilder->buildForm(Object(DrupalnodeNodeForm), Object(DrupalCoreFormFormState)) #9 [internal function]: DrupalCoreControllerFormController->getContentResult(Object(SymfonyComponentHttpFoundationRequest), Object(DrupalCoreRoutingRouteMatch)) #10 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array) #11 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/core/lib/Drupal/Core/Render/Renderer.php(564): DrupalCoreEventSubscriberEarlyRenderingControllerWrapperSubscriber->DrupalCoreEventSubscriber{closure}() #12 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): DrupalCoreRenderRenderer->executeInRenderContext(Object(DrupalCoreRenderRenderContext), Object(Closure)) #13 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): DrupalCoreEventSubscriberEarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) #14 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/vendor/symfony/http-kernel/HttpKernel.php(158): DrupalCoreEventSubscriberEarlyRenderingControllerWrapperSubscriber->DrupalCoreEventSubscriber{closure}() #15 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/vendor/symfony/http-kernel/HttpKernel.php(80): SymfonyComponentHttpKernelHttpKernel->handleRaw(Object(SymfonyComponentHttpFoundationRequest), 1) #16 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/modules/contrib/redirect_after_login/src/RedirectMiddleware.php(46): SymfonyComponentHttpKernelHttpKernel->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true) #17 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Drupalredirect_after_loginRedirectMiddleware->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true) #18 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): DrupalCoreStackMiddlewareSession->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true) #19 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): DrupalCoreStackMiddlewareKernelPreHandle->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true) #20 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupalpage_cacheStackMiddlewarePageCache->pass(Object(SymfonyComponentHttpFoundationRequest), 1, true) #21 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/core/modules/ban/src/BanMiddleware.php(50): Drupalpage_cacheStackMiddlewarePageCache->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true) #22 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): DrupalbanBanMiddleware->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true) #23 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): DrupalCoreStackMiddlewareReverseProxyMiddleware->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true) #24 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): DrupalCoreStackMiddlewareNegotiationMiddleware->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true) #25 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/core/lib/Drupal/Core/DrupalKernel.php(708): StackStackedHttpKernel->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true) #26 /home/bavramor/data/Webdesign/Webseiten/landwehrcie-backup-drupal9/web/index.php(19): DrupalCoreDrupalKernel->handle(Object(SymfonyComponentHttpFoundationRequest)) #27 {main} 
This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Drupal 9 – update to PHP 8 module no longer works

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.