term.inc 4.8 KB

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