term.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * Plugin to provide an argument handler for a Taxonomy term
  6. */
  7. /**
  8. * Plugins are described by creating a $plugin array which will be used
  9. * by the system that includes this file.
  10. */
  11. $plugin = array(
  12. 'title' => t("Taxonomy term: ID"),
  13. // keyword to use for %substitution
  14. 'keyword' => 'term',
  15. 'description' => t('Creates a single taxonomy term from a taxonomy ID or taxonomy term name.'),
  16. 'context' => 'ctools_term_context',
  17. 'default' => array('input_form' => 'tid', 'breadcrumb' => TRUE, 'transform' => FALSE),
  18. 'settings form' => 'ctools_term_settings_form',
  19. 'settings form validate' => 'ctools_term_settings_form_validate',
  20. 'placeholder form' => 'ctools_term_ctools_argument_placeholder',
  21. 'breadcrumb' => 'ctools_term_breadcrumb',
  22. );
  23. /**
  24. * Discover if this argument gives us the term we crave.
  25. */
  26. function ctools_term_context($arg = NULL, $conf = NULL, $empty = FALSE) {
  27. // If unset it wants a generic, unfilled context.
  28. if ($empty) {
  29. return ctools_context_create_empty('entity:taxonomy_term');
  30. }
  31. if (isset($conf['vocabularies'])) {
  32. $vocabularies = $conf['vocabularies'];
  33. }
  34. else {
  35. $vids = isset($conf['vids']) ? $conf['vids'] : array();
  36. // Convert legacy use of vids to machine names.
  37. $vocabularies = _ctools_term_vocabulary_machine_name_convert($vids);
  38. }
  39. if (is_object($arg)) {
  40. $term = $arg;
  41. }
  42. else {
  43. switch ($conf['input_form']) {
  44. case 'tid':
  45. default:
  46. if (!is_numeric($arg)) {
  47. return FALSE;
  48. }
  49. $term = taxonomy_term_load($arg);
  50. break;
  51. case 'term':
  52. if (!empty($conf['transform'])) {
  53. $arg = strtr($arg, '-', ' ');
  54. }
  55. $terms = taxonomy_get_term_by_name($arg);
  56. // If only one term is found, fall through to vocabulary check below.
  57. if ((count($terms) > 1) && $vocabularies) {
  58. foreach ($terms as $potential) {
  59. foreach ($vocabularies as $machine_name) {
  60. if ($potential->vocabulary_machine_name == $machine_name) {
  61. $term = $potential;
  62. // break out of the foreaches AND the case
  63. break 3;
  64. }
  65. }
  66. }
  67. }
  68. $term = array_shift($terms);
  69. break;
  70. }
  71. if (empty($term)) {
  72. return NULL;
  73. }
  74. }
  75. if ($vocabularies && !isset($vocabularies[$term->vocabulary_machine_name])) {
  76. return NULL;
  77. }
  78. $context = ctools_context_create('entity:taxonomy_term', $term);
  79. $context->original_argument = $arg;
  80. return $context;
  81. }
  82. /**
  83. * Settings form for the argument
  84. */
  85. function ctools_term_settings_form(&$form, &$form_state, $conf) {
  86. // @todo allow synonym use like Views does.
  87. $form['settings']['input_form'] = array(
  88. '#title' => t('Argument type'),
  89. '#type' => 'radios',
  90. '#options' => array('tid' => t('Term ID'), 'term' => t('Term name')),
  91. '#default_value' => $conf['input_form'],
  92. '#prefix' => '<div class="clearfix">',
  93. '#suffix' => '</div>',
  94. );
  95. $vocabularies = taxonomy_get_vocabularies();
  96. $options = array();
  97. foreach ($vocabularies as $vid => $vocab) {
  98. $options[$vocab->machine_name] = $vocab->name;
  99. }
  100. // Fallback on legacy 'vids', when no vocabularies are available.
  101. if (empty($conf['vocabularies']) && !empty($conf['vids'])) {
  102. $conf['vocabularies'] = _ctools_term_vocabulary_machine_name_convert(array_filter($conf['vids']));
  103. unset($conf['vids']);
  104. }
  105. $form['settings']['vocabularies'] = array(
  106. '#title' => t('Limit to these vocabularies'),
  107. '#type' => 'checkboxes',
  108. '#options' => $options,
  109. '#default_value' => !empty($conf['vocabularies']) ? $conf['vocabularies'] : array(),
  110. '#description' => t('If no vocabularies are checked, terms from all vocabularies will be accepted.'),
  111. );
  112. $form['settings']['breadcrumb'] = array(
  113. '#title' => t('Inject hierarchy into breadcrumb trail'),
  114. '#type' => 'checkbox',
  115. '#default_value' => !empty($conf['breadcrumb']),
  116. '#description' => t('If checked, taxonomy term parents will appear in the breadcrumb trail.'),
  117. );
  118. $form['settings']['transform'] = array(
  119. '#title' => t('Transform dashes in URL to spaces in term name filter values'),
  120. '#type' => 'checkbox',
  121. '#default_value' => !empty($conf['transform']),
  122. );
  123. // return $form;
  124. }
  125. function ctools_term_settings_form_validate (&$form, &$form_state) {
  126. // Filter the selected vocabularies to avoid storing redundant data.
  127. $vocabularies = array_filter($form_state['values']['settings']['vocabularies']);
  128. form_set_value($form['settings']['vocabularies'], $vocabularies, $form_state);
  129. }
  130. /**
  131. * Form fragment to get an argument to convert a placeholder for preview.
  132. */
  133. function ctools_term_ctools_argument_placeholder($conf) {
  134. switch ($conf['input_form']) {
  135. case 'tid':
  136. default:
  137. return array(
  138. '#type' => 'textfield',
  139. '#description' => t('Enter a taxonomy term ID.'),
  140. );
  141. case 'term':
  142. return array(
  143. '#type' => 'textfield',
  144. '#description' => t('Enter a taxonomy term name.'),
  145. );
  146. }
  147. }
  148. /**
  149. * Inject the breadcrumb trail if necessary.
  150. */
  151. function ctools_term_breadcrumb($conf, $context) {
  152. if (empty($conf['breadcrumb']) || empty($context->data) || empty($context->data->tid)) {
  153. return;
  154. }
  155. $breadcrumb = array();
  156. $current = new stdClass();
  157. $current->tid = $context->data->tid;
  158. while ($parents = taxonomy_get_parents($current->tid)) {
  159. $current = array_shift($parents);
  160. $breadcrumb[] = l($current->name, 'taxonomy/term/' . $current->tid);
  161. }
  162. $breadcrumb = array_merge(drupal_get_breadcrumb(), array_reverse($breadcrumb));
  163. drupal_set_breadcrumb($breadcrumb);
  164. }
  165. /**
  166. * Helper function to convert convert legacy vocabulary ids into machine names.
  167. *
  168. * @param array $vids
  169. * Array of either vids.
  170. * @return array
  171. * A keyed array of machine names.
  172. */
  173. function _ctools_term_vocabulary_machine_name_convert($vids) {
  174. $vocabularies = taxonomy_vocabulary_load_multiple($vids);
  175. $return = array();
  176. foreach($vocabularies as $vocabulary) {
  177. $return[$vocabulary->machine_name] = $vocabulary->machine_name;
  178. }
  179. return $return;
  180. }