1

Could anyone please help with the Double field module in Drupal 8 and Twig? I am trying to produce the following with no luck so far as I cannot figure out the proper Twig variables to use.

<li><dl class="classname"><dt>{{ first }}</dt><dd>{{ second }}</dd></dl></li>

I would like to have the raw values in the html above. -- I'm adding some more info here because my goal is to understand the mechanics of twig in this case and how I can get the field values of the two subitems

I have overriden (as suggested by drupal's theme debug system) the field--fieldname.html.twig template in which I have the following

<ul class="list-sm">
{% for item in items %}
<li>
  <dl class="list-terms-inline">
    <dt>
      {{ item.first }}
    </dt>
    <dd>
      {{ item.second }}
    </dd>
  </dl>
</li>
{% endfor %}
</ul>

which is not printing anything in the value areas and apart from that does not iterate through all the items either.

1 Answer 1

0

If you are using default (Unformatted list) formatter override double-field-item.html.twig template with the following markup:

<dt>{{ item.first }}</dt><dd>{{ item.second }}</dd>

Note that Double field ships with HTML list formatter which may render definition list so you might not need to override anything.

enter image description here

5
  • Thanks a lot @ya.teck, but I still cannot quite grasp the solution. I am not very proficient with drupal excuse my possible ignorance, but are you suggesting I override the double-field-item.html.twig in general? Can I do that by just adding a file with the same name in my theme's templates folder? I have actually already used the {{ item.first/second }} markup on the drupal suggested field template iterating through items but I get no output at all.
    – Kostas
    Commented Mar 19, 2018 at 16:55
  • You need to clear caches for the changes to take effect. Also consider using the second approach with HTML list formatter. Seems it covers your case perfectly.
    – ya.teck
    Commented Mar 19, 2018 at 16:59
  • Thanks again for your suggestion @ya.teck I had a look at the definition list provided and in fact it is working but I'm having a little inconsistency with what the actual html of my theme is so I would really like to understand why isn't the code I added to my original question not working, as it is important all customisations are kept within the actual theme's files and not base anything on core or module alterations.
    – Kostas
    Commented Mar 19, 2018 at 18:25
  • The code you used in field--fieldname.html.twig is wrong. You should not use item.first and item.second there.
    – ya.teck
    Commented Mar 20, 2018 at 4:08
  • "a little inconsistency with what the actual html of my theme is" Note the you can override a template (double-field-definition-list.html.twig) for the definition list as well.
    – ya.teck
    Commented Mar 20, 2018 at 4:10

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.