How can I extend an existing module without modifying it? Actually its a contributed module, so I don't want to hack it. But I want to alter and add certain features to it. I'm new to drupal and as I read tutorials about it, I keep hearing one thing again and again - Don't hack the core... and I believe the same applies for modules too.
5 Answers
Avoid hacking or forking the module if at all possible. Hacking the module gives you a lesser variety of the "hacking core" pain, and forking the module (without a distinctly superior architecture and feature set.) just muddies the module water further- making it even more difficult for sitebuilders to make an effective decision about what to use.
The best solutions are:
- Use any API provided by the module. Sometimes there are functions you can use as API calls that are not necessarily intended that way.
- Create the additional functionality and submit a patch to the module issue queue, so others may benefit from your work, and so you may benefit from security audits without much sweat.
- Not always, but often you can go far creating a custom module that implements hook_form_alter and adds a submit handler that leads back to your module for processing.
Found a very good post about extending a module in drupal. Essential parts:
If the module uses code for a page callback that you want to change, change the page callback with hook_menu_alter().
If the module implements a theme function you want to alter, change the function associated with hook_theme_registry_alter(). In alternative, if it is sufficient to change the variables the theme function gets, then you can implement the preprocess function for that theme function (e.g. hook_preprocess_rdf_metadata() for theme_rdf_metadata(), and change the variables that theme function will get.
If the module executes an SQL query using db_select(), and assigns a tag to the query, change the executed query with hook_query_alter().
If the module implements a hook you don't want it is executed, you can implement hook_module_implements_alter() to avoid it is executed.
If the module implements an alter hook (e.g. hook_page_alter()), and you want to change what that hook altered, implement the same alter hook, being sure it is executed after the one implemented from that module.
In the case the function you want to alter is not a hook, then:
Check that function is using hooks implemented from other module. For example, node_save() invokes hook_node_presave(); if I would want to change the "changed" property of the node, I don't hack node_save(), but I rather implement hook_node_presave() to alter it.
Check that function is referenced/used from a hook; in that case, you can do something for that hook, as I previously described.
If anything I said until now doesn't apply, then it is better to create a custom module, and use the code of the other module to create it. I would also try asking a feature request for the existing module, hoping the feature is implemented. Hacking a third-party module is never a good idea, especially because automatic updates of the module (via the Update Manager, or Drush) would not anymore possible for that module.
What module is it? Some modules provide APIs that you can extend by writing a module of your own. There are tons of modules that "extend" Views by building off of the Views API. If the module you want to extend doesn't have a good API, you can always fork it to make a version that meets your needs. Maybe it will help someone else too.
To change the form shown by the module, you can simply create a module that implements hook_form_alter()
or hook_form_FORM_ID_alter()
. Most of the times, when you want to modify how a module behave, you also need to change the settings form it uses (or any other form).