3

Hi i am unable to access values from a node object inside a preprocess_views_view . Below is a screenshot of my object and i want to access the value highlighted. How can i do that? enter image description here

  • Yeah...infact i've created a custom view for my search result listing..I need to override a url in the view. That's why i need to access these elements so i can override the url. – EXE Apr 3 '17 at 11:41
1

To get the node id you can use $node->id(). Any object extending ContentEntityBase has this method.

You can look at the Node class to see what public getter methods are available (also, ContentEntityBase and Entity).

Note that protected values cannot be overwritten without a setter method - so you'll need to create a new variable from your preprocess hook to do this.

|improve this answer|||||
1

There are several options:

You can use the getter method for the node id (available for all entities):

$node->id()

You can use magic methods to access any field of any content entity:

$node->nid->value

The above code will actually trigger the below:

$node->get('nid')->get(0)->get('value')->getValue()
|improve this answer|||||
  • $node->id() works in hook_node_presave($node) and $node->nid->value doesn't, thanks. – Achraf JEDAY May 11 '18 at 2:25
1

Debugging a content entity object is not very useful. Instead, debug its array representation ($node->toArray()), you can access that data then like this:

$node->get('field')->property. For example $node->get('nid')->value.

For base fields like the ID, there are usually also methods, in this case $node->id() as mentioned by others.

See Entity API Cheat Sheet for more information.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.