2

I want to know how to add conflicts in D8 info.yml.

I tried like this in D8 .info.yml:

name: Example
type: module
description: some example.
core: 8.x
package: Other
dependencies:
  - menu_ui
conflicts:
  - module1
  - module2

and in .install file

<?php
/**
 * Implements hook_requirements().
 */
function example_requirements($phase) {
  $requirements = [];

  switch ($phase) {
    // Called while the module is installed.
    case 'install':
    if (\Drupal::moduleHandler()->moduleExists('module1')) {
        $requirements[] = [
          'title' => t('Module 1'),
          'value' => t('Module 1'),
          'description' => t('Module 1 conflicts with example.'),
          'severity' => REQUIREMENT_ERROR,
        ];
      }
      break;
  }

  return $requirements;
}

But in D7 it is working:

name = Example
description = some example.
core = 7.x
dependencies[] = menu
package = Other
conflicts[] = module1
conflicts[] = module2

While enabling my example module it is not showing any conflict error in D8.

Installation Order:

1) Fresh Drupal installation.

2) Module 1

3) Module 2

4) Example module(here i need to show module1 and 2 are present while installing)

2
  • It seems that this is not yet implemented. See this issue. Commented Aug 8, 2016 at 10:08
  • ty for your comment, it saved my time.
    – Ajay Reddy
    Commented Aug 8, 2016 at 10:22

1 Answer 1

3

It seems that this is not implemented yet. Related issue is here.

An alternative would be to use hook_requirements() in your module's .install file. You can check here if the conflicting module is present during the installation process and prevent the system from installing the module.

Here's example code that would go in your module's .install file:

/**
 * Implements hook_requirements().
 */
function YOUR_MODULE_requirements($phase) {
  $requirements = [];

  switch ($phase) {
    // Called while the module is installed.
    case 'install':
    // Called during regular use of the website.
    case 'runtime':
    // Called while the update.php is being executed.
    case 'update':
      // Do not allow administrators to install the module if conflicting module
      // is already installed.
      //
      // You can move this code to any other phase as well.
      if (\Drupal::moduleHandler()->moduleExists('ARCH_ENEMY_OF_YOUR_MODULE')) {
        $requirements[] = [
          'title' => t('Name of your module'),
          'value' => t('Arch enemy module'),
          'description' => t('Explain that administrators need to uninstall the module before proceeding.'),
          'severity' => REQUIREMENT_ERROR,
        ];
      }
      break;
  }

  return $requirements;
}

You can see a working example of hook_requirements() in my Drupal 8 boilerplate repo on Github.

Hope this helps.

9
  • No luck, still not throwing any error while installing even though my module1 and 2 are installed.
    – Ajay Reddy
    Commented Aug 8, 2016 at 10:30
  • Will have a look at it shortly Commented Aug 8, 2016 at 11:51
  • Just tested this - it's working properly and prevents you from installing the module if the conflicting module is already installed. Post your code if it doesn't work for you. Commented Aug 8, 2016 at 13:17
  • <?php /** * Implements hook_requirements(). */ function example_requirements($phase) { $requirements = []; switch ($phase) { // Called while the module is installed. case 'install': if (\Drupal::moduleHandler()->moduleExists('module1')) { $requirements[] = [ 'title' => t('Module 1'), 'value' => t('Module 1'), 'description' => t('Module 1 conflicts with example.'), 'severity' => REQUIREMENT_ERROR, ]; } break; } return $requirements; }
    – Ajay Reddy
    Commented Aug 8, 2016 at 13:19
  • Code is not formatted properly in comments so it's better to post it to the question itself. However your code seems to be fine - are you sure that the machine name of the other module is module1? Commented Aug 8, 2016 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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