Getting the current module name
Asked Answered
P

4

12

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.

Pulverulent answered 20/5, 2013 at 20:34 Comment(0)
A
24

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');
Airframe answered 20/5, 2013 at 20:47 Comment(5)
CAVEAT: this does not work if the code is outside the .module file which is almost always the case for any non-trivial functionality.Crosslet
I don't understand why this answer accepted either: Regarding D8 .module file is often used.Aboveboard
if you look at the tags, you'll notice this is for Drupal 7. It's marked as the accepted answer because it solved my problem.Pulverulent
CAVEAT: the $module_name variable is not namespaced to a specific class or module, so you may get unexpected results if you are using this across multiple modules.Crosslet
Fair observation; for my implementation I never needed it outside of a specific function, but others who might just drop that into the top of the file could be in trouble.Pulverulent
S
9

Although OP was asking regarding D7, here's the solution for Drupal 8 (D8) as well:

/** @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, you can chain these calls if necessary:

\Drupal::service('module_handler')->getModule(basename(__FILE__, '.module'))->getName()
Sigismondo answered 22/8, 2019 at 9:9 Comment(4)
Thank you for this, but the question was not regarding Drupal 8.Pulverulent
@Lawrence Johnson As you probably see, the very first sentence of my answer was acknowledging the fact you pointed out: "Although OP was asking regarding D7 [...]". I hope you don't mind that I tried to share a piece of experience hopefully useful for some others.Sigismondo
I think it's great, and I upvoted the answer as well. I don't think it's great, however, to retag my question as Drupal 8.Pulverulent
As @Crosslet correctly points out his solution works only within rare circumstances when this code ran in a *.module file.Sigismondo
T
0

As correctly said in comments

 basename(__FILE__, '.module');

only works inside an actual .module file.

Answering to question:

  1. inside your MYMODULE.module

    function  print_current_module_name()  {
      return  basename(__FILE__, '.module');
    } 
    
  2. from everywhere in Drupal >8

    $module_name = print_current_module_name();
    echo "module name is : " . $module_name
    

Not sure this will work on Drupal 7 as well

Theoretician answered 6/1, 2022 at 17:3 Comment(0)
N
0

A bit hacky but still easy and reliable in most cases:

public function getModuleName() { return explode('\\', __CLASS__)[1]; }

Since a third party module class namespace should have Drupal\my_module_name as a prefix, this will return the expected value.

Nick answered 27/3 at 1:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.