首页 > 解决方案 > 允许原始过滤器在树枝的块视图中处理标签变量

问题描述

我想要实现的是在我的标签元素上设置原始过滤器,这样如果用户决定在 CMS 中放置 HTML 标签,它就可以工作。当我尝试这样做时,它似乎没有做任何事情(它仍然刷新 HTML 标记)。我想知道标签变量是否由 Drupal 在 xxx.theme 中预处理?无论如何,如果你们对我如何允许这些
标签有任何想法,那将是最好的。

到目前为止,我尝试的是添加raw twig 标签。

{#
/**
 * @file
 * Default theme implementation to display a block.
 *
 * Available variables:
 * - plugin_id: The ID of the block implementation.
 * - label: The configured label of the block if visible.
 * - configuration: A list of the block's configuration values.
 *   - label: The configured label for the block.
 *   - label_display: The display settings for the label.
 *   - provider: The module or other provider that provided this block plugin.
 *   - Block plugin specific settings will also be stored here.
 * - content: The content of this block.
 * - attributes: array of HTML attributes populated by modules, intended to
 *   be added to the main container tag of this template.
 *   - id: A valid HTML ID and guaranteed unique.
 * - title_attributes: Same as attributes, except applied to the main title
 *   tag that appears in the template.
 * - title_prefix: Additional output populated by modules, intended to be
 *   displayed in front of the main title tag that appears in the template.
 * - title_suffix: Additional output populated by modules, intended to be
 *   displayed after the main title tag that appears in the template.
 *
 * @see template_preprocess_block()
 *
 * @ingroup themeable
 */
#}
<div{{ attributes }}>
  {{ title_prefix }}
  {% if label %}
    <h2{{ title_attributes }}>{{ label | raw }}</h2>
  {% endif %}
  {{ title_suffix }}
  {% block content %}
    {{ content }}
  {% endblock %}
</div>

标签: phpdrupaltwig

解决方案


而不是写My Block <br> Titlewrite My Block [br] Title

之后,将以下函数放入YOUR_THEME.theme

/**
 * Implements hook_preprocess_block()
 * @param array $variables
 */
function modern_preprocess_block(&$variables) {
  if ( !empty($variables['label']) ) {
    $variables['label'] = str_replace('[br]', '<br>', $variables['label']);
  }
}

Drupal 从title/label字段中去除 HTML 标签,以便我们可以自信地假设它是纯文本。因此,这是一个解决方法。


推荐阅读