【问题标题】:Allow raw filter to work on label variable in twig's block view允许原始过滤器在树枝的块视图中处理标签变量
【发布时间】:2019-08-06 14:13:29
【问题描述】:

我想要实现的是在我的标签元素上设置原始过滤器,这样如果用户决定在 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>

【问题讨论】:

  • 先验证您的“html”是否已编码存储在数据库中
  • @DarkBee 不,我不这么认为,当我编辑块时,我看到我的 BR 在那里,当它渲染块时,它只是“剥离”标签。跨度>
  • 如果您将label 转储到您的控制器中?
  • @DarkBee 遗憾的是,在 Drupal 中,所有的“魔法”似乎都来自 xxx.theme 文件,我在其中找不到负责该块的部分。我也是 Drupal 的菜鸟,所以我可能错了。但是当我在视图中转储标签时,它已经被解析,没有任何标签。

标签: php drupal twig


【解决方案1】:

不要写My Block &lt;br&gt; Title,而是写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 标签,这样我们就可以自信地假设它是纯文本。因此,这里有一个变通方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多