Articles

Part III of the practical guide to creating basic Drupal 8 modules
From .info to tests, just the basics

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 VI of the practical guide to creating basic Drupal 8 modules
From .info to tests, just the basics

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.
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.

Hooks are one of the ways modules interact with other contributed modules or Drupal core subsystems. Hooks are used for a variety of tasks, including preprocessing variables for template files (hook_preprocess()
), modifying information lists (hook_tokens_alter()
, hook_views_data_alter()
), and manipulating forms (hook_form_alter()
), among other things. This page lists all hooks provided by core.

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 documentation is for modules. For information about themes, see Adding stylesheets (CSS) and JavaScript (JS) to a Drupal 8 theme.
In Drupal 8, stylesheets (CSS) and JavaScript (JS) are loaded using the same system for both modules (code) and themes: the asset library. Asset libraries can include one or more CSS resources, one or more JS resources, and one or more JS settings.

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.

Drupal 8 introduces many new APIs and includes only some of the APIs from Drupal 7/6. This section provides a detailed overview of the APIs available in Drupal 8.
Before diving into the documentation below, you might want to review the fundamentals and prerequisites as well as the Drupal 8.x module development guides, which explain some of the concepts and terminology assumed in the API documentation here.

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.

API Features
Organized by the least frequently used APIs:
Authentication Provider Services
Implement \Drupal\Core\Authentication\AuthenticationProviderInterface
and use the service tag 'authentication_provider'
.
_auth Option on Routes
The default authentication manager (see below) allows developers to restrict the set of allowed authentication mechanisms to a specified subset by defining _auth
in the route options.
Example: _auth: ['basic_auth', 'cookie']

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.

Overview
Blocks in Drupal 8 actually consist of two separate API structures to create a user interface similar to previous iterations of Drupal. These two APIs are the Block Plugin API, which is a reusable standalone API, and the Block Entity API, which is a Drupal 8-specific implementation for placing blocks and controlling their visibility.

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.
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.

The Cache API has been significantly improved in Drupal 8. The following sections explore each feature in more detail.
For a quick overview, see the Cache API documentation page.
Cacheability Metadata
Everything that is rendered directly or used to determine what to render provides cacheability metadata — from access results to entities and URLs.
Cacheability metadata consists of three properties:

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.