Articles


How to get the value of a node field into a variable in Drupal 8?

How to get the value of a node field into a variable in Drupal 8?



Posted bysivaprasad,21st Nov 2016

To get the value of a node field first of all we need to get the node object.

 

So add the below code to your THEME_preprocess_node() function.

 

if (($node = \Drupal::routeMatch()->getParameter('node')) && $node instanceof \Drupal\node\NodeInterface) {

 

 

}

 

Now we get the node object. Then we can set the field into the variable we want to get the value.

 

if (($node = \Drupal::routeMatch()->getParameter('node')) && $node instanceof \Drupal\node\NodeInterface) {

 

     $variables['my-title'] = $node->field_my_title->value;

 

}

 

Related Articles