nodeformcols.admin.inc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * @file
  4. * Contains the admin functions for Node form columns
  5. */
  6. function _nodeformcols_get_node_type_form($type) {
  7. module_load_include('inc', 'node', 'node.pages');
  8. global $user;
  9. $node = new stdClass();
  10. $node->uid = $user->uid;
  11. $node->name = isset($user->name) ? $user->name : '';
  12. $node->type = $type;
  13. $node->language = '';
  14. $nfid = $type . '_node_form';
  15. $fs = array(
  16. 'build_info' => array('args' => array($node)),
  17. 'input' => array(),
  18. );
  19. $fs += form_state_defaults();
  20. $nf = drupal_retrieve_form($nfid, $fs);
  21. drupal_prepare_form($nfid, $nf, $fs);
  22. drupal_process_form($nfid, $nf, $fs);
  23. return $nf;
  24. }
  25. function nodeformcols_update_placements($type, $variant, &$placements) {
  26. $form = _nodeformcols_get_node_type_form($type);
  27. drupal_alter('nodeformcols_base_form', $form, $variant);
  28. $default_region = variable_get('nodeformcols_default_region', NODEFORMCOLS_DEFAULT_REGION);
  29. foreach (element_children($form) as $key) {
  30. $field = $form[$key];
  31. if (
  32. substr($key, 0, 8) == 'section_' ||
  33. (isset($field['#type']) && in_array($field['#type'], array('value', 'hidden', 'token'))) ||
  34. (isset($field['#type'], $field['#group']) && $field['#type'] === 'fieldset')
  35. ) {
  36. // Remove placements that meet exclusion rules.
  37. if (isset($placements[$key])) {
  38. unset($placements[$key]);
  39. }
  40. continue;
  41. }
  42. // Ensure a weight so that we don't have to worry about it later
  43. $field['#weight'] = isset($field['#weight']) ? $field['#weight'] : 0;
  44. if (!isset($placements[$key])) {
  45. // If the element is unknown we'll add it to
  46. // the field placement list
  47. $placements[$key] = array(
  48. 'region' => $default_region,
  49. 'weight' => $field['#weight'],
  50. );
  51. // Make sure that we don't auto-place the field below the buttons.
  52. if ($placements['actions']['region'] == $default_region && $placements[$key]['weight'] >= $placements['actions']['weight']) {
  53. $placements[$key]['weight'] = $placements['actions']['weight'] - .1;
  54. }
  55. }
  56. elseif (!isset($placements[$key]['weight'])) {
  57. $placements[$key]['weight'] = $field['#weight'];
  58. }
  59. $placements[$key]['has_required'] = _nodeformcols_has_required($field);
  60. // Get the element title
  61. $placements[$key]['title'] = _nodeformcols_get_element_title($field);
  62. if (isset($field['#collapsible']) && $field['#collapsible']) {
  63. if (!isset($placements[$key]['collapsed'])) {
  64. $placements[$key]['collapsed'] = isset($field['#collapsed']) && $field['#collapsed'];
  65. }
  66. }
  67. else {
  68. unset($placements[$key]['collapsed']);
  69. }
  70. }
  71. // Remove fields that have disappeared from
  72. // the placements list.
  73. foreach ($placements as $key => $info) {
  74. if (!isset($form[$key])) {
  75. unset($placements[$key]);
  76. }
  77. }
  78. }
  79. function _nodeformcols_get_element_title($element) {
  80. if (!empty($element['#title'])) {
  81. return $element['#title'];
  82. }
  83. if (isset($element['#type']) && $element['#type'] == 'submit') {
  84. return $element['#value'];
  85. }
  86. if (isset($element['#type']) && $element['#type'] == 'vertical_tabs') {
  87. return t('Vertical tabs');
  88. }
  89. foreach (element_children($element) as $key) {
  90. if ($title = _nodeformcols_get_element_title($element[$key])) {
  91. return $title;
  92. }
  93. }
  94. }
  95. function nodeformcols_configuration_form($form, $form_state, $node_type, $variant = 'default') {
  96. $type = $node_type->type;
  97. $variants = array();
  98. drupal_alter('nodeformcols_variants', $variants, $type);
  99. $form = array();
  100. if (!empty($variants)) {
  101. $variant_links = array(
  102. 'default' => array(
  103. 'title' => t('Default'),
  104. 'href' => 'admin/structure/types/manage/' . $type . '/form',
  105. ),
  106. );
  107. foreach ($variants as $id => $title) {
  108. $variant_links[] = array(
  109. 'title' => $title,
  110. 'href' => 'admin/structure/types/manage/' . $type . '/form/' . $id,
  111. );
  112. }
  113. $form['variant'] = array(
  114. '#type' => 'item',
  115. '#title' => t('Select a form variant'),
  116. '#value' => theme('links', array('links' => $variant_links)),
  117. );
  118. }
  119. if (empty($variant)) {
  120. $variant = 'default';
  121. }
  122. $form['#variant'] = $variant;
  123. $placements = nodeformscols_field_placements($type, $variant);
  124. nodeformcols_update_placements($type, $variant, $placements);
  125. $regions = nodeformcols_form_regions();
  126. $form['type'] = array(
  127. '#type' => 'value',
  128. '#value' => $type,
  129. );
  130. $form['#after_build'][] = '_nodeformcols_configuration_form_after_build';
  131. $form['conf'] = array(
  132. '#theme' => array('nodeformcols_configuration'),
  133. );
  134. foreach ($placements as $name => $info) {
  135. $key = 'field_' . $name;
  136. $weight = isset($info['weight']) ? $info['weight'] : 0;
  137. $form['conf'][$info['region']][$key] = array(
  138. '#weight' => $weight,
  139. $key . '_name' => array(
  140. '#markup' => !empty($info['title']) ? $info['title'] : $name,
  141. ),
  142. $key . '_region' => array(
  143. '#type' => 'select',
  144. '#options' => $regions,
  145. '#default_value' => $info['region'],
  146. '#attributes' => array(
  147. 'class' => array('field-region-select field-region-' . $info['region']),
  148. ),
  149. ),
  150. $key . '_weight' => array(
  151. '#type' => 'textfield',
  152. '#default_value' => $weight,
  153. '#size' => 3,
  154. '#attributes' => array(
  155. 'class' => array('field-weight field-weight-' . $info['region']),
  156. ),
  157. ),
  158. );
  159. if (!$info['has_required']) {
  160. $form['conf'][$info['region']][$key][$key . '_hidden'] = array(
  161. '#type' => 'checkbox',
  162. '#title' => t('Hide'),
  163. '#default_value' => isset($info['hidden']) ? $info['hidden'] : FALSE,
  164. );
  165. }
  166. if (isset($info['collapsed'])) {
  167. $form['conf'][$info['region']][$key][$key . '_collapsed'] = array(
  168. '#type' => 'checkbox',
  169. '#title' => t('Show collapsed'),
  170. '#default_value' => isset($info['collapsed']) ? $info['collapsed'] : FALSE,
  171. );
  172. }
  173. }
  174. $form['submit'] = array(
  175. '#type' => 'submit',
  176. '#value' => t('Save'),
  177. );
  178. return $form;
  179. }
  180. /**
  181. * Checks if a form element or any of it's children are required
  182. *
  183. * @param array $element
  184. * @return bool
  185. */
  186. function _nodeformcols_has_required($element) {
  187. if (isset($element['#required']) && $element['#required']) {
  188. return TRUE;
  189. }
  190. $children = element_children($element);
  191. foreach ($children as $child) {
  192. if (_nodeformcols_has_required($element[$child])) {
  193. return TRUE;
  194. }
  195. }
  196. return FALSE;
  197. }
  198. function _nodeformcols_configuration_form_after_build($form) {
  199. drupal_add_js('misc/tableheader.js');
  200. drupal_add_js(drupal_get_path('module', 'nodeformcols') . '/js/nodeformcols.js');
  201. drupal_add_css(drupal_get_path('module', 'nodeformcols') . '/css/nodeformcols.admin.css');
  202. $regions = nodeformcols_form_regions();
  203. foreach ($regions as $region => $title) {
  204. if (isset($form['conf'][$region]) && is_array($form['conf'][$region])) {
  205. uasort($form['conf'][$region], "element_sort");
  206. }
  207. drupal_add_tabledrag('fields', 'match', 'sibling', 'field-region-select', 'field-region-' . $region, NULL, FALSE);
  208. drupal_add_tabledrag('fields', 'order', 'sibling', 'field-weight', 'field-weight-' . $region);
  209. }
  210. return $form;
  211. }
  212. function nodeformcols_configuration_form_submit($form, $form_state) {
  213. $values = $form_state['values'];
  214. $type = $values['type'];
  215. $placements = nodeformscols_field_placements($type, $form['#variant']);
  216. nodeformcols_update_placements($type, $form['#variant'], $placements);
  217. foreach ($placements as $name => &$opt) {
  218. $field = 'field_' . $name;
  219. $opt['region'] = $values[$field . '_region'];
  220. $opt['weight'] = $values[$field . '_weight'];
  221. if (isset($values[$field . '_hidden'])) {
  222. $opt['hidden'] = $values[$field . '_hidden'];
  223. }
  224. if (isset($values[$field . '_collapsed'])) {
  225. $opt['collapsed'] = $values[$field . '_collapsed'];
  226. }
  227. }
  228. variable_set('nodeformscols_field_placements_' . $type .
  229. '_' . $form['#variant'], $placements);
  230. }