我想设置一个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';
}
答案 0 :(得分:1)
我认为这一切:
我的templater.module
文件。
/**
* Theme hook
*/
function templater_theme($existing, $type, $theme, $path) {
return [
'templater_two_column' => [
'variables' => [
'text' => null,
],
],
'templater_three_column' => [
'variables' => [
'text' => null,
],
],
];
}
function templater_preprocess_page(&$variables) {
$variables['#attached']['library'][] = 'templater/global-styling';
}
我的TemplaterTwoColumnBlock.php
文件:
<?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 [
'#theme' => 'templater_two_column',
'#text' => $this->t('Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.'),
];
}
}