term_synonyms.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide a synonyms-friendly argument handler for a Taxonomy term.
  5. */
  6. $plugin = array(
  7. 'title' => t("Taxonomy term: ID (synonyms-friendly)"),
  8. 'keyword' => 'term',
  9. 'description' => t('Creates a single taxonomy term from a taxonomy term name or one of its synonyms.'),
  10. 'context' => 'synonyms_term_synonyms_context',
  11. 'default' => array('breadcrumb' => TRUE, 'transform' => FALSE),
  12. 'settings form' => 'synonyms_term_synonyms_settings_form',
  13. 'placeholder form' => 'synonyms_term_synonyms_ctools_argument_placeholder',
  14. 'breadcrumb' => 'synonyms_term_synonyms_breadcrumb',
  15. );
  16. /**
  17. * Discover if this argument gives us the term we crave.
  18. */
  19. function synonyms_term_synonyms_context($arg = NULL, $conf = NULL, $empty = FALSE) {
  20. // If unset it wants a generic, unfilled context.
  21. if ($empty) {
  22. return ctools_context_create_empty('entity:taxonomy_term');
  23. }
  24. $conf['vids'] = is_array($conf['vids']) ? array_filter($conf['vids']) : array();
  25. if (is_object($arg)) {
  26. $term = $arg;
  27. if (!empty($conf['vids']) && empty($conf['vids'][$term->vid])) {
  28. return NULL;
  29. }
  30. }
  31. else {
  32. if ($conf['transform']) {
  33. $tids = db_select('taxonomy_term_data', 't')
  34. ->fields('t', array('tid'))
  35. ->where("REPLACE(t.name, ' ', '-') = :argument", array(
  36. ':argument' => $arg,
  37. ));
  38. if (!empty($conf['vids'])) {
  39. $tids->condition('t.vid', $conf['vids']);
  40. }
  41. $tids = $tids->execute()->fetchCol();
  42. $terms = taxonomy_term_load_multiple($tids);
  43. }
  44. else {
  45. $terms = taxonomy_get_term_by_name($arg);
  46. }
  47. if (!empty($conf['vids'])) {
  48. foreach ($terms as $k => $term) {
  49. if (!isset($conf['vids'][$term->vid])) {
  50. unset($terms[$k]);
  51. }
  52. }
  53. }
  54. if (empty($terms)) {
  55. // We couldn't find the term by name, so we will look it up now by
  56. // synonyms.
  57. $vocabularies = taxonomy_vocabulary_load_multiple(empty($conf['vids']) ? FALSE : $conf['vids']);
  58. foreach ($vocabularies as $vocabulary) {
  59. $condition = db_and();
  60. if ($conf['transform']) {
  61. $condition->where("REPLACE(" . AbstractSynonymsSynonymsBehavior::COLUMN_PLACEHOLDER . ", ' ', '-') = :argument", array(
  62. ':argument' => $arg,
  63. ));
  64. }
  65. else {
  66. $condition->condition(AbstractSynonymsSynonymsBehavior::COLUMN_PLACEHOLDER, $arg);
  67. }
  68. $rows = synonyms_synonyms_find($condition, 'taxonomy_term', $vocabulary->machine_name);
  69. if (!empty($rows)) {
  70. // We have found a match, no need to search further.
  71. $terms[] = taxonomy_term_load($rows[0]->entity_id);
  72. break;
  73. }
  74. }
  75. }
  76. if (empty($terms)) {
  77. return NULL;
  78. }
  79. $term = array_shift($terms);
  80. }
  81. $context = ctools_context_create('entity:taxonomy_term', $term);
  82. $context->original_argument = $arg;
  83. return $context;
  84. }
  85. /**
  86. * Settings form for the argument.
  87. */
  88. function synonyms_term_synonyms_settings_form(&$form, &$form_state, $conf) {
  89. $vocabularies = taxonomy_get_vocabularies();
  90. $options = array();
  91. foreach ($vocabularies as $vid => $vocab) {
  92. $options[$vid] = $vocab->name;
  93. }
  94. $form['settings']['vids'] = array(
  95. '#title' => t('Limit to these vocabularies'),
  96. '#type' => 'checkboxes',
  97. '#options' => $options,
  98. '#default_value' => !empty($conf['vids']) ? $conf['vids'] : array(),
  99. '#description' => t('If no vocabularies are checked, terms from all vocabularies will be accepted.'),
  100. );
  101. $form['settings']['breadcrumb'] = array(
  102. '#title' => t('Inject hierarchy into breadcrumb trail'),
  103. '#type' => 'checkbox',
  104. '#default_value' => !empty($conf['breadcrumb']),
  105. '#description' => t('If checked, taxonomy term parents will appear in the breadcrumb trail.'),
  106. );
  107. $form['settings']['transform'] = array(
  108. '#title' => t('Transform dashes in URL to spaces in term name filter values'),
  109. '#type' => 'checkbox',
  110. '#default_value' => !empty($conf['transform']),
  111. );
  112. }
  113. /**
  114. * Form fragment to get an argument to convert a placeholder for preview.
  115. */
  116. function synonyms_term_synonyms_ctools_argument_placeholder($conf) {
  117. return array(
  118. '#type' => 'textfield',
  119. '#description' => t('Enter a taxonomy term name.'),
  120. );
  121. }
  122. /**
  123. * Inject the breadcrumb trail if necessary.
  124. */
  125. function synonyms_term_synonyms_breadcrumb($conf, $context) {
  126. // Outsource the real implementation of breadcrumb to terms argument plugin.
  127. $plugin = ctools_get_plugins('ctools', 'arguments', 'term');
  128. $function = ctools_plugin_get_function($plugin, 'breadcrumb');
  129. if ($function) {
  130. call_user_func_array($function, func_get_args());
  131. }
  132. }