#php #drupal #twig #drupal-modules #drupal-8
#php #drupal #twig #drupal-модули #drupal-8
Вопрос:
Я хочу настроить модуль Drupal 8, который позволит мне определять разные типы блоков (которые несколько связаны) с разными шаблонами twig для каждого блока.
В настоящее время у меня есть структура папок, которая выглядит следующим образом:
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 DrupaltemplaterPluginBlockTemplaterBlock.
*/
namespace DrupaltemplaterPluginBlock;
/**
* 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(amp;$variables) {
$variables['#attached']['library'][] = 'templater/global-styling';
}
Комментарии:
1. Я смог во всем разобраться.
Ответ №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(amp;$variables) {
$variables['#attached']['library'][] = 'templater/global-styling';
}
Мой TemplaterTwoColumnBlock.php
файл:
<?php
/**
* @file
* Contains DrupaltemplaterPluginBlockTemplaterBlock.
*/
namespace DrupaltemplaterPluginBlock;
/**
* 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.'),
];
}
}