4

I've been wrestling with Drupal 8 for some time now and have run into a little problem.

I want to get a field value from a content type that I can use in my html--front.html.twig. I can create a new variable that I can use in the twig template i.e

$variables['custom_twig_variable'] = 'Some important value';

Then in my twig template I would simply call this like this

{{ custom_twig_variable }}

The thing here is that I don't now how to "reach" my field value in the array $variables that is passed into the preprocess_html function.

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 read the value of field_background_color?

5

So your field is on the node?

Then you need to get the node object first.

<?php
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.

  • Code snippet is missing a "&&". Would have fixed but I couldn't find 6 wrong characters :) – benjy Mar 12 '15 at 0:44
  • I am not limited by that, fixed it. Also never forget the big entity debugging trick $node->toArray() – user49 Mar 12 '15 at 7:35
  • 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 '15 at 8:19
1

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}}

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.