【发布时间】:2016-10-14 15:28:29
【问题描述】:
我想设置一个 Drupal 8 模块,它允许我为每个块使用不同的树枝模板定义不同的块类型(有些相关)。
目前,我的文件夹结构如下所示:
modules
custom
templater
src
Plugin
Block
TemplaterBaseBlock.php
TemplaterTwoColumnBlock.php
TemplaterThreeColumnBlock.php
templates
block--templater-two-column.html.twig
block--templater-three-column.html.twig
看看我的 TwoColumn 类:
<?php
/**
* @file
* Contains \Drupal\templater\Plugin\Block\TemplaterBlock.
*/
namespace Drupal\templater\Plugin\Block;
/**
* Provides a 'Templater Two Column' Block
*
* @Block(
* id = "templater_two_column",
* admin_label = @Translation("Templater Two Column"),
* )
*/
class TemplaterTwoColumnBlock extends TemplaterBaseBlock {
/**
* {@inheritdoc}
*/
public function build() {
return [
'#title' => $this->t('test'),
];
}
}
而我的block--templater-two-column.html.twig 只是有{{ title }}
我的第一个问题 模板文件实际上在我上面指定的位置不起作用。实际上,我必须将它们移至我的主题才能正常工作。我真的很想将模板文件保留在模块本身中。有谁知道我需要做什么才能启用它?
我的第二个问题
我知道当我第一次开始时,我得到了{{ title }} 的渲染出现在页面上。但是,{{ title }} 似乎不再呈现任何内容。不知道我做了什么改变才能做到这一点。
最后,看看我的templater.module 文件:
<?php
/**
* @file
* Code for the example module.
*/
/**
* Theme hook
*/
function templater_theme($existing, $type, $theme, $path) {
return [
'templater' => [
'variables' => [
'title' => null,
],
],
];
}
function templater_preprocess_page(&$variables) {
$variables['#attached']['library'][] = 'templater/global-styling';
}
【问题讨论】:
-
我能够弄清楚所有事情。
标签: php drupal twig drupal-modules drupal-8