taxonomy_term.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * @file
  4. * Define Linkit term search plugin class.
  5. */
  6. /**
  7. * Reprecents a Linkit term search plugin.
  8. */
  9. class LinkitSearchPluginTaxonomy_term extends LinkitSearchPluginEntity {
  10. /**
  11. * Overrides LinkitSearchPluginEntity::__construct().
  12. */
  13. function __construct($plugin, $profile) {
  14. /**
  15. * The term entity doesn't use the same column name as in the entity keys
  16. * bundle definition, so lets add it our self.
  17. */
  18. $this->entity_key_bundle = 'vid';
  19. parent::__construct($plugin, $profile);
  20. }
  21. /**
  22. * Overrides LinkitSearchPluginEntity::createDescription().
  23. */
  24. function createDescription($data) {
  25. $description = token_replace(check_plain($this->conf['result_description']), array(
  26. 'term' => $data,
  27. ), array('clear' => TRUE));
  28. return $description;
  29. }
  30. /**
  31. * Overrides LinkitSearchPluginEntity::createGroup().
  32. */
  33. function createGroup($entity) {
  34. // Get the entity label.
  35. $group = $this->entity_info['label'];
  36. if (isset($this->conf['group_by_bundle']) && $this->conf['group_by_bundle']) {
  37. $bundles = $this->entity_info['bundles'];
  38. $bundle_name = $bundles[$entity->vocabulary_machine_name]['label'];
  39. $group .= ' - ' . check_plain($bundle_name);
  40. }
  41. return $group;
  42. }
  43. /**
  44. * Overrides LinkitSearchPluginEntity::fetchResults().
  45. */
  46. function fetchResults($search_string) {
  47. // The term entity doesn't use the entity keys bundle definition, its using
  48. // the vid instead, so lets 'translate' the bundle names to vids.
  49. if (isset($this->entity_key_bundle) && isset($this->conf['bundles']) ) {
  50. $bundles = array_filter($this->conf['bundles']);
  51. // Get all vocabularies.
  52. $vocabularies = taxonomy_vocabulary_get_names();
  53. // Temp storage for values.
  54. $tmp_bundles = array();
  55. foreach ($bundles as $bundle) {
  56. $tmp_bundles[] = $vocabularies[$bundle]->{$this->entity_key_bundle};
  57. }
  58. // Assign the new values as the bundles.
  59. $this->conf['bundles'] = $tmp_bundles;
  60. }
  61. // Call the parent.
  62. return parent::fetchResults($search_string);
  63. }
  64. /**
  65. * Overrides LinkitSearchPlugin::buildSettingsForm().
  66. */
  67. function buildSettingsForm() {
  68. $form = parent::buildSettingsForm();
  69. // The entity plugin uses the entity name for the #token_types, but terms
  70. // is a special case, its name is "Taxonomy_term" and the tokens are defined
  71. // (in the taxonomy module) with just "term".
  72. // If the token modules is installed.
  73. if (isset($form[$this->plugin['name']]['token_help']['help']['#token_types'])) {
  74. $form[$this->plugin['name']]['token_help']['help']['#token_types'] = array('term');
  75. }
  76. // If the token module is not installed, use the orignal tokens provided by
  77. // Drupal core.
  78. else {
  79. // Get supported tokens for the term entity type.
  80. $tokens = linkit_extract_tokens('term');
  81. $form[$this->plugin['name']]['result_description']['#description'] = t('Available tokens: %tokens.', array('%tokens' => implode(', ', $tokens)));
  82. }
  83. return $form;
  84. }
  85. }