Overwriting code in Drupal allows customizing existing functionality without directly modifying core files. This can be done by altering hooks, extending classes, or overriding routes and services. In Drupal 7, menu items can be overwritten by hook_menu_alter(), views plugins with hook_views_plugins_alter(), and handlers by altering the file registry with hook_registry_files_alter(). In Drupal 8, menu items are altered by modifying routing files, and views plugins and handlers can be customized via alter hooks or changing the cache_views_info table. Overwriting should aim to extend rather than directly copy core code due to upgrade issues.
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. 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!
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.
25. Overwrite views handlers: D7
● How are they created?
● The same as plugins are, in _views_create_handler()
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()
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!
28. 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:
29. ● 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.
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()
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.
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”.