3

Basically I have this if else condition that checks the entity type of a node and outputs a value based on the condition. As seen in the code below if the entity type is press_release it outputs Press Release on the markup and if the entity type is interview, it outputs Interview.

The problem with this is that once there are multiple conditions it becomes a hassle to maintain is there a way to shorten it?

{% for newsdesk in accordion.entity.field_newsdesk_and_press %}

    {% if article.entity.getType == 'press_release' %}

         <a href="#" class="tag-label">Press Release</a>

    {% endif %}


   {% if article.entity.getType == 'interview' %}

         <a href="#" class="tag-label">interview</a>

    {% endif %}

{% endfor %}
clestcruz
  • 1,081
  • 3
  • 31
  • 75

2 Answers2

2

Thing is, page.html.twig probably is the second-worst template to do this kind of stuff (headed by html.html.twig and followed by node.html.twig). Instead you'd better do this on the field-level.

Template Suggestions

But first of all, it's really important that you enable Twig debugging. Simply follow: Debugging Twig templates. From now on you will get template suggestions printed as HTML comments into your markup. See the sample output for my "Article" content type's body field:

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'field' -->
<!-- FILE NAME SUGGESTIONS:
   * field--node--body--article.html.twig
   * field--node--body.html.twig
   * field--node--article.html.twig
   * field--body.html.twig
   x field--text-with-summary.html.twig
   * field.html.twig
-->
<!-- BEGIN OUTPUT from 'core/themes/classy/templates/field/field--text-with-summary.html.twig' -->

<div property="schema:text" data-quickedit-field-id="node/1/body/en/teaser" class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item quickedit-field"><p>Elitphas sellus laoreet consequa laoreetc maecenas aesent iam ras fames. Volutpat sacras feugiatm hendre lacusp esent quamsusp uada netus. Duinulla sque teger justop justov metiam. Uisque vestibu tcras pretium llam lum llam. Consec ctetur llaut necinte magnap vel proin. Justo teger sedinteg vulput ligula arcu litora telluss eger. Roin massan quamnull nibh vitaenu sduis felis rproin ndisse. Ipsumves nullap teger mussed condim sacras.</p></div>

<!-- END OUTPUT from 'core/themes/classy/templates/field/field--text-with-summary.html.twig' -->

The little x in front of the listed templates indicates which templates is currently in use. And just on top of the actual field output the relative path on your hard drive to the current template file is printed. Now you could go, copy this template, paste it into your theme's templates, and rename it along one of the commented suggestions. Probably as distinct as possible.

So in your case that would be:

  • field--node--field-newsdesk-and-press--press-release.html.twig
  • field--node--field-newsdesk-and-press--interview.html.twig

And inside there you edit the markup to fit your needs. Via hook_theme_suggestions_HOOK_alter the existing template suggestions also could easily be extended to respect the view mode as well.


Still, this isn't the ideal solution in my own opinion (though it's widely done that way). Managing a lot of templates can become fiddly really fast. Plus: the actual Field UI in the back-end becomes more and more useless.

I'd recommend to programmatically create two pseudo fields instead which simply can be placed in each node type's Manage Display settings. Or to do this entirely inside preprocess functions, again programmatically. But this would go far beyond the scope of your question and should better be asked separately if you don't get it done yourself.

leymannx
  • 5,138
  • 5
  • 45
  • 48
2

In view of your example, I can propose this:

{% for newsdesk in accordion.entity.field_newsdesk_and_press %}
    <a href="#" class="tag-label">{{ article.entity.getType|humanize|title }}</a>
{% endfor %}
doydoy44
  • 5,720
  • 4
  • 29
  • 45
  • Sorry how does `humanize` work? Drupal basically flag is an unknown filter – clestcruz Jul 11 '18 at 03:50
  • @clestcruz: humanize is a [symfony function](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/FormRenderer.php#L287) If Dupral does not provide this function, you can create your own filter ( [for example](https://web4pro.net/blog-news/custom-twig-filter/) ) – doydoy44 Jul 11 '18 at 07:26