3

Sometimes it is necessary to get the actual value of a field (not the formatted/rendered one), e.g. to check a boolean for true or false.

If I want to get a field value of a node or paragraph inside twig templates (node.html.twig / paragraph.html.twig) I can use node.field_my_boolean.value respectively paragraph.field_my_boolean.value.

How do I accomplish this in a media twig template? I've already tried media.field_my_boolean.value but it does not work.

5
  • 1
    try {{ content.field_my_boolean }} and you want the raw value without the HTML markup {{ content.field_my_boolean.0 }}
    – No Sssweat
    Apr 20, 2017 at 10:26
  • Both output the rendered value and not the actuall true/false. I can not use this because it is translated (e.g. "wahr"/"falsch" in German). Apr 20, 2017 at 10:37
  • When you do {{ content.field_my_boolean.0 }} is it showing wahr/flasch or 1/0 ?
    – No Sssweat
    Apr 20, 2017 at 10:48
  • It depends on the display format setting. If I set the formatter to 1/0 I get 1 or 0. If I set it to ``wahr/falsch` I get wahr or falsch. That's what I mean by "rendered value". Apr 20, 2017 at 11:08
  • I have adjusted my answer.
    – No Sssweat
    Apr 21, 2017 at 6:06

2 Answers 2

4

Right now, the Media module does not pass its object in the variables like Paragraphs and Node does. This is why media.field_my_boolean.value does nothing.

You can correct this in your theme by implementing theme_preprocess_media, which adds the media object to the variables:

/**
 * Prepares variables for media templates.
 *
 * Default template: media.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - media: An individual media for display.
 */
function mytheme_preprocess_media(&$variables) {
  $variables['media'] = $variables['elements']['#media'];
}

You can then access media. in your twig template and any connected fields on it just like Paragraph or Node.

3
  • You seem to be right. Actually function mytheme_preprocess_media(&$variables) {$variables['media'] = $variables['elements']['#media']} should be enough because your themes preprocess function is just adding variables. No need to copy the whole function from media module. Apr 20, 2017 at 12:16
  • Gotcha - for some reason I had to use the whole function, could be an earlier build.
    – Kevin
    Apr 20, 2017 at 13:01
  • OK, I'm referring to Drupal 8.3.0 and Media Entity 8.x-1.6. Just update your answer and I will accept it. Apr 20, 2017 at 13:39
1

You can with {{ content.field_my_boolean }} and if you want the raw value without the HTML markup use content.field_my_boolean.0

Both output the rendered value and not the actuall true/false. I can not use this because it is translated (e.g. "wahr"/"falsch" in German).

Then use the |t function and do this

{% if content.field_my_boolean.0 == 'Wahr'|t %}
    // do something
{% elseif content.field_my_boolean.0 == 'Falsch'|t %}
    // do something
{% endif %}

Your Answer

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

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