我是Drupal 8的新手.我正在尝试在Drupal 8中实现Chart js.
我正在尝试创建一个< canvas>要加载图表的元素.但在渲染页面时,< canvas>元素被自动删除.
在控制器代码下面.
class DashboardController extends ControllerBase {
public function content() {
return array(
'#type' => 'markup',
'#markup' => '<div class="chart"><canvas id="test"></canvas></div>',
);
}
}
在这里,我只得到< div class =“chart”>< / div>
我无法在页面中找到画布.因此,图表不会被加载.
任何人帮我弄清楚确切的问题.
最佳答案 对于迟到的回答道歉,但我自己偶然发现了这个问题,有两种“正确”的做法.
#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.
所以你可以实现一个主题钩子或做一些不那么优雅的事情:
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'])
);
}
}