Score:0

Accessing menu protected data

pt flag

How can the protected data of a menu object, pictured below, be accessed ?

enter image description here

using $menu->id() works and returns the menu ID. But neighter $menu->values nor $menu->get('values') work.

4uk4 avatar
cn flag
To get all values use `->toArray()`. See the same question for nodes https://drupal.stackexchange.com/questions/233098/how-to-access-values-in-node-object
Score:0
cn flag

It's a content entity so you access fields using magic methods, e.g.

$x = $menu->field_foo->value;
pt flag
I tried `$menu->values->value` but that returned null. `$menu->values` is actually an array in the menu object. And I though magic methods are called using `$menu->get('values')` ?
cn flag
`$values` actually holds the values for the entity properties (not class properties, possibly a bit confusing). When you use `$entity->foo`, you're requesting `$values['foo']`, via the magic method (https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21ContentEntityBase.php/function/ContentEntityBase%3A%3A__get/8.2.x). You don't need to try to access `$values` directly itself, indeed you can't as there's no public method available, but you can use the magic get to request anything inside it. Hopefully that clears it up but if not, just edit the post and mention what you're...
cn flag
...trying to get at, and it should be easy enough to point you in the right direction
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.