variable.form.inc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * @file
  4. * Variable API module. Form library.
  5. */
  6. /**
  7. * Build form element for a variable
  8. */
  9. function variable_form_element($variable, $options = array()) {
  10. $variable = variable_build($variable);
  11. $variable = variable_build_options($variable, $options);
  12. if (!empty($variable['element callback'])) {
  13. $element = call_user_func($variable['element callback'], $variable, $options);
  14. }
  15. elseif (isset($variable['options'])) {
  16. $element = variable_form_element_options($variable, $options);
  17. }
  18. else {
  19. $element = variable_form_element_default($variable, $options);
  20. }
  21. if (!empty($variable['validate callback'])) {
  22. $element['#element_validate'][] = 'variable_form_element_validate';
  23. }
  24. if (!empty($options['form parents'])) {
  25. $element['#parents'] = $options['form parents'];
  26. $element['#parents'][] = $variable['name'];
  27. }
  28. $element += array('#access' => variable_access($variable));
  29. if (!empty($variable['required'])) {
  30. $element += array('#required' => TRUE);
  31. }
  32. // Add variable data to element so we can use it later fo validation, etc..
  33. $element['#variable'] = $variable;
  34. return $element;
  35. }
  36. /**
  37. * Build array form element
  38. */
  39. function variable_form_element_array($variable, $options = array()) {
  40. // This will be possibly a fieldset with tree value
  41. $element = variable_form_element_default($variable, $options);
  42. // We may have a multiple element base that will default to plain textfield
  43. $item = $variable['repeat'];
  44. $value = variable_get_value($variable, $options);
  45. // Compile names and defaults for all elements
  46. $names = $defaults = array();
  47. if (!empty($variable['multiple'])) {
  48. // If we've got a multiple value, it will be an array with known elements
  49. $names = $variable['multiple'];
  50. }
  51. else {
  52. // Array with unknown elements, we add an element for each existing one
  53. $names = $value ? array_combine(array_keys($value), array_keys($value)) : array();
  54. }
  55. // Now build elements with the right names
  56. foreach ($names as $key => $title) {
  57. if (isset($value[$key]) && is_array($value[$key])) {
  58. // This element is an array, we cannot edit it but we need to add it to the form
  59. $element[$key] = array('#type' => 'value', '#value' => $value[$key]);
  60. $element['variable_element_array_' . $key] = array('#type' => 'item', '#title' => $title, '#markup' => variable_format_array($value[$key]));
  61. }
  62. else {
  63. $element[$key] = $item['element'] + array('#title' => $title, '#default_value' => isset($value[$key]) ? $value[$key] : '');
  64. }
  65. }
  66. return $element;
  67. }
  68. /**
  69. * Build multiple form element
  70. */
  71. function variable_form_element_multiple($variable, $options = array()) {
  72. $variable += array('element' => array(), 'title' => '', 'description' => '');
  73. $element = $variable['element'] + array(
  74. '#type' => 'fieldset',
  75. '#title' => $variable['title'],
  76. '#description' => $variable['description'],
  77. );
  78. foreach ($variable['children'] as $name => $item) {
  79. $element[$name] = variable_form_element($item, $options);
  80. }
  81. return $element;
  82. }
  83. /**
  84. * Build default form element
  85. */
  86. function variable_form_element_default($variable, $options = array()) {
  87. $variable += array('element' => array(), 'title' => '', 'description' => '');
  88. $type = variable_get_type($variable['type']) + array('element' => array());
  89. $element = $variable['element'] + array(
  90. '#title' => $variable['title'],
  91. '#description' => $variable['description'],
  92. ) + $type['element'];
  93. $value = variable_get_value($variable, $options);
  94. if (isset($value)) {
  95. $element['#default_value'] = $value;
  96. }
  97. return $element;
  98. }
  99. /**
  100. * Build form element for unknown variable.
  101. *
  102. * This is not an editable form element but a form item.
  103. */
  104. function variable_form_element_unknown($variable, $options = array()) {
  105. $variable += array('element' => array(), 'title' => '', 'description' => '');
  106. $type = variable_get_type($variable['type']) + array('element' => array());
  107. $element = $variable['element'] + array(
  108. '#title' => $variable['title'],
  109. '#description' => $variable['description'],
  110. '#markup' => variable_format_value($variable, $options),
  111. ) + $type['element'];
  112. return $element;
  113. }
  114. /**
  115. * Build form element for text_format variable.
  116. */
  117. function variable_form_element_text_format($variable, $options = array()) {
  118. $variable += array('element' => array(), 'title' => '', 'description' => '');
  119. $type = variable_get_type($variable['type']) + array('element' => array());
  120. $element = $variable['element'] + array(
  121. '#title' => $variable['title'],
  122. '#description' => $variable['description'],
  123. ) + $type['element'];
  124. $value = variable_get_value($variable, $options);
  125. if (isset($value) && is_array($value)) {
  126. if (isset($value['value'])) {
  127. $element['#default_value'] = $value['value'];
  128. }
  129. if (isset($value['format'])) {
  130. $element['#format'] = $value['format'];
  131. }
  132. }
  133. return $element;
  134. }
  135. /**
  136. * Build options variables
  137. */
  138. function variable_form_element_options($variable, $options = array()) {
  139. $element = variable_form_element_default($variable, $options);
  140. $element['#options'] = $variable['options'];
  141. // Depending on the number of options this may be radios or a drop-down.
  142. // However if there are nested options (an option is an array) it should be always a drop-down.
  143. if (empty($element['#type'])) {
  144. $element['#type'] = count($variable['options']) > 4 || array_filter(array_map('is_array', $variable['options'])) ? 'select' : 'radios';
  145. }
  146. return $element;
  147. }
  148. /**
  149. * Execute submit callbacks for variables in form.
  150. */
  151. function variable_form_submit_callback($form, &$form_state) {
  152. if (isset($form['#variable_edit_form'])) {
  153. // This may contain some realm options.
  154. $options = isset($form['#variable_options']) ? $form['#variable_options'] : array();
  155. foreach ($form['#variable_edit_form'] as $name) {
  156. $variable = variable_get_info($name);
  157. if ($variable && isset($variable['submit callback'])) {
  158. variable_include($variable);
  159. $variable['submit callback']($variable, $options, $form, $form_state);
  160. }
  161. }
  162. }
  163. }
  164. /**
  165. * Form to select variables
  166. */
  167. function theme_variable_table_select($variables) {
  168. $element = $variables['element'];
  169. $header = isset($element['#header']) ? $element['#header'] : array('element' => '', 'title' => t('Name'), 'description' => t('Description'));
  170. $fields = array_keys($header);
  171. $rows = array();
  172. foreach (element_children($element) as $name) {
  173. if (isset($element[$name]['#variable_name'])) {
  174. $variable_name = $element[$name]['#variable_name'];
  175. }
  176. else {
  177. $variable_name = str_replace(array('<', '>'), array('[', ']'), $name);
  178. }
  179. $variable = _variable_variable($variable_name);
  180. $row = array();
  181. foreach ($fields as $field) {
  182. if ($field == 'element') {
  183. $row[] = drupal_render($element[$name]);
  184. }
  185. else {
  186. $row[] = isset($variable[$field]) ? $variable[$field] : '';
  187. }
  188. }
  189. $rows[] = $row;
  190. }
  191. // Add a "Select all" checkbox.
  192. drupal_add_js('misc/tableselect.js');
  193. $header['element'] = array('class' => array('select-all'));
  194. return theme('table', array('header' => array_values($header), 'rows' => $rows));
  195. }