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?

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.

5

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.

  • I think it is best to give a code example... – JohnDoea Aug 17 '16 at 20:25

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.