term_synonyms.inc 4.7 KB

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