overwriting code in drupal
DESCRIPTION
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.TRANSCRIPT
![Page 1: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/1.jpg)
Overwriting code in Drupal
Vasile CHINDRIS
vasi1186 d.o
![Page 2: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/2.jpg)
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!
![Page 3: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/3.jpg)
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!
![Page 4: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/4.jpg)
Overwriting menu items: D7
hook_menu_alter(&$items)
![Page 5: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/5.jpg)
![Page 6: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/6.jpg)
![Page 7: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/7.jpg)
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.
![Page 8: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/8.jpg)
The event subscriber is declared in the utils.services.yml file
![Page 9: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/9.jpg)
![Page 10: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/10.jpg)
In lib\Drupal\utils\Controller\CustomNodeController.php:
![Page 11: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/11.jpg)
![Page 12: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/12.jpg)
Overwriting menu items: real use case
We have a hook_menu():
![Page 13: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/13.jpg)
When using a file that is uploaded with ajax.
Why?
![Page 14: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/14.jpg)
![Page 15: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/15.jpg)
Overwrite existing hooks: D7
hook_module_implements_alter(&$implementations, $hook)
![Page 16: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/16.jpg)
Use case: remove the hook_user_view() from the user module.
![Page 17: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/17.jpg)
![Page 18: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/18.jpg)
![Page 19: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/19.jpg)
A real use case
You have a module called “action” on your site.
![Page 20: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/20.jpg)
![Page 21: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/21.jpg)
Overwriting existing hooks: D8
● Good news: the same as in D7!
● hook_module_implements_alter() exists also in D8
![Page 22: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/22.jpg)
Overwrite views plugins: D7
● It is a PHP class: views_plugin_pager_full
● Extend the class: class A extends B
● Views full pager plugin.
![Page 23: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/23.jpg)
● hook_views_plugins()
● hook_views_plugins_alter()
How is the B class instantiated in the current implementation?
![Page 24: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/24.jpg)
![Page 25: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/25.jpg)
Overwrite views handlers: D7
● How are they created?
● The same as plugins are, in _views_create_handler()
![Page 26: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/26.jpg)
● 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()
![Page 27: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/27.jpg)
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!
![Page 28: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/28.jpg)
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:
![Page 29: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/29.jpg)
● 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.
![Page 30: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/30.jpg)
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()
![Page 31: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/31.jpg)
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.
![Page 32: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/32.jpg)
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”.
![Page 33: Overwriting code in Drupal](https://reader034.vdocuments.site/reader034/viewer/2022042513/554bb5a9b4c9053a298b4858/html5/thumbnails/33.jpg)
Overwriting code in Drupal
Thank you!
Questions?