nodeformcols.admin.inc 8.5 KB

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