Posted By

sjkhan on 07/23/13


Tagged


Versions (?)

Modules_weight


 / Published in: PHP
 

Modules Weight - updates modules weight.

  1. <?php
  2.  
  3.  
  4. /**
  5. * Implementation of hook_permission().
  6. */
  7. function modules_weight_permission() {
  8. return array(
  9. 'access modules weight' => array(
  10. 'title' => t('Access Module Weights'),
  11. 'description' => t('Access the Module Weights admin section.'),
  12. ),
  13. );
  14. }
  15.  
  16. /**
  17. * Implementation of hook_menu().
  18. */
  19. function modules_weight_menu(){
  20. $items=Array();
  21.  
  22. $items['admin/config/system/module_weight']=Array(
  23. 'title' => t('Module Weights'),
  24. 'description' => '',
  25. 'page callback' => 'drupal_get_form',
  26. 'page arguments' => array('modules_weight_form'),
  27. 'access arguments' => array('access modules weight'),
  28. );
  29.  
  30. return $items;
  31. }
  32.  
  33. /**
  34. * Implementation of hook_theme().
  35. */
  36. function modules_weight_theme($existing, $type, $theme, $path) {
  37. return array(
  38. 'modules_weight_form' => array(
  39. 'arguments' => array('form' => NULL),
  40. 'render element' => 'form',
  41. ),
  42. );
  43. }
  44.  
  45. function modules_weight_form($form_state){
  46. $modules=Array();
  47. foreach(module_list(true) as $module){
  48. if($module=='standard') continue;
  49. $res=db_query("SELECT `weight`,`info` FROM {system} WHERE `type`=:type AND `name`=:name LIMIT 1",Array(':type'=>'module',':name'=>$module));
  50.  
  51. foreach($res as $obj){
  52. $w=$obj->weight;
  53. $infos=unserialize($obj->info);
  54. $name=$infos['name'];
  55. $desc=$infos['description'];
  56.  
  57. $modules[]=Array(
  58. 'machine_name'=>$module,
  59. 'weight'=>$w,
  60. 'name'=>$name,
  61. 'description'=>$desc,
  62. );
  63. }
  64.  
  65. }
  66. $form=Array();
  67.  
  68. foreach($modules as $module){
  69. $form['module_'.$module['machine_name']]=Array(
  70. '#type'=>'textfield',
  71. '#title'=>$module['name'],
  72. '#description'=>$module['description'],
  73. '#default_value'=>$module['weight'],
  74. '#attributes'=>Array('class'=> Array('my-elements-weight')),
  75. );
  76. }
  77.  
  78. $form['submit']=Array(
  79. '#type'=>'submit',
  80. '#value'=>t('Submit'),
  81. );
  82. //$form['#theme'] = 'modules_weight_form';
  83. $form['#submit'][] = 'modules_weight_form_submit';
  84. return $form;
  85. }
  86.  
  87. function theme_modules_weight_form($variables) {
  88. $form=$variables['form'];
  89. drupal_add_tabledrag('modules-weight-table', 'order', 'sibling', 'my-elements-weight');
  90. $header=Array(t('Machine Name'),t('Module'),t('Weight'));
  91. $rows=Array();
  92. foreach($form as $key=>&$elem){
  93. if(strpos($key,"module_")===0){
  94. $cell1=preg_replace("/^module_/","",$key);
  95. $cell2=$elem['#title'].'<br />'.$elem['#description'];
  96. $elem['#title']='';$elem['#description']='';
  97. $cell3="";
  98. $cell3=drupal_render($elem);
  99.  
  100. $row=Array(Array('data'=>$cell1),Array('data'=>$cell2),Array('data'=>$cell3));
  101. $rows[] = array(
  102. 'data' => $row,
  103. 'class' => array('draggable'),
  104. );
  105. }
  106. }
  107.  
  108. //var_dump($form);die();
  109. $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'modules-weight-table')));
  110. $output .= drupal_render_children($form);
  111. return $output;
  112. }
  113.  
  114. function modules_weight_form_submit($form,$form_state){
  115. foreach($form_state['values'] as $key=>$weight){
  116. if(strpos($key,"module_")===0 && is_numeric($weight)){
  117. $modulename=preg_replace("/^module_/","",$key);
  118. if(trim($modulename)=='') continue;
  119. db_update('system')
  120. ->fields(array('weight' => $weight))
  121. ->condition('name', $modulename, '=')
  122. ->execute();
  123. }
  124. }
  125. drupal_flush_all_caches();
  126. }

Report this snippet  

You need to login to post a comment.