From 438d00be370561f25799e7f46fee7073d2b27ad6 Mon Sep 17 00:00:00 2001
From: capynet <capy.net@gmail.com>
Date: Mon, 24 Mar 2014 05:27:57 +0100
Subject: [PATCH] #1473760 Use data-* to check modules dependencies before
 submit

---
 .../lib/Drupal/system/Form/ModulesListForm.php     |    5 ++
 core/modules/system/system.libraries.yml           |    1 +
 core/modules/system/system.modules.js              |   87 ++++++++++++++++++++
 3 files changed, 93 insertions(+)

diff --git a/core/modules/system/lib/Drupal/system/Form/ModulesListForm.php b/core/modules/system/lib/Drupal/system/Form/ModulesListForm.php
index 7b5d257..649bf06 100644
--- a/core/modules/system/lib/Drupal/system/Form/ModulesListForm.php
+++ b/core/modules/system/lib/Drupal/system/Form/ModulesListForm.php
@@ -141,6 +141,7 @@ public function buildForm(array $form, array &$form_state) {
       }
     }
 
+
     // Add a wrapper around every package.
     foreach (element_children($form['modules']) as $package) {
       $form['modules'][$package] += array(
@@ -251,6 +252,10 @@ protected function buildRow(array $modules, Extension $module, $distribution) {
       '#title' => $this->t('Install'),
       '#default_value' => (bool) $module->status,
       '#disabled' => (bool) $module->status,
+      '#attributes' => array(
+        'data-module' => $module->name,
+        'data-requires' => implode(',', array_keys($module->requires)),
+      ),
     );
 
     // Disable the checkbox for required modules.
diff --git a/core/modules/system/system.libraries.yml b/core/modules/system/system.libraries.yml
index 61eb1b4..c0bc5af 100644
--- a/core/modules/system/system.libraries.yml
+++ b/core/modules/system/system.libraries.yml
@@ -44,3 +44,4 @@ drupal.system.modules:
     - core/jquery
     - core/drupal
     - core/jquery.once
+    - core/drupal.dialog
diff --git a/core/modules/system/system.modules.js b/core/modules/system/system.modules.js
index 5ccad36..4f0c142 100644
--- a/core/modules/system/system.modules.js
+++ b/core/modules/system/system.modules.js
@@ -57,4 +57,91 @@
     }
   };
 
+  /**
+   * Display an alert for dependant modules.
+   */
+  Drupal.behaviors.moduleDependencies = {
+    attach: function (context, settings) {
+      var self = this;
+      self.gettingDependencies = false;
+
+      $('input[data-requires!=""]').once('module-dependency').on('click', function () {
+
+        var $that = $(this);
+        var module = $that.attr('data-module');
+        var singularMsgDeps = 'You must enable the @required module to install @module.';
+        var pluralMsgDeps = 'You must enable the @required modules to install @module.';
+        var messageFooter = Drupal.t('Would you like to continue with the above?');
+        var dialogTitle = 'Some required modules must be enabled';
+
+        var confirmHandler = function () {
+          $(this).dialog("close");
+          self.gettingDependencies = true;
+          $dependencies.click();
+          self.gettingDependencies = false;
+        };
+
+        var cancelHandler = function () {
+          $(this).dialog("close");
+          $that.attr('checked', false);
+        };
+
+        if ($that.prop('checked') && !self.gettingDependencies) {
+          var dependenciesList = $that.attr('data-requires').split(',');
+          var $dependencies = self.getDeps(dependenciesList);
+
+          if ($dependencies.length) {
+
+            var message = Drupal.formatPlural($dependencies.length, singularMsgDeps, pluralMsgDeps,
+              {'@required': dependenciesList.join(', '), '@module': module}
+            );
+
+            Drupal.dialog('<p>' + message + '<br/><br/>' + messageFooter + '</p>', {
+              title: Drupal.t(dialogTitle),
+              dialogClass: 'module-mark-dependencies-modal',
+              resizable: false,
+              minWidth: 340,
+              buttons: [
+                {
+                  text: Drupal.t('Confirm'),
+                  click: confirmHandler,
+                  'class': 'button-primary button'
+                },
+                {
+                  text: Drupal.t('Cancel'),
+                  click: cancelHandler,
+                  'class': 'button'
+                }
+              ],
+              beforeClose: function (event) {
+                if (event.keyCode === $.ui.keyCode.ESCAPE) {
+                  $that.attr('checked', false);
+                }
+              }
+            }).showModal();
+
+          }
+        }
+      });
+
+      /**
+       * Returns a jQuery collection of unchecked inputs from the dependency list
+       *
+       * @param {Array} list
+       *   Array of ids
+       */
+      self.getDeps = function (list) {
+        return $('input[data-module]').filter(function () {
+          var $that = $(this);
+          var required = $.inArray($that.attr('data-module'), list) !== -1;
+          var notEnabled = true;
+          if (required) {
+            notEnabled = !$that.is(':checked');
+          }
+          return required && notEnabled;
+        });
+      };
+    }
+  };
+
 }(jQuery, Drupal));
-- 
1.7.10.4

