简体   繁体   English

Drupal 8 Double Field HTML

[英]Drupal 8 Double Field HTML

I'm using Drupal 8, and the Double Field module. 我正在使用Drupal 8和Double Field模块。 I want for the second field to allow HTML tags, but currently the tags are directly output on the page as text. 我想让第二个字段允许HTML标签,但是当前标签直接以文本形式输出在页面上。 There is no field format that would not escape HTML tags. 没有没有不能逃脱HTML标记的字段格式。

In the issues for this module, the author suggests "overriding theme_double_field()" , but I don't know how to go about that. 在该模块的问题中, 作者建议使用“ overrideing theme_double_field()” ,但我不知道该如何处理。

How can I enable HTML tags for the second field of a double field? 如何为double字段的第二个字段启用HTML标签?

By default, all strings in twig templates are escaped. 默认情况下,树枝模板中的所有字符串均被转义。 That's why you lose your formatting. 这就是为什么您丢失格式的原因。 You can avoid this in 2 ways: 您可以通过两种方式避免这种情况:

  1. Use FormattableMarkup in preprocess function to pass formattable string variable to your twig template. 在预处理功能中使用FormattableMarkupFormattableMarkup字符串变量传递到您的树枝模板。
  2. Use raw filter in twig template. 在树枝模板中使用raw过滤器。

Double Field provides several formatters, but suppose you use the simplest Unformatted list formatter, which uses double_field_item theme to render double field item. Double Field提供了几种格式化程序,但假设您使用最简单的Unformatted list格式化程序,该格式器使用double_field_item主题呈现双字段项目。

Then the first solution is to write your own preprocess function for double_field_item theme: 然后第一个解决方案是为double_field_item主题编写自己的预处理函数:

use Drupal\Component\Render\FormattableMarkup;

function mytheme_preprocess_double_field_item(&$variables) {
  $item = $variables['elements']['#item'];
  $second_value = new FormattableMarkup($item->second, []);
  $variables['item']['second'] = $second_value;
}

And the alternative second solution is to override double-field-item.html.twig template in your theme: 第二种替代解决方案是在主题中覆盖double-field-item.html.twig模板:

{% for subfield, subitem in item %}
    {% if subitem %}
        <div class="double-field-{{ subfield }}">
            {% if settings[subfield].prefix %}
                <span class="double-field-prefix">{{ settings[subfield].prefix }}</span>
            {% endif %}
            {{ subitem|raw }}
            {% if settings[subfield].suffix %}
                <span class="double-field-suffix">{{ settings[subfield].suffix }}</span>
            {% endif %}
        </div>
    {% endif %}
{% endfor %}

Choose any :) 选择任何:)

It appears there is support in the works , but that the patch isn't ready for inclusion in the module yet. 似乎在工作中有支持 ,但是该补丁尚未准备好包含在模块中。 Your best option is likely to help finish work on the patch. 您最好的选择可能是帮助完成补丁程序的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM