3

Is there any programming method to hide or show each Field's Label of custom content type in twig template files, regardless of fields manage display setting?

2 Answers 2

5

Heres a little twig filter module that does just that: https://www.drupal.org/project/twig_field_value. If you study the code you can see how the filter works and deduce how it works programatically.

6

Override the template for that specific field and don't print the label.

field--field-thing.html.twig

{% if multiple %}
  <div{{ attributes }}>
    {% for item in items %}
      <div{{ item.attributes }}>{{ item.content }}</div>
    {% endfor %}
  </div>
{% else %}
  {% for item in items %}
    <div{{ attributes }}>{{ item.content }}</div>
  {% endfor %}
{% endif %}

Alternatively, you can preprocess the field and remove the label/change the display, but personally I would do it in the templates so later on the label could easily be added back in.

2
  • I think it is best to give a code example...
    – user16289
    Aug 17, 2016 at 20:25
  • code example has been added Sep 14, 2021 at 17:46

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.