terms.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide a terms context.
  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 terms"),
  12. 'description' => t('Multiple taxonomy terms, as a group.'),
  13. 'context' => 'ctools_context_create_terms',
  14. 'keyword' => 'terms',
  15. // This context is deprecated and should not be usable in the UI.
  16. 'no ui' => TRUE,
  17. 'context name' => 'terms',
  18. 'convert list' => array(
  19. 'tid' => t('Term ID of first term'),
  20. 'tids' => t('Term ID of all term, separated by + or ,'),
  21. 'name' => t('Term name of first term'),
  22. 'name_dashed' => t('Term name of first term, lowercased and spaces converted to dashes'),
  23. 'names' => t('Term name of all terms, separated by + or ,'),
  24. 'names_dashed' => t('Term name of all terms, separated by + or , and lowercased and spaces converted to dashes'),
  25. 'vid' => t('Vocabulary ID of first term'),
  26. ),
  27. 'convert' => 'ctools_context_terms_convert',
  28. );
  29. /**
  30. * It's important to remember that $conf is optional here, because contexts
  31. * are not always created from the UI.
  32. */
  33. function ctools_context_create_terms($empty, $data = NULL, $conf = FALSE) {
  34. // The input is expected to be an object as created by ctools_break_phrase
  35. // which contains a group of terms.
  36. $context = new ctools_context(array('terms', 'entity:taxonomy_term'));
  37. $context->plugin = 'terms';
  38. if ($empty) {
  39. return $context;
  40. }
  41. if (!empty($data) && is_object($data)) {
  42. $context->operator = $data->operator;
  43. $context->tids = $data->value;
  44. if (!isset($data->term)) {
  45. // Load the first term:
  46. reset($context->tids);
  47. $data->term = taxonomy_term_load(current($context->tids));
  48. }
  49. $context->data = $data->term;
  50. $context->title = $data->term->name;
  51. $context->argument = implode($context->operator == 'or' ? '+' : ',', array_unique($context->tids));
  52. return $context;
  53. }
  54. }
  55. /**
  56. * Convert a context into a string.
  57. */
  58. function ctools_context_terms_convert($context, $type) {
  59. switch ($type) {
  60. case 'tid':
  61. return $context->data->tid;
  62. case 'tids':
  63. return $context->argument;
  64. case 'name':
  65. return $context->data->name;
  66. case 'name_dashed':
  67. return drupal_strtolower(str_replace(' ', '-', $context->data->name));
  68. case 'names':
  69. case 'names_dashed':
  70. // We only run this query if this item was requested:
  71. if (!isset($context->names)) {
  72. if (empty($context->tids)) {
  73. $context->names = '';
  74. }
  75. else {
  76. $result = db_query('SELECT tid, name FROM {taxonomy_term_data} WHERE tid IN (:tids)', array(':tids' => $context->tids));
  77. foreach ($result as $term) {
  78. $names[$term->tid] = $term->name;
  79. if ($type == 'names_dashed') {
  80. $names[$term->tid] = drupal_strtolower(str_replace(' ', '-', $names[$term->tid]));
  81. }
  82. }
  83. $context->names = implode($context->operator == 'or' ? ' + ' : ', ', $names);
  84. }
  85. }
  86. return $context->names;
  87. case 'vid':
  88. return $context->data->vid;
  89. }
  90. }