Hello, everyone! The release of Drupal 8 is almost here, but its beta version is already vailable for use. So let's explore Drupal 8 together.

Creating pages and menus in Drupal 8

Probably, the first hook for everyone to implement was hook_menu (). It’s hard to believe but hook_menu() is removed from Drupal 8. The main task of hook_menu() is creating menus and pages. The new version has a new routing system to replace hook_menu (). To create a page in Drupal 8, you need to create a your_module.routing.yml file in the module folder. The content of this file looks about like this:

The getSubscribedEvents() method returns an array of names of events. The array keys are the names of the events, and values can be:

a name of the method for callback (the default priority is 0));

an array that consists of a method name for callback and a priority;

an array of arrays with names of methods and their priorities, or priority = 0 if not set.

The checkForRedirection (GetResponseEvent $event) method is a method that is called according to the set event.

In line 16, check the address of the page to which we you going. With line 17, form the url of the page to which you want to make a redirect (example_mod.mypage2 is the name of the route of the page to which you make a redirect). In line 18, set a response to the event, in this case it’s an object of RedirectResponse class with a link as a parameter.

Adding files of scripts and css styles in Drupal 7 is easy to do through hook_init(). It looks like this:

In the getFormId() method, return a form identifier. For creating forms, the buildForm() is responsible, where its elements are actually created.The form validation should be implemented in the validateForm() method.

More information on creating forms in Drupal 8 can be found here and here.

hook_cron () in Drupal 8

I would also like to provide a simple example of hook_cron() work on Drupal 8. Let cron add the nid of this node in brackets to the title of all new nodes. I chose this example to kill two birds with one stone. First, we will see how cron works, and second, we will learn to receive and to change the values of the node fields.

Let's see in more detail which line is responsible for which action. In line (2), we create an array in which, using node_load_multiple() function, we load all nodes. Using line (3), we get the value of the last cron running. In lines (5), (6), (7) we get the values of the required fields. With line (10) we set the necessary value of the node title, in line (11) we save the node using the save() method. And finally we set current time in the value of the last cron run. As you see, everything is very easy.

Using your own twig pattern with hook_theme()()

As is known, in Drupal 8, instead of Phptemplate templates, Twig templates are used. So let’s see how, using hook_theme(), to make Drupal use its own template. For example, let's replace the 'block__system_menu_block template with your own, let’s call it custom-menu-template.html.twig. Setting your own template looks like this:

By default, the option of debugging template files is disabled. To enable this feature, you need to set this in the sites/default/services.yml file:

twig.config:
debug: true

To output variables, you can use the Devel module. Now you can display information about the variables using the dump() and kint() functions. kint() displays information in a fairly readable way for a programmer:

Hope these Drupal 8 tips will make your coding easier and more fruitful. Good luck!