Overwriting code in Drupal Vasile CHINDRIS vasi1186 d.o Why to - - PowerPoint PPT Presentation

overwriting code in drupal
SMART_READER_LITE
LIVE PREVIEW

Overwriting code in Drupal Vasile CHINDRIS vasi1186 d.o Why to - - PowerPoint PPT Presentation

Overwriting code in Drupal Vasile CHINDRIS vasi1186 d.o Why to overwrite? There is no magic solution that solves all our problems. We are not happy with the existing implementation. Wrap the current implementation. ...


slide-1
SLIDE 1

Overwriting code in Drupal

Vasile CHINDRIS

vasi1186 d.o

slide-2
SLIDE 2

Why to overwrite?

  • There is no “magic” solution that solves all our problems.
  • We are not happy with the existing implementation.
  • Wrap the current implementation.
  • ... plenty other reasons.
  • Good frameworks should (easily) allow overwriting the code.

without touching the core!

slide-3
SLIDE 3

What to overwrite?

  • Classes.
  • Functions (possible in a few cases in Drupal).
  • Every piece of the framework (ideal case).

and again, without touching the core!

slide-4
SLIDE 4

Overwriting menu items: D7

hook_menu_alter(&$items)

slide-5
SLIDE 5
slide-6
SLIDE 6
slide-7
SLIDE 7

Overwriting menu items: D8

  • There is no hook_menu_alter() in D8.
  • We have to alter the routes defined in the routing.yml file.
  • We use an EventSubscriber.
slide-8
SLIDE 8

The event subscriber is declared in the utils.services.yml file

slide-9
SLIDE 9
slide-10
SLIDE 10

In lib\Drupal\utils\Controller\CustomNodeController.php:

slide-11
SLIDE 11
slide-12
SLIDE 12

Overwriting menu items: real use case

We have a hook_menu():

slide-13
SLIDE 13

When using a file that is uploaded with ajax. Why?

slide-14
SLIDE 14
slide-15
SLIDE 15

Overwrite existing hooks: D7

hook_module_implements_alter(&$implementations, $hook)

slide-16
SLIDE 16

Use case: remove the hook_user_view() from the user module.

slide-17
SLIDE 17
slide-18
SLIDE 18
slide-19
SLIDE 19

A real use case

You have a module called “action” on your site.

slide-20
SLIDE 20
slide-21
SLIDE 21

Overwriting existing hooks: D8

  • Good news: the same as in D7!
  • hook_module_implements_alter() exists also in D8
slide-22
SLIDE 22

Overwrite views plugins: D7

  • It is a PHP class: views_plugin_pager_full
  • Extend the class: class A extends B
  • Views full pager plugin.
slide-23
SLIDE 23
  • hook_views_plugins()
  • hook_views_plugins_alter()

How is the B class instantiated in the current implementation?

slide-24
SLIDE 24
slide-25
SLIDE 25

Overwrite views handlers: D7

  • How are they created?
  • The same as plugins are, in _views_create_handler()
slide-26
SLIDE 26
  • hook_views_handlers_alter()
  • Ooops: there is no hook_views_handlers_alter()
  • Make it the hard way: alter the file registry.
  • hook_registry_files_alter()
slide-27
SLIDE 27

Add to the custom.info file: Copy the entire code from the original file to the new file. Big issue: we are not extending, but cloning core!

slide-28
SLIDE 28

Put the original class into a new file and change only the name

  • f the class

Add the file to the files array in the .info file: Extend the “new” original class:

slide-29
SLIDE 29
  • What happens when the module updates?
  • Manually update the class.
  • Goal 99% achieved: did not change any implementation,
  • nly the class name.
  • Can be used with any classes in D7.
slide-30
SLIDE 30

Overwrite views plugins: D8

  • How are they constructed?
  • A bit different than D7.
  • But, we have the hook_views_plugins_pager_alter()
  • There is an alter hook for every plugin:

hook_views_plugin_pluginname_alter()

slide-31
SLIDE 31

Overwrite views handlers: D8

  • Same issue as in D7, no alter hook.
  • Still to find a way to overwrite them, cannot apply the

same solution as in D7.

  • One alternate solution: hook_views_data_alter().
  • The handler class name is stored in the

cache_views_info table.

slide-32
SLIDE 32

Conclusions

  • Evaluate first the code you want to overwrite.
  • Many things in Drupal can be overwritten with alter hooks.
  • When extending classes, if possible do not do it the “hard

way”.

slide-33
SLIDE 33

Overwriting code in Drupal

Thank you! Questions?