gpt4 book ai didi

Drupal 8 - Canvas 元素正在从页面中删除

转载 作者:行者123 更新时间:2023-12-01 02:01:14 24 4
gpt4 key购买 nike

我是 Drupal 8 的新手。我正在尝试在 Drupal 8 中实现 Chart js。

我正在尝试创建一个 <canvas>元素来加载图表。但是在渲染页面时,<canvas>元素被自动删除。
在 Controller 代码下方。

class DashboardController extends ControllerBase {

public function content() {

return array(
'#type' => 'markup',
'#markup' => '<div class="chart"><canvas id="test"></canvas></div>',
);
}
}

在这里,我只得到 <div class="chart"></div>
我无法在页面中找到 Canvas 。因此,图表没有被加载。

任何人都可以帮助我找出确切的问题。

最佳答案

为迟到的答案道歉,但我自己偶然发现了这个问题,有两种“正确”的方法可以做到这一点。

来自 Render API overview :

#markup: Specifies that the array provides HTML markup directly. Unless the markup is very simple, such as an explanation in a paragraph tag, it is normally preferable to use #theme or #type instead, so that the theme can customize the markup. Note that the value is passed through \Drupal\Component\Utility\Xss::filterAdmin(), which strips known XSS vectors while allowing a permissive list of HTML tags that are not XSS vectors. (I.e, and are not allowed.) See \Drupal\Component\Utility\Xss::$adminTags for the list of tags that will be allowed. If your markup needs any of the tags that are not in this whitelist, then you can implement a theme hook and template file and/or an asset library. Aternatively, you can use the render array key #allowed_tags to alter which tags are filtered.



所以你可以实现一个主题钩子(Hook)或者做一些不太优雅的事情:
use Drupal\Component\Utility\Xss;

class DashboardController extends ControllerBase {

public function content() {

return array(
'#type' => 'markup',
'#markup' => '<div class="chart"><canvas id="test"></canvas></div>',
'#allowed_tags' => array_merge(Xss::getHtmlTagList(), ['canvas', 'div'])
);
}
}

关于Drupal 8 - Canvas 元素正在从页面中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36195221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com