term.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide a term 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 term"),
  12. 'description' => t('A single taxonomy term object.'),
  13. 'context' => 'ctools_context_create_term',
  14. 'edit form' => 'ctools_context_term_settings_form',
  15. 'defaults' => array(
  16. 'vid' => '',
  17. 'tid' => '',
  18. ),
  19. 'keyword' => 'term',
  20. 'context name' => 'term',
  21. 'convert list' => array(
  22. 'tid' => t('Term ID'),
  23. 'name' => t('Term name'),
  24. 'name_dashed' => t('Term name, lowercased and spaces converted to dashes'),
  25. 'description' => t('Term Description'),
  26. 'vid' => t('Vocabulary ID'),
  27. ),
  28. 'convert' => 'ctools_context_term_convert',
  29. // This context is deprecated and should not be usable in the UI.
  30. 'no ui' => TRUE,
  31. 'no required context ui' => TRUE,
  32. );
  33. /**
  34. * It's important to remember that $conf is optional here, because contexts
  35. * are not always created from the UI.
  36. */
  37. function ctools_context_create_term($empty, $data = NULL, $conf = FALSE) {
  38. $context = new ctools_context('term');
  39. $context->plugin = 'term';
  40. if ($empty) {
  41. return $context;
  42. }
  43. if ($conf && isset($data['tid'])) {
  44. $data = taxonomy_term_load($data['tid']);
  45. }
  46. if (!empty($data)) {
  47. $context->data = $data;
  48. $context->title = $data->name;
  49. $context->argument = $data->tid;
  50. $context->description = $data->description;
  51. return $context;
  52. }
  53. }
  54. function ctools_context_term_settings_form($form, &$form_state) {
  55. $conf = $form_state['conf'];
  56. $form['vid'] = array(
  57. '#title' => t('Vocabulary'),
  58. '#type' => 'select',
  59. '#options' => array(),
  60. '#description' => t('Select the vocabulary for this form.'),
  61. '#id' => 'ctools-select-vid',
  62. '#default_value' => $conf['vid'],
  63. );
  64. $description = '';
  65. if (!empty($conf['tid'])) {
  66. $info = db_query('SELECT * FROM {taxonomy_term_data} WHERE tid = :tid', array(':tid' => $conf['tid']))->fetchObject();
  67. if ($info) {
  68. $description = ' ' . t('Currently set to @term. Enter another term if you wish to change the term.', array('@term' => $info->name));
  69. }
  70. }
  71. ctools_include('dependent');
  72. $options = array();
  73. $form['taxonomy']['#tree'] = TRUE;
  74. foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
  75. $options[$vid] = $vocabulary->name;
  76. $form['taxonomy'][$vocabulary->vid] = array(
  77. '#type' => 'textfield',
  78. '#description' => t('Select a term from @vocabulary.', array('@vocabulary' => $vocabulary->name)) . $description,
  79. '#autocomplete_path' => 'taxonomy/autocomplete/' . $vocabulary->vid,
  80. '#dependency' => array('ctools-select-vid' => array($vocabulary->vid)),
  81. );
  82. }
  83. $form['vid']['#options'] = $options;
  84. $form['tid'] = array(
  85. '#type' => 'value',
  86. '#value' => $conf['tid'],
  87. );
  88. $form['set_identifier'] = array(
  89. '#type' => 'checkbox',
  90. '#default_value' => FALSE,
  91. '#title' => t('Reset identifier to term title'),
  92. '#description' => t('If checked, the identifier will be reset to the term name of the selected term.'),
  93. );
  94. return $form;
  95. }
  96. /**
  97. * Validate a term.
  98. */
  99. function ctools_context_term_settings_form_validate($form, &$form_state) {
  100. // Validate the autocomplete.
  101. $vid = $form_state['values']['vid'];
  102. if (empty($form_state['values']['tid']) && empty($form_state['values']['taxonomy'][$vid])) {
  103. form_error($form['taxonomy'][$vid], t('You must select a term.'));
  104. return;
  105. }
  106. if (empty($form_state['values']['taxonomy'][$vid])) {
  107. return;
  108. }
  109. $term = db_query('SELECT tid FROM {taxonomy_term_data} WHERE LOWER(name) = LOWER(:name) AND vid = :vid', array(':name' => $form_state['values']['taxonomy'][$vid], ':vid' => $vid))->fetchObject();
  110. if (!$term) {
  111. form_error($form['taxonomy'][$vid], t('Invalid term selected.'));
  112. }
  113. else {
  114. form_set_value($form['tid'], $term->tid, $form_state);
  115. }
  116. }
  117. function ctools_context_term_settings_form_submit($form, &$form_state) {
  118. if ($form_state['values']['set_identifier']) {
  119. $term = db_query('SELECT tid, name FROM {taxonomy_term_data} WHERE LOWER(tid) = :tid', array(':tid' => $form_state['values']['tid']))->fetchObject();
  120. $form_state['values']['identifier'] = $term->name;
  121. }
  122. $form_state['conf']['tid'] = $form_state['values']['tid'];
  123. $form_state['conf']['vid'] = $form_state['values']['vid'];
  124. }
  125. /**
  126. * Convert a context into a string.
  127. */
  128. function ctools_context_term_convert($context, $type) {
  129. switch ($type) {
  130. case 'tid':
  131. return $context->data->tid;
  132. case 'name':
  133. return $context->data->name;
  134. case 'name_dashed':
  135. return drupal_strtolower(str_replace(' ', '-', $context->data->name));
  136. case 'vid':
  137. return $context->data->vid;
  138. case 'description':
  139. return $context->data->description;
  140. }
  141. }