drupaldrupal-7

Getting the current module name


Is there a way to get the name of the module you are working within? I have a large set of modules (about 35) with some common functionality. Long story short is that I would like to be able to get the module name without hard-coding it in a string. Hopefully this isn't necessary, but here's an idea of what I'm trying for:

function MYMODULE_mycustom_hook($args) {
    $sCurrModule = 'MYMODULE';

    // Operations using $sCurrModule...
}

Essentially, I can replace 'MYMODULE' with the module name and be done with it, but I'm wondering if there is a way to get that value programmatically. I'm using Drupal 7.

This does not apply to Drupal 8.


Solution

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

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

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