terms.inc 3.1 KB

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