2

In my twig file i try to override the field. In the {% if %} clause I need to check whether a field has particular value, if yes then assign the value as a class.

{% for item in items %}
  {% if node.language.value == "English"  %}
    {% set example_classes = 'english' %}
   {% else %}
    {% set example_classes = node.language.value %}
  {% endif %}
  <div{{ attributes.addClass('field', field_name, example_classes) }}>
    {{ item.content }}
  </div>
{% endfor %}

the line {% if node.language.value == "English" %} brings nothing, how can I get the value of the node's field_language in twig file?

In the field.html.twig a list of available variables is specified:

Available variables:
* - attributes: HTML attributes for the containing element.
* - label_hidden: Whether to show the field label or not.
* - title_attributes: HTML attributes for the title.
* - label: The label for the field.
* - multiple: TRUE if a field can contain multiple items.
* - items: List of all the field items. Each item contains:
*   - attributes: List of HTML attributes for each item.
*   - content: The field item's content.
* - entity_type: The entity type to which the field belongs.
* - field_name: The name of the field.
* - field_type: The type of the field.
* - label_display: The display settings for the label.
  • Are you really looking for a custom field field_language, or the language of the node itself? - If the latter, {{ node.langcode.value }} could help you further. – Mario Steinitz Sep 26 '18 at 6:44
  • no, I need a custom field. – user89638 Sep 26 '18 at 6:44
  • Then it's usually an array (field item list). For a single value field with index 0. E.g., {{ node.field_language[0].value }} - PS.: {{ dump(node.field_language) }} is a useful tool to explore a field's data structure. – Mario Steinitz Sep 26 '18 at 6:51
  • The twig code seems to be OK, only that field names of custom fields normally start with field_, so {{ node.field_language.value|clean_class }} should display the field value. I've added a filter to get valid class names without the need to run them through if conditions. – 4k4 Sep 26 '18 at 9:02
  • 1
    In a field template the parent node is in a different place {{ element['#object'].field_language.value }}, see drupal.stackexchange.com/questions/260313/… – 4k4 Sep 26 '18 at 9:22
1

I'm a friend of template pre-processing, in order to keep presentation logic separated from (advanced) field processing.

Therefore, I suggest a hook_preprocess_HOOK() implementation in your theme's *.theme file and outline this approach:

/**
 * Implements hook_preprocess_HOOK() for nodes.
 */
mytheme_preprocess_node(array &$variables) {
  $node = $variables['node'];
  $example_class = '';
  if (
    $node->hasField('field_language')
    && !$node->field_language->isEmpty()
  ) {
    $variables['example_class'] = strtolower($node->field_language->value);
  }
  $variables['example_class'] = $example_class;
}

So you could access the newly added template variable {{ example_class }} within your node template:

{% for item in items %}
  {%
    set field_classes = [
      'field',
      field_name,
      example_class,
    ]
  %}
  <div{{ attributes.addClass(field_classes) }}>
    {{ item.content }}
  </div>
{% endfor %}

If you'd like to stick to just altering your template, you can also access all fields of a node entity within its templates by using the {{ node }} variable.

Keep in mind, that unrendered/preprocessed fields of the original entity usually are FieldItemLists. So you'll access an array of field item instances keyed by their delta:

{% if node.field_language and node.field_language[0].value == 'English' %}
...
{% endif %}

The {{ dump() }} Twig function can help you to debug your available values.

  • You can consider class names part of the presentation logic. The core classy theme for example has no preprocess hooks, it has not even a classy.theme file. – 4k4 Sep 26 '18 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy