nodeformcols.module 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. define('NODEFORMCOLS_DEFAULT_REGION', 'main');
  3. /**
  4. * Implementation of hook_theme().
  5. */
  6. function nodeformcols_theme($aExisting) {
  7. return array(
  8. // This needs to run after node.module's hook_theme(), which we ensure
  9. // by setting this module's weight to 1 during install.
  10. 'node_form' => array(
  11. 'render element' => 'form',
  12. 'template' => 'node-form',
  13. ),
  14. 'nodeformcols_configuration' => array(
  15. 'render element' => 'element',
  16. 'template' => 'nodeformcols-configuration',
  17. ),
  18. );
  19. }
  20. /**
  21. * Implementation of hook_menu().
  22. */
  23. function nodeformcols_menu() {
  24. $items = array();
  25. if (!defined('MAINTENANCE_MODE')) {
  26. $items['admin/structure/types/manage/%node_type/form'] = array(
  27. 'title' => 'Manage form',
  28. 'page callback' => 'drupal_get_form',
  29. 'page arguments' => array('nodeformcols_configuration_form', 4, 6),
  30. 'access arguments' => array('administer content types'),
  31. 'file' => 'nodeformcols.admin.inc',
  32. 'type' => MENU_LOCAL_TASK,
  33. 'weight' => 3,
  34. );
  35. }
  36. return $items;
  37. }
  38. /**
  39. * Implementation of hook_ctools_plugin_directory().
  40. */
  41. function nodeformcols_ctools_plugin_directory($module, $plugin) {
  42. if ($module == 'ctools') {
  43. return 'plugins/' . $plugin;
  44. }
  45. }
  46. function nodeformcols_form_regions() {
  47. return array(
  48. 'main' => t('Main column'),
  49. 'right' => t('Right'),
  50. 'footer' => t('Footer'),
  51. );
  52. }
  53. /**
  54. * Gets default placements for standard fields
  55. *
  56. * @return array
  57. */
  58. function _nodeformscols_default_field_placements() {
  59. return array(
  60. 'title' => array('region' => 'main'),
  61. 'additional_settings' => array('region' => 'main'),
  62. 'actions' => array('region' => NODEFORMCOLS_DEFAULT_REGION, 'weight' => 100),
  63. );
  64. }
  65. function nodeformscols_field_placements($content_type, $variant) {
  66. $default = _nodeformscols_default_field_placements();
  67. if ($variant != 'default') {
  68. $default = variable_get('nodeformscols_field_placements_' .
  69. $content_type . '_default', $default);
  70. }
  71. $placements = variable_get('nodeformscols_field_placements_' . $content_type .
  72. '_' . $variant, $default);
  73. return $placements;
  74. }
  75. /**
  76. * Implementation of hook_form_alter().
  77. */
  78. function nodeformcols_form_alter(&$form, $form_state, $form_id) {
  79. if (isset($form['#node_edit_form']) && $form['#node_edit_form']) {
  80. drupal_alter('nodeformcols_pre_form', $form);
  81. $variant = isset($form['#nodeformcols_variant']) ? $form['#nodeformcols_variant'] : 'default';
  82. $placements = nodeformscols_field_placements($form['#node']->type, $variant);
  83. foreach ($placements as $key => $p) {
  84. if (isset($p['hidden']) && $p['hidden']) {
  85. $form[$key]['#access'] = FALSE;
  86. }
  87. }
  88. drupal_alter('nodeformcols_post_form', $form);
  89. }
  90. }
  91. /**
  92. * Preprocess function to run ahead of other modules.
  93. */
  94. function template_preprocess_node_form(&$aVars) {
  95. drupal_add_css(drupal_get_path('module', 'nodeformcols') . '/css/nodeformcols.css');
  96. $default_region = variable_get('nodeformcols_default_region', NODEFORMCOLS_DEFAULT_REGION);
  97. $form = &$aVars['form'];
  98. $class = array('node-form', 'clearfix');
  99. $regions = array();
  100. $has_elements = array();
  101. $weight = 0;
  102. foreach (nodeformcols_form_regions() as $name => $title) {
  103. $regions[$name] = array(
  104. '#prefix' => '<div class="form-region-' . $name . '">',
  105. '#suffix' => '</div>',
  106. '#weight' => $weight,
  107. );
  108. $weight++;
  109. }
  110. drupal_alter('nodeformcols_pre_placement', $form);
  111. $variant = isset($form['#nodeformcols_variant']) ? $form['#nodeformcols_variant'] : 'default';
  112. $placements = nodeformscols_field_placements($form['#node']->type, $variant);
  113. // Track if new fields should be adjusted above the buttons.
  114. // TODO: This should be generalized to a way to tell nodeformcols where to place new fields (above below field X).
  115. $adjust_to_buttons = isset($placements['buttons']['region']) && ($placements['buttons']['region'] == $default_region);
  116. foreach (element_children($form) as $key) {
  117. $field = $form[$key];
  118. if (isset($field['#type']) && in_array($field['#type'], array('value', 'hidden', 'token')) ||
  119. (isset($field['#access']) && $field['#access'] == FALSE)) {
  120. continue;
  121. }
  122. if (isset($placements[$key])) {
  123. $p = $placements[$key];
  124. if (isset($p['weight'])) {
  125. $field['#weight'] = $p['weight'];
  126. }
  127. if (isset($p['collapsed']) && isset($field['#collapsible']) && $field['#collapsible']) {
  128. $field['#collapsed'] = $p['collapsed'];
  129. }
  130. $regions[$p['region']][$key] = $field;
  131. $has_elements[$p['region']] = TRUE;
  132. unset($form[$key]);
  133. }
  134. else { // Set the default placement for unknown fields
  135. $regions[$default_region][$key] = $field;
  136. if ($adjust_to_buttons && $regions[$default_region][$key]['#weight'] >= $placements['buttons']['weight']) {
  137. $regions[$default_region][$key]['#weight'] = $placements['buttons']['weight'] - .1;
  138. }
  139. $has_elements[$default_region] = TRUE;
  140. unset($form[$key]);
  141. }
  142. }
  143. foreach ($has_elements as $name => $has) {
  144. if ($has) {
  145. $class[] = 'node-form-has-region-' . $name;
  146. $form['nodeformcols_region_' . $name] = $regions[$name];
  147. }
  148. }
  149. $aVars['class'] = join($class, ' ');
  150. }
  151. /**
  152. * Implementation of hook_node_type_delete().
  153. */
  154. function nodeformcols_node_type_delete($info) {
  155. $result = db_select('variable')
  156. ->condition('name', 'nodeformscols_field_placements_' . $info->old_type . '%', 'LIKE')
  157. ->fields('variable', array('name'))
  158. ->execute();
  159. foreach ($result as $row) {
  160. variable_del($row->name);
  161. }
  162. }
  163. /**
  164. * Implementation of hook_node_type_update().
  165. */
  166. function nodeformcols_node_type_update($info) {
  167. if (!empty($info->old_type) && $info->old_type != $info->type) {
  168. $base = 'nodeformscols_field_placements_' . $info->old_type;
  169. $new_base = 'nodeformscols_field_placements_' . $info->type;
  170. $result = db_select('variable')
  171. ->condition('name', $base . '%', 'LIKE')
  172. ->fields('variable', array('name'))
  173. ->execute();
  174. foreach ($result as $row) {
  175. $value = variable_get($row->name, NULL);
  176. $new_name = str_replace($base, $new_base, $row->name);
  177. variable_set($new_name, $value);
  178. variable_del($row->name);
  179. }
  180. }
  181. }