Getting the current module name

Is there a way to get the name of the module you are working in? I have a large set of modules (about 35) with some common features. In short, I would like to be able to get the module name without hard coding it in a string. Hopefully this is not necessary, but here is the idea of ​​what I'm trying to do:

function MYMODULE_mycustom_hook($args) { $sCurrModule = 'MYMODULE'; // Operations using $sCurrModule... } 

Essentially, I can replace "MYMODULE" with the module name and end it, but I wonder if there is a way to get this value programmatically. I am using Drupal 7.

This does not apply to Drupal 8.

+9
source share
2 answers

If your module file is sites/default/modules/MYMODULE/MYMODULE.module , then the module name is MYMODULE.

You can get it programmatically in the MYMODULE.module file using the following command:

 $module_name = basename(__FILE__, '.module'); 
+21
source

Although the OP asked about D7, there is also a solution for Drupal 8 (D8) here:

 /** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */ $module_handler = \Drupal::service('module_handler'); /** @var \Drupal\Core\Extension\Extension $module_object */ $module_object = $module_handler->getModule(basename(__FILE__, '.module')); $module_name = $module_object->getName(); 

Of course, if necessary, you can link these calls:

 \Drupal::service('module_handler')->getModule(basename(__FILE__, '.module'))->getName() 
+1
source

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


All Articles