Better Practice, Dec. 2018

(singke) #1
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


  1. <?php

  2. /**



    • @file





    • This is the Sweet module for altering the main menu.











    • modules/custom/sweet/sweet.module



  3. */



  4. use \Drupal\Core\Url;



  5. /**



    • Implements hook_preprocess_hook().



  6. */

  7. function sweet_preprocess_menu__main(&$variables) {

  8. // Copy the first menu item for simplicity.

  9. $new_menu_item = reset($variables['items']);

  10. // Set the title value, wrapping in translation function.

  11. $new_menu_item['title'] = t('Treats');

  12. // Set the url to the search page filtered on 'chocolate'

  13. $new_menu_item['url'] = new Url(

  14. 'search.view_node_search', [],

  15. ['query' => [

  16. 'keys' => 'chocolate',

  17. ]]);

  18. // Get current page route name.

  19. $current_route = \Drupal::routeMatch()->getRouteName();

  20. // Show active trail attribute if on the active trail.

  21. if ($current_route == 'search.view_node_search') {

  22. $new_menu_item['in_active_trail'] = TRUE;

  23. }

  24. // Add the menu item to the items array.

  25. $variables['items']['sweet.treats'] = $new_menu_item;

  26. }


Figure 12

Figure 13
Free download pdf