When:
to
Room:
Room 2
Tags:
development & coding, drupal & open source 101
Track:
SVG
m&b icon_new brand
coding & site building

Hooks, from magic names to php language feature

Hooks, from magic names to php language feature

Sally Young (Justafish), Fabian Bircher (Bircher)

In bygone versions you could extend Drupal by creating a special module file and write php functions with magic names like mymodule_block_info or mymodule_form_alter. In Drupal 11 almost all of that code lives in particular places and has php attributes but it doesn't have to be frightening.

Prerequisite
All you need to know is some features of modern php.
No familiarity with Drupal 7 and before is required.
While the session makes a nod at how "easy" it was to hook into Drupal, it makes the argument that with modern php you have the same basis but a lot of improvements for DX.

Outline
Code for Drupal 11.1 looks almost like magic again for someone who was used to the magic naming that kept working the same way even in Drupal 11.0.
Hook_block_info has become a plugin a long time ago, and recently plugins changed to use php annotations.
But the big change that happened in Drupal 11.1 is that most of the hooks can now be methods on classes. This allows for better code organisation and better testing because one can use dependency injection.
We will showcase that implementing hooks the new way is just as easy and everyone should know about it.

Learning Objectives
You will learn how easy it is to adapt your code to the object oriented way of defining hooks and see that you can test your code much better.

Experience level
Beginner