trait DescriptionTemplateTrait
Same name in other branches
- 3.x src/Utility/DescriptionTemplateTrait.php \Drupal\examples\Utility\DescriptionTemplateTrait
Trait to implement a "drop-in" template for Example's controllers.
This is a simple utility trait to allow our example modules to put their explanatory text into a twig template, and pass any variables needed for the template. By default, the template will be named 'description.html.twig, and should be placed in the module's templates/ directory.
These templates should be localizable as is usual for Drupal's Twig templates, using the {% trans } and {% endtrans %} tags to block out the text that needs to be passed to the translator. Modules using this trait should:
- Implement the getModuleName() member function.
- Override the getDescriptionVariables() member function in order to pass variables to Twig needed to render your template.
Hierarchy
- trait \Drupal\examples\Utility\DescriptionTemplateTrait
See also
\Drupal\Core\Render\Element\InlineTemplate
https://www.drupal.org/developing/api/8/localization
17 files declare their use of DescriptionTemplateTrait
- AjaxExampleController.php in modules/
ajax_example/ src/ Controller/ AjaxExampleController.php - BlockExampleController.php in modules/
block_example/ src/ Controller/ BlockExampleController.php - FieldExampleController.php in modules/
field_example/ src/ Controller/ FieldExampleController.php - HooksExampleController.php in modules/
hooks_example/ src/ Controller/ HooksExampleController.php - JsExampleController.php in modules/
js_example/ src/ Controller/ JsExampleController.php
File
-
src/
Utility/ DescriptionTemplateTrait.php, line 26
Namespace
Drupal\examples\UtilityView source
trait DescriptionTemplateTrait {
/**
* Generate a render array with our templated content.
*
* @return array
* A render array.
*/
public function description() {
$template_path = $this->getDescriptionTemplatePath();
$template = file_get_contents($template_path);
$build = [
'description' => [
'#type' => 'inline_template',
'#template' => $template,
'#context' => $this->getDescriptionVariables(),
],
];
return $build;
}
/**
* Name of our module.
*
* @return string
* A module name.
*/
protected abstract function getModuleName();
/**
* Variables to act as context to the twig template file.
*
* @return array
* Associative array that defines context for a template.
*/
protected function getDescriptionVariables() {
$variables = [
'module' => $this->getModuleName(),
];
return $variables;
}
/**
* Get full path to the template.
*
* @return string
* Path string.
*/
protected function getDescriptionTemplatePath() {
return \Drupal::service('extension.list.module')->getPath($this->getModuleName()) . '/templates/description.html.twig';
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overrides |
---|---|---|---|---|
DescriptionTemplateTrait::description | public | function | Generate a render array with our templated content. | |
DescriptionTemplateTrait::getDescriptionTemplatePath | protected | function | Get full path to the template. | |
DescriptionTemplateTrait::getDescriptionVariables | protected | function | Variables to act as context to the twig template file. | 1 |
DescriptionTemplateTrait::getModuleName | abstract protected | function | Name of our module. | 17 |