繁体   English   中英

允许原始过滤器在细枝的块视图中处理标签变量

[英]Allow raw filter to work on label variable in twig's block view

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

到目前为止,我尝试添加的是原始树枝标签。

{#
/**
 * @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>

而不是写My Block <br> TitleMy 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标签,因此我们可以放心地假设它是纯文本。 结果,这里是一种解决方法。

暂无
暂无

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

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