The scenario:
- You've written a module (and, even better, it's compatible with J! 4 ) and you've installed it on your brand new J! website.
- You've decided to publish two instances of the module on a web page. (This doesn't happen often that you need the same module to be displayed "twice" but each instance of the module may have different parameters.)
- You want to do something different depending on the instance of the module (without having to create another parameter to differentiate one instance from another). The normal way to identify a module is to get its id.
The usual way to get a module's id is to write the following PHP:
Code: Select all
// no direct access
defined('_JEXEC') or die('Restricted access');
use Joomla\CMS\Factory;
jimport('joomla.application.module.helper');
$module = JModuleHelper::getModule('mod_mymodule');
$id = $module->id
OK, now run the page with two instances of the module and display the value of $id. Interesting, isn't it, that the value is the same in both cases! This was situation was discussed years ago on this forum: viewtopic.php?t=254805. It's also been discussed more recently on GitHub and at StackExchange but I'm really not interested in running AJAX to identify the specific instance.
It would be really nice if there was a method or property that identified the specific instance of the module that's being used at the time. I don't want to have to feed the call to getModule() like this:
getModule(string $name, string $title = null)
because I won't always know the title of the module when I'm using it. I just want to know the "id" of the module's instance and, if there are multiple instances, which particular instance of the module the PHP needs to "work" on. In other words, the module should be agnostic about the title of the module or what template position it uses or may use.
Ideally, it be great if there was a property like this:
Code: Select all
JModuleHelper::getModule('mod_mymodule')->getInstance