【问题标题】:Unique id to recurring field in twig - Drupal 8树枝中重复字段的唯一ID - Drupal 8
【发布时间】:2021-04-23 07:27:54
【问题描述】:

我有一个 Drupal 内容类型,它使用多个相同类型的引用实体(例如,“相关产品”)

我想为每个相关产品的标题字段指定一个唯一 ID。它可以是产品名称、迭代器、产品节点的 ID,以最简单的为准。

我创建了一个树枝模板,它覆盖了所述内容类型中所述节点的标题:field--node--title--my-content-type.html.twig,但我不知道从这里去哪里。

我可以添加自定义 id

{% set attributes = attributes.setAttribute('id', 'customID') %}

但这是静态的,并且在多次调用该字段时不会是唯一的。

{% set node = element['#object'] %}{{ item.content['#node'].field_name.value }} 推荐的 here 不适合我。

如果可能的话,我想只用树枝来解决它,而不需要任何额外的 php 代码。

非常感谢任何指针或建议

【问题讨论】:

    标签: drupal twig drupal-8 drupal-templates


    【解决方案1】:

    Kien Nguyen 的回答并不完全适合我,因为虽然有多个引用实体,但每个实体只有一个标题,所以索引总是以“0”结尾,这并不完全是我可以用作唯一 ID 的值。

    但根据他的逻辑,现在很容易找到解决方案:

    我没有将索引用作索引,而是将其用作访问item.content 中的值的键。 (因为我要使用 item.content 作为唯一 ID,所以我必须去掉空格,但为了做到这一点,我必须直接访问 item.content 数组中的值。

    在我的场景中,该项目是“相关产品”类型节点的标题,所以它是

    • 足够短,可以用作 HTML id
    • 独特(即没有两个不同的产品同名)

    所以

    {% for index, item in items %}                                     {# use index #}
            <span>{{ index }}</span>                                         {# and print it #}
            <div{{ item.attributes }}>{{ item.content }}</div>
    

    Kien 代码的一部分变成了

    {% for key, item in items %}
          <span id="{{element['#items'][key].value|replace({' ':''}) }}"></span>
          <div{{ item.attributes }}>{{ item.content }}</div>
        
    

    在我的实现中,它会按照最初的预期为每个引用的节点提供一个唯一的 ID。

    感谢您的帮助,基恩!

    【讨论】:

      【解决方案2】:

      您可以修改循环通过items 数组的代码。

      比如我加了一个迭代索引:

      field--node--title--my-content-type.html.twig

      {# Here I coppied template from web/core/modules/system/templates/field.html.twig and modified it #}
      {%
        set title_classes = [
        label_display == 'visually_hidden' ? 'visually-hidden',
      ]
      %}
      
      {% if label_hidden %}
        {% 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 %}
      {% else %}
        <div{{ attributes }}>
          <div{{ title_attributes.addClass(title_classes) }}>{{ label }}</div>
          {% if multiple %}
          <div>
            {% endif %}
            {% for index, item in items %}                                     {# use index #}
              <span>{{ index }}</span>                                         {# and print it #}
              <div{{ item.attributes }}>{{ item.content }}</div>
            {% endfor %}
            {% if multiple %}
          </div>
          {% endif %}
        </div>
      {% endif %}
      
      

      结果

      【讨论】:

      • 谢谢,你是英雄。虽然您的解决方案对我来说不是开箱即用的,但它确实让我朝着正确的方向前进,并帮助开发了完全符合我要求的解决方案(有关详细信息,请参阅我的答案)。因此,我会将您的答案标记为已接受,因为我的解决方案基于您的逻辑。再次感谢
      • @ZsoltBalla 因为你写了“它可以是一个迭代器”,所以也许我误解了你想要的。但无论如何,我很高兴我对你有所帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      • 2016-06-02
      • 1970-01-01
      • 2018-07-03
      相关资源
      最近更新 更多