term.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. '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 (is_object($arg)) {
  31. $term = $arg;
  32. }
  33. else {
  34. switch ($conf['input_form']) {
  35. case 'tid':
  36. default:
  37. if (!is_numeric($arg)) {
  38. return FALSE;
  39. }
  40. $term = taxonomy_term_load($arg);
  41. break;
  42. case 'term':
  43. if (!empty($conf['transform'])) {
  44. $arg = strtr($arg, '-', ' ');
  45. }
  46. $terms = taxonomy_get_term_by_name($arg);
  47. $conf['vids'] = is_array($conf['vids']) ? array_filter($conf['vids']) : NULL;
  48. if ((count($terms) > 1) && isset($conf['vids'])) {
  49. foreach ($terms as $potential) {
  50. foreach ($conf['vids'] as $vid => $active) {
  51. if ($active && $potential->vid == $vid) {
  52. $term = $potential;
  53. // break out of the foreaches AND the case
  54. break 3;
  55. }
  56. }
  57. }
  58. }
  59. $term = array_shift($terms);
  60. break;
  61. }
  62. if (empty($term)) {
  63. return NULL;
  64. }
  65. }
  66. if (!empty($conf['vids']) && array_filter($conf['vids']) && empty($conf['vids'][$term->vid])) {
  67. return NULL;
  68. }
  69. $context = ctools_context_create('entity:taxonomy_term', $term);
  70. $context->original_argument = $arg;
  71. return $context;
  72. }
  73. /**
  74. * Settings form for the argument
  75. */
  76. function ctools_term_settings_form(&$form, &$form_state, $conf) {
  77. // @todo allow synonym use like Views does.
  78. $form['settings']['input_form'] = array(
  79. '#title' => t('Argument type'),
  80. '#type' => 'radios',
  81. '#options' => array('tid' => t('Term ID'), 'term' => t('Term name')),
  82. '#default_value' => $conf['input_form'],
  83. '#prefix' => '<div class="clearfix">',
  84. '#suffix' => '</div>',
  85. );
  86. $vocabularies = taxonomy_get_vocabularies();
  87. $options = array();
  88. foreach ($vocabularies as $vid => $vocab) {
  89. $options[$vid] = $vocab->name;
  90. }
  91. $form['settings']['vids'] = array(
  92. '#title' => t('Limit to these vocabularies'),
  93. '#type' => 'checkboxes',
  94. '#options' => $options,
  95. '#default_value' => !empty($conf['vids']) ? $conf['vids'] : array(),
  96. '#description' => t('If no vocabularies are checked, terms from all vocabularies will be accepted.'),
  97. );
  98. $form['settings']['breadcrumb'] = array(
  99. '#title' => t('Inject hierarchy into breadcrumb trail'),
  100. '#type' => 'checkbox',
  101. '#default_value' => !empty($conf['breadcrumb']),
  102. '#description' => t('If checked, taxonomy term parents will appear in the breadcrumb trail.'),
  103. );
  104. $form['settings']['transform'] = array(
  105. '#title' => t('Transform dashes in URL to spaces in term name filter values'),
  106. '#type' => 'checkbox',
  107. '#default_value' => !empty($conf['transform']),
  108. );
  109. // return $form;
  110. }
  111. /**
  112. * Form fragment to get an argument to convert a placeholder for preview.
  113. */
  114. function ctools_term_ctools_argument_placeholder($conf) {
  115. switch ($conf['input_form']) {
  116. case 'tid':
  117. default:
  118. return array(
  119. '#type' => 'textfield',
  120. '#description' => t('Enter a taxonomy term ID.'),
  121. );
  122. case 'term':
  123. return array(
  124. '#type' => 'textfield',
  125. '#description' => t('Enter a taxonomy term name.'),
  126. );
  127. }
  128. }
  129. /**
  130. * Inject the breadcrumb trail if necessary.
  131. */
  132. function ctools_term_breadcrumb($conf, $context) {
  133. if (empty($conf['breadcrumb']) || empty($context->data) || empty($context->data->tid)) {
  134. return;
  135. }
  136. $breadcrumb = array();
  137. $current = new stdClass();
  138. $current->tid = $context->data->tid;
  139. while ($parents = taxonomy_get_parents($current->tid)) {
  140. $current = array_shift($parents);
  141. $breadcrumb[] = l($current->name, 'taxonomy/term/' . $current->tid);
  142. }
  143. $breadcrumb = array_merge(drupal_get_breadcrumb(), array_reverse($breadcrumb));
  144. drupal_set_breadcrumb($breadcrumb);
  145. }