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:
- Modify the MenuLinkContent getEntity() function to always populate like other entity loads
- 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()
Comment | File | Size | Author |
---|---|---|---|
#19 | updateLink-2775665-14.patch | 807 bytes | beunerd |
#13 | updateLink-2775665-13.patch | 807 bytes | dhirendra.mishra |
#13 | interdiff_2-13.txt | 1.03 KB | dhirendra.mishra |
Comments
Comment #2
cyb_tachyon CreditAttribution: cyb_tachyon as a volunteer commentedPatch for Proposed Resolution.
Comment #10
Elijah LynnThanks for opening this issue. Getting this quite a bit on a site. Confirmed #2 works to rid the error.
Comment #11
Elijah LynnNot sure if we still need 2 reviews to mark RTBC, but let's try it out!
Comment #12
alexpottSo 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.
Comment #13
dhirendra.mishra CreditAttribution: dhirendra.mishra at Valuebound for Valuebound commentedRe-rolled patch again 8.9.x as earlier patch was not getting applied.
Comment #14
alexpottWe still need to address #12 and work out what is the correct fix here.
Comment #15
Elijah LynnPosting full error message for the search engines:
Comment #19
beunerd CreditAttribution: beunerd commentedRe-rolled for Drupal 9.3.x