term.inc 6.0 KB

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