modules_weight.module 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * @file
  4. * Modules weight functionlity implementation.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function modules_weight_menu() {
  10. $items = array();
  11. $items['admin/config/system/modules-weight'] = array(
  12. 'title' => 'Modules Weight',
  13. 'description' => 'Provide admin interface to order the modules execution.',
  14. 'type' => MENU_NORMAL_ITEM,
  15. 'page callback' => 'drupal_get_form',
  16. 'page arguments' => array('modules_weight_admin_config_page_form'),
  17. 'access arguments' => array('administer site configuration'),
  18. );
  19. $items['admin/config/system/modules-weight/default'] = array(
  20. 'title' => 'Modules Weight',
  21. 'description' => 'jQuery twitter search block config',
  22. 'type' => MENU_DEFAULT_LOCAL_TASK,
  23. 'weight' => 2,
  24. );
  25. $items['admin/config/system/modules-weight/configration'] = array(
  26. 'title' => 'Modules Weight configurations',
  27. 'description' => 'Configure Modules Weight.',
  28. 'page callback' => 'drupal_get_form',
  29. 'page arguments' => array('modules_weight_configuration_form'),
  30. 'access arguments' => array('administer system'),
  31. 'type' => MENU_LOCAL_TASK,
  32. 'weight' => 2,
  33. );
  34. return $items;
  35. }
  36. function modules_weight_admin_config_page_form($form, &$form_state) {
  37. $form['modules_weight']['#tree'] = TRUE;
  38. $result = db_select('system', 's')
  39. ->condition('s.type', 'module')
  40. ->condition('s.status', 1)
  41. ->fields('s', array('weight', 'info', 'name'))
  42. ->orderBy('weight', 'ASC')
  43. ->execute();
  44. $show_system_module = variable_get('show_system_modules', 0);
  45. foreach ($result as $module) {
  46. $info = unserialize($module->info);
  47. if ($info['package'] != 'Core' || $show_system_module) {
  48. $delta = modules_weight_prepare_delta($module->weight);
  49. $form['modules_weight'][$module->name] = array(
  50. 'name' => array(
  51. '#markup' => t($info['name']),
  52. ),
  53. 'description' => array(
  54. '#markup' => t($info['description']),
  55. ),
  56. 'weight' => array(
  57. '#type' => 'weight',
  58. '#title' => t('Weight'),
  59. '#default_value' => $module->weight,
  60. '#delta' => $delta,
  61. '#title-display' => 'invisible',
  62. ),
  63. 'package' => array(
  64. '#markup' => t($info['package']),
  65. ),
  66. 'old_weight_value' => array(
  67. '#type' => 'hidden',
  68. '#value' => $module->weight,
  69. ),
  70. );
  71. }
  72. }
  73. $form['actions'] = array('#type' => 'actions');
  74. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save Changes'));
  75. return $form;
  76. }
  77. /**
  78. * Implements hook_theme().
  79. */
  80. function modules_weight_theme() {
  81. return array(
  82. 'modules_weight_admin_config_page_form' => array(
  83. 'render element' => 'form',
  84. ),
  85. );
  86. }
  87. function theme_modules_weight_admin_config_page_form($variables) {
  88. $form = $variables['form'];
  89. $rows = array();
  90. foreach (element_children($form['modules_weight']) as $id) {
  91. $form['modules_weight'][$id]['weight']['#attributes']['class'] = array('module-weight');
  92. $rows[] = array(
  93. 'data' => array(
  94. drupal_render($form['modules_weight'][$id]['name']),
  95. drupal_render($form['modules_weight'][$id]['description']),
  96. drupal_render($form['modules_weight'][$id]['weight']),
  97. drupal_render($form['modules_weight'][$id]['package']),
  98. ),
  99. 'class' => array('draggable'),
  100. );
  101. }
  102. $header = array(t('Name'), t('Description'), t('Weight'), t('Package'));
  103. $table_id = 'module-items-table';
  104. $output = theme('table', array(
  105. 'header' => $header,
  106. 'rows' => $rows,
  107. 'attributes' => array('id' => $table_id),
  108. ));
  109. $output .= drupal_render_children($form);
  110. //Remove tabledrage functionlity due to issue related to re-weight all module
  111. // for more info : https://www.drupal.org/node/2205787
  112. //drupal_add_tabledrag($table_id, 'order', 'self', 'module-weight');
  113. return $output;
  114. }
  115. /**
  116. * Submit callback for the modules_weight_admin_config_page_form form.
  117. *
  118. * Updates the 'weight' column for each module in our table, taking into
  119. * account that item's new order after the drag and drop actions have been
  120. * performed.
  121. */
  122. function modules_weight_admin_config_page_form_submit($form, $form_state) {
  123. foreach ($form_state['values']['modules_weight'] as $name => $weight) {
  124. if($weight['weight'] != $weight['old_weight_value']) {
  125. db_query(
  126. "UPDATE {system} SET weight = :weight WHERE name = :name",
  127. array(':weight' => $weight['weight'], ':name' => $name)
  128. );
  129. }
  130. }
  131. }
  132. function modules_weight_configuration_form() {
  133. $form = array();
  134. $form['show_system_modules'] = array(
  135. '#type' => 'checkbox',
  136. '#title' => t('Show system modules'),
  137. '#return_value' => 1,
  138. '#default_value' => variable_get('show_system_modules', 0),
  139. );
  140. $form['notice'] = array(
  141. '#markup' => '<strong>' . t("cautions: This module just display non-core module by, if you check this option it will cause unexpected behavior in system, (USE IT ON YOUR OWN RISCK) that's because displaying core module in the configuration form will reorder the system core modules execution even if you didn't change them and as you might notice all core modules has 0 weight value by default.") . '',
  142. );
  143. return system_settings_form($form);
  144. }
  145. /**
  146. * Prepares the delta for the weight field on the administration form.
  147. * If a module has a weight higher then 100 (or lower than 100), it will use that
  148. * value as delta and the '#weight' field will turn into a textfield most likely
  149. *
  150. * @param $weight
  151. * @return int
  152. */
  153. function modules_weight_prepare_delta($weight) {
  154. $delta = 100;
  155. if ((int) $weight > $delta) {
  156. return (int) $weight;
  157. }
  158. if ((int) $weight < -100) {
  159. return (int) $weight * -1;
  160. }
  161. return $delta;
  162. }