Articles

Part IV of the Practical Guide to Creating Basic Drupal 8 Modules
From .info to Tests, Just the Basics
So far, everything is pretty tidy, but how can we change what we see? With some forms, of course.

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.

Main Topic: Defining Your Own Configuration
You can include default configuration in your module based on the functionality of other modules (content types, views, fields, text formats, etc.).
For example, the Node module provides a content type configuration, so in your own module, you define a default content type that can be shipped with your module.

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.

There are two steps to creating a simple page in Drupal:
Declare the route and its parameters.
This step includes the page title, access requirements, and more.
In Drupal 7, you had to implement hook_menu()
.
In Drupal 8, create a <module_name>.routing.yml
file in the top-level directory of your module.
Write the code that returns the page content.
In Drupal 7, you would write a page callback function specified in hook_menu()
.

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.

Unlike in Drupal 7, creating multiple instances of a block to place on your site is a simple task in Drupal 8.
This tutorial will teach you how to programmatically add a block to the block layout interface, how to add an (administrative) configuration form to the block, and how to handle it. Finally, you’ll learn how to define and display default configuration values for the form.
Before starting this step-by-step guide, prepare your module skeleton as described in “Prepare the module skeleton.”

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.

Blocks in Drupal 8 are instances of the block plugin.
The Drupal Block Manager scans your modules for classes containing the @Block annotation.
The example below uses the @Block
annotation with the properties id
and admin_label
to define a custom block.

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.

Now let’s say we want to allow the site builder to enter some configuration for each instance of our custom block. Always keep in mind that all site-building configuration in Drupal 8 can be exported from a development site and imported into a live site (known as Configuration Management). As a module builder, you can also provide default configuration to pre-fill the form when a site builder creates a new block.

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.

Add the following method to the HelloBlock
class. In this example, it's located in the src/Plugin/Block/HelloBlock.php
file, but as you start thinking in a more OOP-oriented way, where it's physically located in the file structure is less important than the namespace. If you're a savvy OO programmer, you'll keep both closely aligned. But just in case, the namespace—very similar to the module's folder name and machine name from our earlier discussion—will be important later when you want to interact with your module’s code programmatically.

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.

To use block instance configuration, we can modify the build()
method of the HelloBlock
class:
/** * {@inheritdoc} */ public function build() { $config = $this->getConfiguration(); if (!empty($config['hello_block_name'])) { $name = $config['hello_block_name']; } else { $name = $this->t('to no one'); } return [ '#markup' => $this->t('Hello @name!', [ '@name' => $name, ]), ]; }

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.

Part V of the Practical Guide to Creating Basic Drupal 8 Modules
From .info to tests, just the basics
Remember at the beginning of this tutorial I mentioned we’d define a block with a form? Well, now is the time to dive into it.

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.

This guide includes tutorials on how to create a custom field type, widget, and formatter in Drupal 8.

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.