Basic module building tutorial: Lorem ipsum generator

Step-by-step guide for module building beginners. 

Step-by-step guide for building a module called Lorem ipsum that generates placeholder blocks of random text. This guide walks developers through the motions of writing:

  • The basic structure;
  • Config sync;
  • Settings form;
  • Controller;
  • Services;
  • A block;
  • Theming; and
  • Testing.

Basic Concepts

Overview of Drupal's Ajax API.

This page is based on the "Ajax API" documentation

Testing a Drupal module

If you have been following this practical guide to building basic Drupal 8 modules from the beginning, we are now ready for some QA. If you just want to jump right in and play along you can grab the Lorum ipsum module directly from Drupal.

Let's check that the module works as expected by writing a few tests of our own.

Defining a block

Remember at the beginning of this tutorial when I said we'd define a block with a form? Well, now's the time to get right on it.

Settings Form

Everything's pretty neat so far, but how do we add some flexibility to our module? With some settings forms, of course.

Theming

loremipsum.module: hook_theme() implementation

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

Pages

Subscribe with RSS Subscribe to RSS - Basic Structure