4

I'm working a on a project that needs to have one of their submodules enable but not both.

I was wondering if I can have an or statement in the .info so the user would not enable both submodules.

The Drupal 7 documentation "Writing module .info files (Drupal 7.x)" does not talk about this.

5

No, Drupal doesn't have any mechanism for that - dependencies cannot be conditional.

If a single module can have a dependency on an arbitrary module, it might suggest that module is trying to do too much, and should be re-thought. Maybe you should create three modules - one for the core functionality, one for integration with the first module dependency, then another for integration with the second module dependency. Then everybody wins, and you can accomplish it with the functionality that already exists.

Drupal 8 will introduce enhances and enhancedby to module info files to allow them to specify complementary modules, but there's still nothing for conditional dependencies (see https://www.drupal.org/node/328932).

6

The direct answer to your question (can you use "OR" in an .info file?) has already been provided, but the actual functional outcome you're looking for can be achieved with hook_requirements. For instance, you might use this code in my_module.install:

function my_module_requirements($phase) {

  $requirements = array();

  if ($phase == 'install') {
      if (module_exists('submodule1') && module_exists('submodule2')) {
        $requirements['too_many_modules'] = array(
          'title' => $t('Too many modules'),
          'description' => t('You may not enable submodule1 and submodule2 simultaneously'),
          'value' => 'Too many modules',
          'severity' => REQUIREMENT_ERROR,
        );
      }
      if (!module_exists('submodule1') && !module_exists('submodule2')) {
        $requirements['not_enough_modules'] = array(
          'title' => $t('Not enough modules'),
          'description' => t('You must enable submodule1 or submodule2 (but not both)'),
          'value' => 'Not enough modules',
          'severity' => REQUIREMENT_ERROR,
        );
      }
    }

  return $requirements;
}

Further reading

1
  • 2
    Good answer. I seem to remember being chewed out by one of the core maintainers a couple of years ago for suggesting this method, though, so be warned :) – Clive Feb 24 '15 at 15:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.