http://www.phparch.com \ December 2018 \ 17
The Flexibility of Drupal 8
instances in a *.module file. The second way is to use a custom
theme and place hooks in a *.theme file. When implementing
a hook, the PHP function won’t be discovered or executed
unless it follows a specific naming convention: the name of
the hook prefixed by the name of the module or theme.
Example of implementing the field_widget_info_alter
hook in a module.
function mymodule_field_widget_info_alter(array &$info) {
// Let a new field type re-use an existing widget.
$info['options_select']['field_types'][] = 'my_field_type';
}
Changing a Menu Item Using Hooks
The Twig debug HTML markup suggests menu__main (note
the double underscores) as a hook for altering menu items.
We can create a custom module named Sweet to implement
a hook using this suggestion. Custom modules reside in the
modules/custom/ directory off of the Drupal root. Drupal
requires a *.info.yml file to recognize the existence of a
module.
The following sweet.info.yml file defines the custom Sweet
module.
name: Sweet
type: module
description: 'A custom module to add some sweetness to a menu.'
version: VERSION
core: 8.x
The file sweet.module contains the implementation of the
hook. It uses a new function named sweet_preprocess_menu__
main to alter data before being rendered by the menu template.
As seen in Listing 6, this hook instance contains logic to artifi-
cially add a new menu item with the label of “Treats”.
Enabling the module allows the hook instance to alter the
data before rendering. The admin toolbar is used to navigate
to the Module Administration page by clicking on Manage
and Extend. As shown in Figure 12, filtering the module list
by the term “Sweet” helps find the custom module to enable.
After enabling the module, the custom hook instance
alters the data before Drupal renders the main menu. This
data alteration creates an additional menu item with the label
“Treats”, as shown in Figure 13.
Services
The adoption of the Symfony framework in Drupal 8 intro-
duced many new development methods. One such method
is services: swappable sets of PHP classes that control core
functionality.
Drupal 8 uses services in a wide variety of ways: to control
database access, send emails, build menu trees, and much
more. Developers can define new services, as well as replace
the existing ones used by Drupal. This flexibility gives devel-
opers the ability to alter the core of how Drupal works.
Listing 6
- <?php
- /**
- @file
- This is the Sweet module for altering the main menu.
- modules/custom/sweet/sweet.module
- */
- use \Drupal\Core\Url;
- /**
- Implements hook_preprocess_hook().
- */
- function sweet_preprocess_menu__main(&$variables) {
- // Copy the first menu item for simplicity.
- $new_menu_item = reset($variables['items']);
- // Set the title value, wrapping in translation function.
- $new_menu_item['title'] = t('Treats');
- // Set the url to the search page filtered on 'chocolate'
- $new_menu_item['url'] = new Url(
- 'search.view_node_search', [],
- ['query' => [
- 'keys' => 'chocolate',
- ]]);
- // Get current page route name.
- $current_route = \Drupal::routeMatch()->getRouteName();
- // Show active trail attribute if on the active trail.
- if ($current_route == 'search.view_node_search') {
- $new_menu_item['in_active_trail'] = TRUE;
- }
- // Add the menu item to the items array.
- $variables['items']['sweet.treats'] = $new_menu_item;
- }
Figure 12
Figure 13