You are here

trait DescriptionTemplateTrait in Examples for Developers 8

Same name and namespace in other branches
  1. 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

See also

\Drupal\Core\Render\Element\InlineTemplate

https://www.drupal.org/developing/api/8/localization

16 files declare their use of DescriptionTemplateTrait
AjaxExampleController.php in ajax_example/src/Controller/AjaxExampleController.php
BlockExampleController.php in block_example/src/Controller/BlockExampleController.php
FieldExampleController.php in field_example/src/Controller/FieldExampleController.php
HooksExampleController.php in hooks_example/src/Controller/HooksExampleController.php
JsExampleController.php in js_example/src/Controller/JsExampleController.php

... See full list

File

src/Utility/DescriptionTemplateTrait.php, line 26

Namespace

Drupal\examples\Utility
View 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_get_path('module', $this
      ->getModuleName()) . "/templates/description.html.twig";
  }

}

Members

Namesort descending Modifiers Type Description 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. 16