Drupal Module Development

Lists Module

Lists Module Development

Create the Module’s Directory

mkdir <site>/modules/custom/lists

Create the Module’s Info File

The module’s info file tells Drupal that the module exists and supplies some beginning configuration information.

create <site>/modules/custom/lists/lists.info.yml
name: Lists
description: A Drupal 8 module which manages a list
package: Lists
type: module
version: 1.0
core: 8.x

Install the Module within Drupal

Install the module by checking the lists entry at http://<url>/admin/modules.

Create the Module’s Module File

The module’s module file contains code which is loaded into Drupal each time a user accesses the Drupal site.

The module file will contain Drupal hooks which are functions that execute when some predefined event happens on the Drupal site (e.g., a new node is inserted, content is updated, a user logins to the system). Drupal hooks are similar to Drupal rules but at the programming level rather than the user interface level.

You can also include code that is shared among the module’s controllers in the module file. The module file is loaded each time a Drupal site is accessed. I believe the controller code is loaded only when that controller is needed to run. Thus, it is more efficient to put code in the controller files that is specific to the controller’s functionality.

We’re going to start by creating code to display a list. We’ll add a function to the module file that will eventually make that happen.

create <site>/modules/custom/lists/lists.module

function lists_display_list() {
   return t('Display List');

Model-View-Controller (MVC)-based Applications

Drupal is a Model-View-Controller (MVC)-based application.

  • The Model represents the underlying data that the application operates against.
  • The View defines the user interface to the application.
  • The Controller is the workhorse of the applications, including routing requests from users and returning information to the view to display to the user.

Create the Module’s Routing File

The routing file defines how a visitor will access the functionality of our module, as defined by the controller, and returns the values to be displayed.

create <site>/modules/custom/lists/lists.routing.yml
  path: 'displaylist'
    _controller: '\Drupal\lists\Controller\ListsController::displaylist'
    _permission: 'access content'

Create the Module’s Controller

create <site>/modules/custom/lists/src/Controller/ListsController.php

namespace Drupal\lists\Controller;

use Drupal\Core\Controller\ControllerBase;

class ListsController extends ControllerBase {

  public function displaylist() {
    return array(
      '#markup' => lists_display_list(),


Add a Menu Item

Now, let’s add a menu item that allows easy access to the display list functionality.

create <site>/modules/custom/lists/lists.links.menu.yml
  title: Display List
  menu_name: main
  route_name: displaylist
  expanded: TRUE
  weight: 100

Display List Functionality

So, we’ve created four files that we will expand that serve as a skeleton for the display list functionality and future functionality for our Drupal module.

Add functionality to insert an item into the list

So, let’s all the skeleton files for functionality to insert an item into the list.

update <site>/modules/custom/lists/lists.module
function lists_insert_list() {
   return t('Insert List');
update <site>/modules/custom/lists/lists.routing.yml
  path: 'insertlist'
    _controller: '\Drupal\lists\Controller\ListsController::insertlist'
    _permission: 'access content'
update <site>/modules/custom/lists/src/Controller/ListsController.php
public function insertlist() {
  return array(
    '#markup' => lists_insert_list(),
update <site>/modules/custom/lists/lists.links.menu.yml
  title: Insert List
  menu_name: main
  route_name: insertlist
  expanded: TRUE
  weight: 110

Create the Module’s Block Plugin

Let’s create a block that will display the items in the list.

create <site>/modules/custom/lists/src/Plugins/Block/listblock.php

namespace Drupal\lists\Plugin\Block;

use Drupal\Core\Block\BlockBase;

 * Provides a list block.
 * @Block(
 *   id = "lists_block",
 *   admin_label = @Translation("List Block")
 * )

class ListBlock extends BlockBase {

   * {@inheritdoc}

  public function build() {
    return array(
      '#markup' => $this->t("List Information"),


Place the Module’s Block

Place the module’s block at http://md.knocout.com/admin/structure/block.

Create a ListItem type

Create a list item type that has a title field (called item) and a body field (called details) using Drupal’s User Interface.

Create some list items.

Create some list items using Drupal’s User Interface.

Update the lists_display_list to display the list.

Add code to lists_display_list to display the list. The following code should be worked into that file.

$nids = \Drupal::entityQuery('node')
  ->condition('status', 1)
  ->condition('type', '<YOUR-NODE-TYPE>')
$nodes = \Drupal\node\Entity\Node::loadMultiple($nids);

$query = new EntityFieldQuery();
      $query = $query->entityCondition('entity_type', 'node')
      ->propertyCondition('status',1)->propertyCondition('uid', $uid);
      $nodes = $query->execute();

      foreach($nodes as $node)

    $node = node_load($node);

    $old_title = $node->title;

    if(strpos($old_title,'(MODERATED USER)') == FALSE)
        $node->title =  $old_title . " (MODERATED USER)";

      drupal_set_message('All nodes of the user updated');


Update the listblock to display the list titles.

Add code to display the number of items in the list in the block.

Update the lists_insert_list to insert items into the list.

Add code to lists_insert_list to insert items into the list. The following code should be worked into that file.

use Drupal\node\Entity\Node;

$node = Node::create(['type' => '<node_type>']);

$node->set('title', '<title>');

$body = [
'value' => '<body_text>',
'format' => 'basic_html',
$node->set('body', $body);

$node->set('uid', <uid>);

$node->status = 1;


drupal_set_message("List item with nid " . $node->id() . " has been created.\n");

Title: Drupal Module Development
Subtitle: Lists Module
Author: Dr. Mark A. Friedman
Email: drmarkafriedman@drmarkafriedman.com
Date: Monday, January 7, 2019
Updated: 2019-01-07 Mon 14:33