What are the Drupal bindings for a particular module?

What binds Drupal to a particular module?

In Drupal 7, each core module has an "api" file

$ ls modules/*/*.api.php modules/aggregator/aggregator.api.php modules/openid/openid.api.php modules/block/block.api.php modules/overlay/overlay.api.php modules/comment/comment.api.php modules/path/path.api.php modules/contextual/contextual.api.php modules/rdf/rdf.api.php modules/dashboard/dashboard.api.php modules/search/search.api.php modules/field/field.api.php modules/shortcut/shortcut.api.php modules/field_ui/field_ui.api.php modules/simpletest/simpletest.api.php modules/file/file.api.php modules/system/system.api.php modules/filter/filter.api.php modules/system/theme.api.php modules/help/help.api.php modules/taxonomy/taxonomy.api.php modules/image/image.api.php modules/trigger/trigger.api.php modules/locale/locale.api.php modules/update/update.api.php modules/menu/menu.api.php modules/user/user.api.php modules/node/node.api.php 

Each of these files contains a function that never called (?), But documents the existence of a hook that other modules (including third-party) can implement.

 File: modules/path/path.api.php function hook_path_delete($path) { db_delete('mytable') ->condition('pid', $path['pid']) ->execute(); } 

My question is: what links a particular hook to a specific module? Why is path.api.php included in path_delete ? Why is system.api.php included in the entity_view ? Is it just arbitrary after organizing the facts, or is there something in the Drupal system that binds a particular hook to a specific module? Or something else?

+4
source share
2 answers

Hooks are called using module_invoke() and module_invoke_all() : if you look at the code for these two functions, you can build how this works, but basically, if I add this to my module code:

 // Other code $foo = module_invoke_all('foo_bar', $var1, $var2); // More code 

Drupal will call every implementation of hook_foo_bar($var1, $var2) that it finds in the allowed modules. Based on this, you should see that the only thing that binds a particular hook to a particular module is a naming convention: if I call my module foo , my hook functions should start with hook_foo_ .

You don't know anything about nothing being called in *.api.php : since a module call is just a function call, the authors of the module include foo.api.php for documentation purposes only, to tell developers how to implement the hook.

For example, in the above case, foo.api.php will contain an example function, for example:

 /** * Doxygen comments documenting the function goes here */ function hook_foo_bar($var1, $var2) { return $var1 + $var2; } 

But as a module developer, I could implement hook_foo_bar() another way:

 function mymodule_foo_bar($var1, $var2) { return $var1 - $var2; } 

And when module_invoke_all() is called, Drupal creates a function using the short name of the implementation module ( mymodule ) and the hook name passed to module_invoke_all() ( foo_bar ), thereby calling the function mymodule_foo_bar() I just defined.

The system module in the kernel is a bit of everything: one task for Drupal 8 is to kill it and delegate its functionality to other modules.

+4
source


maybe you can try to learn how to connect to drupal? it is easy:
http://api.drupal.org/api/drupal/includes--module.inc/group/hooks/7

About hook_path_delete:
Look at path.module, you will see path_delete (...) calls somewhere.
For example, in path_node_update () - this function is called, then you change the current path - first delete the old path, than create a new path for the node.
Now it looks where the path_delete () function is defined - it is placed in the path.inc file:
In this function you will see: module_invoke_all ('path_delete', $ path); - What does this function do?
It lists all the modules (in the drupal 7 file that it caches) where hook_path_delete is defined (as I showed earlier, for the user module yiu it is defined as YOURMODULENAME_path_delete, and it will also be enabled here) and run all these functions one by one ( the order of work is determined by the weight and file name of the modules).
So what can you do in a user module? You can respond to this deletion reaction and perform some other actions - for example, delete another path that can be used to duplicate the path of this node (this is just an example).

ps A good point to run custom modules: http://drupal.org/contributors-guide

0
source

Source: https://habr.com/ru/post/1339770/


All Articles