Score:-1

How do I access the protected value of node entity?

in flag

I have a view node and I use the below code to Kint out the node objects:

$node = \Drupal::entityTypeManager()
      ->getStorage('node')
      ->loadMultiple();
 dpm($node);

returns the below output:

enter image description here

I get to see the properties of each node. In this, I want to access the target_id/value of the highlighted field - field_cp_level2_ref_level3. I tried the below line and it is coming out as null:

$node->field_cp_level2_ref_level3->target_id

How to access this field value?

miststudent2011 avatar
fr flag
Does this answer your question? [8 - Node::load is generating an object with circular reference which is crashing kint() and var\_export()](https://drupal.stackexchange.com/questions/200046/8-nodeload-is-generating-an-object-with-circular-reference-which-is-crashing)
miststudent2011 avatar
fr flag
When you want to dump it, just use $node->toArray() , Refer https://drupal.stackexchange.com/a/200118/71454
in flag
@misstudent2011 I'm not mentioning that my page is crashing due to circular reference generated. I'm just asking a way to access the protected values. The question you have referenced does not help me.
miststudent2011 avatar
fr flag
have you gone through the answer in the link I shared ? It provides you the way to get the protected values. Seems you are having difficulty to get it let me paste it as comment.
miststudent2011 avatar
fr flag
When you want to dump it, just use `$node->toArray()`. Then you get an array with the plain values. You can use those field names and properties again to access the values on the node object, e.g. `$node->yourfield->value.`
in flag
Thanks @miststudent2011 but doing a $node->toArray returns the below error: "Error: Call to a member function toArray() on array" . also doing $node->yourfield->value returns null :(
Score:0
bd flag

Your problem is that you wrongly assume the result of loadMultiple to be a node object, when instead it returns an array of node objects.

You should be able to do this:

$nodes = \Drupal::entityTypeManager()
  ->getStorage('node')
  ->loadMultiple();
$node = reset($nodes);

You should now be able to access the values you see in your debug output:

$node->field_cp_level2_ref_level3->target_id;

It's actually a bit questionable to load all the nodes you have in your system. That might still work with your 294 nodes, but it will create problems on sites with more content. Instead you should add conditions, e.g. by using loadByProperties on the node storage.

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.