content_taxonomy_tree.module

Tracking 6.x-2.x branch
  1. drupal
    1. 6 contributions/content_taxonomy/content_taxonomy_tree.module

Functions & methods

NameDescription
content_taxonomy_tree_data2formHelper function to transpose the values as stored in the database to the format the content_taxonomy_tree needs to select the values
content_taxonomy_tree_elementsImplementation of hook_elements().
content_taxonomy_tree_form2dataHelper function to transpose the values returned by submitting the content_taxonomy_tree to the format to be stored in the field
content_taxonomy_tree_processProcesses the content_taxonomy_tree form element
content_taxonomy_tree_themeImplementation of hook_theme().
content_taxonomy_tree_validateValidation function for the content_taxonomy_tree element
content_taxonomy_tree_widgetImplementation of hook_widget().
content_taxonomy_tree_widget_infoImplementation of hook_widget_info().
content_taxonomy_tree_widget_settingsImplementation of hook_widget_settings
theme_content_taxonomy_treeFAPI theme for content_taxonomy_tree

File

View source
  1. <?php
  2. /**
  3. * Implementation of hook_theme().
  4. */
  5. function content_taxonomy_tree_theme() {
  6. return array(
  7. 'content_taxonomy_tree' => array(
  8. 'arguments' => array('element' => NULL),
  9. ),
  10. );
  11. }
  12. /**
  13. * Implementation of hook_widget_info().
  14. */
  15. function content_taxonomy_tree_widget_info() {
  16. return array(
  17. 'content_taxonomy_tree' => array(
  18. 'label' => t('Tree'),
  19. 'field types' => array('content_taxonomy'),
  20. 'multiple values' => CONTENT_HANDLE_MODULE,
  21. 'callbacks' => array(
  22. 'default value' => CONTENT_CALLBACK_DEFAULT,
  23. ),
  24. ),
  25. );
  26. return $items;
  27. }
  28. /**
  29. * Implementation of hook_widget_settings
  30. */
  31. function content_taxonomy_tree_widget_settings($op, $widget) {
  32. switch ($op) {
  33. case 'form':
  34. $form['settings'] = array(
  35. '#type' => 'fieldset',
  36. '#title' => t('Settings for Trees'),
  37. '#collapsible' => TRUE,
  38. '#weight' => 10,
  39. );
  40. $form['settings']['expand_all'] = array(
  41. '#type' => 'checkbox',
  42. '#title' => t('Expand whole tree by default'),
  43. '#default_value' => isset($widget['expand_all']) ? $widget['expand_all'] : 0,
  44. '#description' => t('Otherwise only branches, where a term is selected get expanded by default'),
  45. );
  46. return $form;
  47. case 'save':
  48. return array('expand_all');
  49. }
  50. }
  51. /**
  52. * Implementation of hook_elements().
  53. */
  54. function content_taxonomy_tree_elements() {
  55. return array(
  56. 'content_taxonomy_tree' => array(
  57. '#input' => TRUE,
  58. '#columns' => array('value'),
  59. '#delta' => 0,
  60. '#process' => array('content_taxonomy_tree_process'),
  61. '#element_validate' => array('content_taxonomy_tree_validate'),
  62. '#autocomplete_path' => FALSE,
  63. ),
  64. );
  65. }
  66. /**
  67. * Implementation of hook_widget().
  68. */
  69. function content_taxonomy_tree_widget(&$form, &$form_state, $field, $items, $delta = NULL) {
  70. $element = array(
  71. '#type' => 'content_taxonomy_tree',
  72. '#default_value' => !empty($items) ? $items : array(),
  73. );
  74. return $element;
  75. }
  76. /**
  77. * Processes the content_taxonomy_tree form element
  78. *
  79. * uses the 'taxonomy_manager_tree' element type
  80. */
  81. function content_taxonomy_tree_process($element, $edit, &$form_state, $form) {
  82. $field_name = $element['#field_name'];
  83. $field = $form['#field_info'][$field_name];
  84. $field_key = $element['#columns'][0];
  85. if (is_array($element['#value'])) {
  86. if (array_key_exists($field_key, $element['#value'])) {
  87. //validation function where values are set haven't been called yet for this form element
  88. //because some other element might have caused an validation error
  89. //so we have to do the processing of the values here
  90. if ($field['multiple']) {
  91. $selected_terms = _taxonomy_manager_tree_get_selected_terms($element['#value']['value']);
  92. $element['#value'] = content_transpose_array_rows_cols(array($field_key => array_values($selected_terms)));
  93. }
  94. }
  95. $element['#value'] = content_taxonomy_tree_data2form($element, $element['#value'], $field);
  96. }
  97. $element[$field_key] = array(
  98. '#type' => 'taxonomy_manager_tree',
  99. '#default_value' => isset($element['#value']) ? $element['#value'] : '',
  100. '#vid' => $field['vid'],
  101. '#parent' => content_taxonomy_field_get_parent($field),
  102. '#default_value' => is_array($element['#value']) ? $element['#value'] : array(),
  103. '#render_whole_tree' => TRUE,
  104. '#pager' => FALSE,
  105. '#add_term_info' => FALSE,
  106. '#expand_all' => $field['widget']['expand_all'],
  107. '#tree_is_required' => $field['required'], //using tree_is_required instead of required, prevents that error messages are shown twice
  108. '#required' => FALSE,
  109. '#multiple' => isset($field['multiple']) ? $field['multiple'] : FALSE,
  110. '#field_name' => $element['#field_name'],
  111. '#delta' => $element['#delta'],
  112. '#columns' => $element['#columns'],
  113. '#title' => $element['#title'],
  114. '#description' => $element['#description'],
  115. );
  116. // Make sure field info will be available to the validator which
  117. // does not get the values in $form.
  118. $form_state['#field_info'][$field['field_name']] = $field;
  119. return $element;
  120. }
  121. /**
  122. * Validation function for the content_taxonomy_tree element
  123. *
  124. * assigns the selected terms to the element
  125. * additionally checks if terms are selected for required fields (if this check isn't already done by form.inc)
  126. */
  127. function content_taxonomy_tree_validate($element, &$form_state) {
  128. $field = $form_state['#field_info'][$element['#field_name']];
  129. $field_key = $element['#columns'][0];
  130. $values = content_taxonomy_tree_form2data($element, $form_state, $field);
  131. //check for required fields
  132. //if $element['#value'] is empty, it's validated by form.inc
  133. //otherwise we have to check this by our own
  134. if ($field['required'] && is_null($values[0]['value']) && !empty($element['#value'])) {
  135. form_error($element[$field_key], t('!name field is required.', array('!name' => $element['#title'])));
  136. }
  137. if (($field['multiple'] >= 2) && (count($element['#value']) > $field['multiple'])) {
  138. form_error($element[$field_key], t('%name: this field cannot hold more than @count values.', array('%name' => t($field['widget']['label']), '@count' => $field['multiple'])));
  139. }
  140. form_set_value($element, $values, $form_state);
  141. }
  142. /**
  143. * Helper function to transpose the values as stored in the database
  144. * to the format the content_taxonomy_tree needs to select the values
  145. */
  146. function content_taxonomy_tree_data2form($element, $values, $field) {
  147. $terms = array();
  148. foreach ($values as $delta => $entry) {
  149. if ($entry['value']) {
  150. $terms[] = $entry['value'];
  151. }
  152. }
  153. return $terms;
  154. }
  155. /**
  156. * Helper function to transpose the values returned by submitting the content_taxonomy_tree
  157. * to the format to be stored in the field
  158. */
  159. function content_taxonomy_tree_form2data($element, &$form_state, $field) {
  160. $field_key = $element['#columns'][0];
  161. $selected_terms = $form_state['values'][$element['#field_name']][$field_key]['selected_terms'];
  162. if (!is_array($selected_terms) || !count($selected_terms)) {
  163. $selected_terms[] = NULL;
  164. }
  165. $form_state['#field_info'][$field['field_name']] = $field;
  166. return content_transpose_array_rows_cols(array($element['#columns'][0] => array_values($selected_terms)));
  167. }
  168. /**
  169. * FAPI theme for content_taxonomy_tree
  170. *
  171. * element already rendered by taxonomy_manager_tree
  172. */
  173. function theme_content_taxonomy_tree($element) {
  174. return $element['#children'];
  175. }