4

I've tried several different ways to try access the value of a field item list value without success.

The item in question within Drupal 8 is a float text field and I need the unmodified number from this. The value gained from the standard content.field_latitude[0]['#markup'] call is rounded in some manner so I want to grab the one located in the #items object as shown in the screen shot below.

I can get this via a preprocessor in the theme file but want to avoid using a preprocess if I can and want to gain access directly in the twig file.

enter image description here

1

2 Answers 2

5

To get the value you've highlighted in the debug output in Twig:

{{ content.field_latitude['#items'].0.value }}

You can see this follows the path you've shown in the screenshot, but missing the protected values array. Instead it uses .value on the field item, which calls a magic method __get('value') to retrieve the protected property.

But as @MarioSteinitz commented it's better to use the node object:

{{ node.field_latitude.value }}

Because content is not an API like the methods of the node object. It is a render array which can change when you configure the field display differently or install a layout module.

And never use the |raw filter on user input!

0

Use the raw filter:

{{ content.field_latitude[0]|raw }}

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.