content_taxonomy.module 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Implements hook_form_ID_alter().
  4. */
  5. function content_taxonomy_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
  6. $field = $form['#field'];
  7. $instance = $form['#instance'];
  8. // Add parent selctor to term reference fields,
  9. // except to the autocomplete widget, as it ignores the parent setting.
  10. if ($field['type'] == 'taxonomy_term_reference'
  11. && !($instance['widget']['type'] == 'taxonomy_autocomplete' || $instance['widget']['type'] == 'autocomplete_deluxe_taxonomy')) {
  12. // add parent form.
  13. foreach ($field['settings']['allowed_values'] as $delta => $tree) {
  14. $options[0] = '---';
  15. // todo this might break with huge vocs
  16. $voc = taxonomy_vocabulary_machine_name_load($tree['vocabulary']);
  17. foreach (taxonomy_get_tree($voc->vid) as $term) {
  18. $options[$term->tid] = str_repeat('- ', $term->depth) . $term->name;
  19. }
  20. $form['field']['settings']['allowed_values'][$delta]['parent'] = array(
  21. '#type' => 'select',
  22. '#title' => t('Parent'),
  23. '#options' => $options,
  24. '#default_value' => isset($tree['parent']) ? $tree['parent'] : 0,
  25. );
  26. $form['field']['settings']['allowed_values'][$delta]['depth'] = array(
  27. '#type' => 'textfield',
  28. '#title' => t('Tree depth'),
  29. '#default_value' => isset($tree['depth']) ? $tree['depth'] : '',
  30. '#description' => t('Set the depth of the tree. Leave empty to load all terms.'),
  31. '#element_validate' => array('_element_validate_integer_positive'),
  32. );
  33. }
  34. }
  35. }
  36. /**
  37. * Implements hook_field_info_alter().
  38. */
  39. function content_taxonomy_field_info_alter(&$info) {
  40. // Use own options callback for handling additional configuration options.
  41. $info['taxonomy_term_reference']['settings']['options_list_callback'] = 'content_taxonomy_allowed_values';
  42. // Add depth option.
  43. foreach ($info['taxonomy_term_reference']['settings']['allowed_values'] as $key => $values) {
  44. $info['taxonomy_term_reference']['settings']['allowed_values'][$key]['depth'] = 0;
  45. }
  46. }
  47. /**
  48. * Returns the set of valid terms for a taxonomy field.
  49. * Extends taxonomy_allowed_values() with the tree depth option.
  50. *
  51. * @param $field
  52. * The field definition.
  53. * @return
  54. * The array of valid terms for this field, keyed by term id.
  55. */
  56. function content_taxonomy_allowed_values($field) {
  57. $options = array();
  58. foreach ($field['settings']['allowed_values'] as $tree) {
  59. if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
  60. $max_depth = (isset($tree['depth']) && !empty($tree['depth'])) ? $tree['depth'] : NULL;
  61. if ($terms = taxonomy_get_tree($vocabulary->vid, $tree['parent'], $max_depth)) {
  62. foreach ($terms as $term) {
  63. $options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
  64. }
  65. }
  66. }
  67. }
  68. return $options;
  69. }