0

I have few modules that provide the same functionality in a different way and they would collide if they would be installed simultaneously.

So I want to prevent installation of multiple modules of this "type". I do not want to add hook_requirements into each one of these modules, I'd prefer to have a central logic(in a "central" module, or profile even).

What would be a good approach to this?

  • Sorry, but your question is not clear enough. What can collide? Hooks, configs, types, blocks, ...? In general you can check if a module is already installed in .install file and do a proper install. Or if them share data or functionality, you can define your own API. – Vagner Mar 2 '16 at 19:47
  • It does not matter what collides, simply put I want to prevent installation of module XYZ from an already installed module. Unfortunately there isn't hook that I know of that would allow this. The hook_requirements is called on the installed module so that won't work, the hook_modules_installed is called after so that won't work either. – user21641 Mar 2 '16 at 20:20
0

How about this?

  1. Create a central module that implements hook_module_preinstall
  2. Follow some naming convention for the modules which should not coexist, or simply gather their names in a list
  3. Use your naming convention or the list of module names in your hook to detect installation of one of these modules. Also, implement the logic for counting the number of such modules that are already enabled in the system.
  4. Conditional on one such module arriving as an argument to hook_module_preinstall and the count of existing enabled modules of this kind being greater than 0, throw a custom Exception in order to block the installation of the said module.
  5. Most importantly, add your central module as a dependency to all of your modules that you wanna control in that way.

Very theoretical answer. :-) May it be of help to you!

| improve this answer | |
  • I already looked at that. The issue is that the hook is called after the module is put into the registry in core/lib/Drupal/Core/Extension/ModuleInstaller.php:150 so even if I would throw an exception I would then have to take care of that manually which is a working solution but I'd rather do it properly...somehow. – user21641 Mar 3 '16 at 21:45
0

One solution is to use hook_requirements(). It has to be in an .install file. It can return an array that prevents module installation.

An example. This is for the module t1. The code is in t1.install.

/**
 * Implements hook_requirements().
 */
function t1_requirements($phase) {
  if ($phase == 'install') {
    $requirements['bob_having_a_fit'] = [
      'title' => t('T1'),
      'description' => t('Bob does not like it.'),
      'severity' => REQUIREMENT_ERROR,
    ];
    return $requirements;
  }
}

To test whether a module is installed, you could do something like:

if (\Drupal::service('module_handler')->moduleExists('doom_module')) {
| improve this answer | |
  • It seems like the OP wants an alternative to hook_requirements... – Cesar Moore Sep 24 '18 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy