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 implem...
What to overwrite?
● Classes.
● Functions (possible in a few cases in Drupal).
● Every piece of the framework (ideal case)...
Overwriting menu items: D7
hook_menu_alter(&$items)
Overwriting menu items: D8
● There is no hook_menu_alter() in D8.
● We have to alter the routes defined in the routing.yml...
The event subscriber is declared in the utils.services.yml file
In libDrupalutilsControllerCustomNodeController.php:
Overwriting menu items: real use case
We have a hook_menu():
When using a file that is uploaded with ajax.
Why?
Overwrite existing hooks: D7
hook_module_implements_alter(&$implementations, $hook)
Use case: remove the hook_user_view() from the user module.
A real use case
You have a module called “action” on your site.
Overwriting existing hooks: D8
● Good news: the same as in D7!
● hook_module_implements_alter() exists also in D8
Overwrite views plugins: D7
● It is a PHP class: views_plugin_pager_full
● Extend the class: class A extends B
● Views ful...
● hook_views_plugins()
● hook_views_plugins_alter()
How is the B class instantiated in the current implementation?
Overwrite views handlers: D7
● How are they created?
● The same as plugins are, in _views_create_handler()
● hook_views_handlers_alter()
● Ooops: there is no hook_views_handlers_alter()
● Make it the hard way: alter the file regi...
Add to the custom.info file:
Copy the entire code from the original file to the new file.
Big issue: we are not extending,...
Put the original class into a new file and change only the name
of the class
Add the file to the files array in the .info ...
● What happens when the module updates?
● Manually update the class.
● Goal 99% achieved: did not change any implementatio...
Overwrite views plugins: D8
● How are they constructed?
● A bit different than D7.
● But, we have the hook_views_plugins_p...
Overwrite views handlers: D8
● Same issue as in D7, no alter hook.
● Still to find a way to overwrite them, cannot apply t...
Conclusions
● Evaluate first the code you want to overwrite.
● Many things in Drupal can be overwritten with alter hooks.
...
Overwriting code in Drupal
Thank you!
Questions?
Overwriting code in Drupal
Overwriting code in Drupal
Overwriting code in Drupal
Overwriting code in Drupal
Overwriting code in Drupal
Overwriting code in Drupal
Overwriting code in Drupal
Overwriting code in Drupal
Overwriting code in Drupal
Upcoming SlideShare
Loading in …5
×

Overwriting code in Drupal

3,619 views

Published on

This session is supposed to present various ways to overwrite the default behaviour of the drupal core and some of the well-known contributed modules (like views or panels). The focus will be on Drupal 7, but each case will also have the Drupal 8 correspondent. This will go beyond the things that can be overwrite with alter hooks, and will try to present cases when some specific behavior of a class or small pieces of code have to be overwritten. The attendees should be familiar with Drupal and should have developed at least a few modules before.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,619
On SlideShare
0
From Embeds
0
Number of Embeds
2,314
Actions
Shares
0
Downloads
4
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Overwriting code in Drupal

  1. 1. Overwriting code in Drupal Vasile CHINDRIS vasi1186 d.o
  2. 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!
  3. 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!
  4. 4. Overwriting menu items: D7 hook_menu_alter(&$items)
  5. 5. 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.
  6. 6. The event subscriber is declared in the utils.services.yml file
  7. 7. In libDrupalutilsControllerCustomNodeController.php:
  8. 8. Overwriting menu items: real use case We have a hook_menu():
  9. 9. When using a file that is uploaded with ajax. Why?
  10. 10. Overwrite existing hooks: D7 hook_module_implements_alter(&$implementations, $hook)
  11. 11. Use case: remove the hook_user_view() from the user module.
  12. 12. A real use case You have a module called “action” on your site.
  13. 13. Overwriting existing hooks: D8 ● Good news: the same as in D7! ● hook_module_implements_alter() exists also in D8
  14. 14. Overwrite views plugins: D7 ● It is a PHP class: views_plugin_pager_full ● Extend the class: class A extends B ● Views full pager plugin.
  15. 15. ● hook_views_plugins() ● hook_views_plugins_alter() How is the B class instantiated in the current implementation?
  16. 16. Overwrite views handlers: D7 ● How are they created? ● The same as plugins are, in _views_create_handler()
  17. 17. ● 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()
  18. 18. 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!
  19. 19. Put the original class into a new file and change only the name of the class Add the file to the files array in the .info file: Extend the “new” original class:
  20. 20. ● What happens when the module updates? ● Manually update the class. ● Goal 99% achieved: did not change any implementation, only the class name. ● Can be used with any classes in D7.
  21. 21. 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()
  22. 22. 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.
  23. 23. 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”.
  24. 24. Overwriting code in Drupal Thank you! Questions?

×