logo

Extra Block Types (EBT) - Nieuwe Layout Builder ervaring❗

Extra Block Types (EBT) - gestileerde, aanpasbare bloktypes: Slideshows, Tabs, Cards, Accordions en vele andere. Ingebouwde instellingen voor achtergrond, DOM Box, javascript-plugins. Ervaar vandaag al de toekomst van layout building.

Demo EBT-modules Download EBT-modules

❗Extra Paragraph Types (EPT) - Nieuwe Paragraphs ervaring

Extra Paragraph Types (EPT) - analoge op paragrafen gebaseerde set modules.

Demo EPT-modules Download EPT-modules

Scroll

Een themasjabloon toevoegen aan een Drupal-module

03/10/2025, by Ivan

Menu

Deel III van de praktische handleiding voor het maken van basis Drupal 8-modules
Van .info tot tests, alleen de basis

loremipsum.module

/**
 * Implements hook_theme().
 */
function loremipsum_theme($existing, $type, $theme, $path) {
  $variables = array(
    'loremipsum' => array(
      'variables' => array(
        'source_text' => NULL,
      ),
      'template' => 'loremipsum',
    ),
  );
  return $variables;
}

Een andere reden om het .module-bestand niet weg te laten is dat hook_theme() daar moet worden geplaatst. Dit werkt bijna hetzelfde als in D7: u declareert een array die uw variabelen en het sjabloonbestand bevat, dat op de juiste plek (de map templates) moet worden opgeslagen met de extensie .html.twig.

Vervolgens kunt u, voordat u de render-array naar Twig doorgeeft, een preprocess uitvoeren. De volgende hook voegt willekeurige leestekens toe aan het einde van elke zin:

/**
 * Template preprocess function for Lorem ipsum.
 *
 * @param array $variables
 *   An associative array containing:
 *   - source_text
 */
function template_preprocess_loremipsum(&$variables) {
  $punctuation = array('. ', '! ', '? ', '... ', ': ', '; ');
  for ($i = 0; $i < count($variables['source_text']); $i++) {
    $big_text = explode('. ', $variables['source_text'][$i]);
    for ($j = 0; $j < count($big_text) - 1; $j++) {
      $big_text[$j] .= $punctuation[floor(mt_rand(0, count($punctuation) - 1))];
    }
    $variables['source_text'][$i] = implode('', $big_text);
  }
}

/templates/loremipsum.html.twig

{#
/**
 * @file
 * Default theme implementation to print Lorem ipsum text.
 *
 * Available variables:
 *   - source_text
 *
 * @see template_preprocess_loremipsum()
 *
 * @ingroup themeable
 */
#}
<div class="loremipsum">
{% for item in source_text %}
  <p>{{ item }}</p>
{% endfor %}
</div>

Nu wordt de array $source_text verwerkt met een eenvoudige for-lus binnen onze Twig-template, omringd door <p>-tags.

Let op de overeenkomst tussen hook_theme(), template_preprocess_hook() en ons Twig-bestand:

06_0

07_0

08_0