I want to set up a Drupal 8 module that will allow me to define different block types (that are somewhat related) with different twig templates for each block.
Currently, I have a folder structure that looks like this:
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
Here's a look at my TwoColumn class:
<?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'),
];
}
}
And my block--templater-two-column.html.twig
simply has {{ title }}
My First Problem The template files don't in fact work in the location I specified above. I've actually had to move them to my theme for them to work properly. I would really like to keep the template files within the module itself. Does anyone know what I would need to do to enable this?
My Second Problem I know when I first started, I was getting the rendering of {{ title }}
to appear on the page. However, nothing seems to render out of {{ title }}
any longer. Not sure what I changed to make this happen.
Lastly, a look at my templater.module
file:
<?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';
}
I figured it all out:
My templater.module
file.
/**
* 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';
}
My TemplaterTwoColumnBlock.php
file:
<?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.'),
];
}
}