I want to get a field value from a node that I can use in the html--front.html.twig template file.
I can create a new variable, for example with $variables['custom_twig_variable'] = 'Some important value';.

Then in the template file, I would simply get that value with {{ custom_twig_variable }}.

I don't know how to get the field value from $variables, the argument passed to the theme hook, whose content is the following one.

Array (
  [user] => Drupal\Core\Session\AccountProxy Object (
    [requestStack:protected] => Symfony\Component\HttpFoundation\RequestStack Object (
      [requests:Symfony\Component\HttpFoundation\RequestStack:private] => Array (
        [0] => Symfony\Component\HttpFoundation\Request Object (
          [attributes] => Symfony\Component\HttpFoundation\ParameterBag Object (
            [parameters:protected] => Array (
              [_system_path] => node/4
              [_controller] => \Drupal\node\Controller\NodeViewController::view
              [_title_callback] => \Drupal\node\Controller\NodeViewController::title
              [node] => Drupal\node\Entity\Node Object (
                [values:protected] => Array (
                  [vid] => Array (
                      [x-default] => 4
                  [field_background_color] => Array (
                    [x-default] => Array (
                      [0] => Array (
                        [value] => ff00ff

How do I get the value of field_background_color?

3 Answers 3


So your field is on the node?

Then you need to get the node object first.

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


Once you have that, you can read your field, assuming it is a text field, like this: $node->field_background_color->value. Make sure to always check if you really have a $node object as done in the snippet above when you're in the global context. You will only have on on node/N, but not on other pages.

There are many questions already about how to access field values, and there are great presentations as well.

  • True, that is helpful but you then still have to know how to access the value. I've already seen too much code that is then working with the array structure returned from that, when working with the node object is much easier.
    – Berdir
    Mar 12, 2015 at 8:19

Simply iterate the $variables['view']->result array and get every field value you want.

foreach ($variables['view']->result as $key=>$result){
    $field = $result->_entity->get('field_name')->getValue();
    $variables['rows'][$key]['var_name'] = $field[0]['value']; 

In this line $variables['rows'][$key]['var_name'] = $field[0]['value']; you save the data into the row-variable where you can access it later in your twig-template with {{row.var_name}}


You have entity values at hand

$element = $variables['element'];
if ( $entity = $element['#object'] ) {
  dump ($entity); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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