Problem/Motivation

When updating a MenuLinkContent item with new definition values programmatically, there is a possibility that the entity property value may not already exist, even when present in the overrideAllowed list.

This will generate the PHP warning "Creating default object from empty value" when warning or strict modes are set.

The specific code is line 215 of MenuLinkContent.php:

213      $entity = $this->getEntity();
214      foreach ($overrides as $key => $value) {
215        $entity->{$key}->value = $value;
216      }

Proposed resolution

We have a few options. The simplest and easiest patch:

foreach ($overrides as $key => $value) {
  if (!isset(entity->{$key}) {
    $entity->{$key} = new stdClass();
  }
  $entity->{$key}->value = $value;
}

Creates an empty object as a property if the property is not already present.

Other solutions:

  1. Modify the MenuLinkContent getEntity() function to always populate like other entity loads
  2. Modify the MenuLinkContent updateLink() function to work more like other updateLink functions

These solutions will require a decent rewrite of their respective functions.

Remaining tasks

Any decision making for the other solutions.
A patch is incoming shortly.

User interface changes

None.

API changes

Removing a PHP Warning from \Drupal\menu_link_content\Plugin\Menu\MenuLinkContent::updateLink()

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

cyb.tachyon created an issue. See original summary.

cyb_tachyon’s picture

FileSize
745 bytes

Patch for Proposed Resolution.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Elijah Lynn’s picture

Thanks for opening this issue. Getting this quite a bit on a site. Confirmed #2 works to rid the error.

Elijah Lynn’s picture

Status: Active » Reviewed & tested by the community

Not sure if we still need 2 reviews to mark RTBC, but let's try it out!

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/menu_link_content/src/Plugin/Menu/MenuLinkContent.php
@@ -212,6 +212,9 @@ public function updateLink(array $new_definition_values, $persist) {
       foreach ($overrides as $key => $value) {
+        if (!isset($entity->{$key})) {
+          $entity->{$key} = new \stdClass();
+        }
         $entity->{$key}->value = $value;

So the entity has a magic getter \Drupal\Core\Entity\ContentEntityBase::__get(). If this code returns something which can't have ->value then that implies there is no corresponding field. So you're overriding something that doesn't exist - at all.

I think the correct thing to do here is to raise an a more helpful error telling you what key doesn't exist.

dhirendra.mishra’s picture

Status: Needs work » Needs review
FileSize
1.03 KB
807 bytes

Re-rolled patch again 8.9.x as earlier patch was not getting applied.

alexpott’s picture

Status: Needs review » Needs work

We still need to address #12 and work out what is the correct fix here.

Elijah Lynn’s picture

Posting full error message for the search engines:

Warning: Creating default object from empty value in Drupal\menu_link_content\Plugin\Menu\MenuLinkContent->updateLink() (line 237 of /var/www/cms/docroot/core/modules/menu_link_content/src/Plugin/Menu/MenuLinkContent.php)

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

beunerd’s picture

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.