node_terms.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * @file
  4. * Plugins are described by creating a $plugin array which will be used
  5. * by the system that includes this file.
  6. */
  7. $plugin = array(
  8. 'single' => TRUE,
  9. 'title' => t('Node terms'),
  10. 'icon' => 'icon_node.png',
  11. 'description' => t('Taxonomy terms of the referenced node.'),
  12. 'required context' => new ctools_context_required(t('Node'), 'node'),
  13. 'category' => t('Node'),
  14. 'defaults' => array(
  15. 'vid' => 0,
  16. 'term_format' => 'term-links',
  17. 'link' => TRUE,
  18. 'term_delimiter' => ', ',
  19. ),
  20. );
  21. /**
  22. * Render the node_terms content type.
  23. */
  24. function ctools_node_terms_content_type_render($subtype, $conf, $panel_args, $context) {
  25. if (empty($context) || empty($context->data)) {
  26. return;
  27. }
  28. // Get a shortcut to the node.
  29. $node = $context->data;
  30. // Load all terms for this node from all vocabularies.
  31. $query = db_select('taxonomy_index', 't');
  32. $result = $query
  33. ->fields('t')
  34. ->condition('t.nid', $node->nid)
  35. ->execute();
  36. $tids = array();
  37. foreach ($result as $term) {
  38. $tids[] = $term->tid;
  39. }
  40. // Get the real term objects.
  41. $term_objects = taxonomy_term_load_multiple($tids);
  42. $terms = array();
  43. if (empty($conf['vid'])) {
  44. // All terms.
  45. foreach ($term_objects as $term) {
  46. $terms['taxonomy_term_' . $term->tid] = array(
  47. 'title' => check_plain($term->name),
  48. 'href' => 'taxonomy/term/' . $term->tid,
  49. 'attributes' => array('rel' => 'tag', 'title' => strip_tags($term->description)),
  50. );
  51. }
  52. }
  53. else {
  54. // They want something special and custom, we'll have to do this ourselves.
  55. foreach ($term_objects as $term) {
  56. if ($term->vid == $conf['vid']) {
  57. if ($conf['term_format'] == 'term-links') {
  58. $terms['taxonomy_term_' . $term->tid] = array(
  59. 'title' => $term->name,
  60. 'href' => 'taxonomy/term/' . $term->tid,
  61. 'attributes' => array('rel' => 'tag', 'title' => strip_tags($term->description)),
  62. );
  63. }
  64. elseif (empty($conf['link'])) {
  65. $terms[] = check_plain($term->name);
  66. }
  67. else {
  68. $terms[] = l($term->name, 'taxonomy/term/' . $term->tid, array('attributes' => array('rel' => 'tag', 'title' => strip_tags($term->description))));
  69. }
  70. }
  71. }
  72. }
  73. $formatted_terms = '';
  74. switch ($conf['term_format']) {
  75. case 'term-links':
  76. drupal_alter('link', $terms, $node);
  77. $formatted_terms = theme('links', array('links' => $terms));
  78. break;
  79. case 'ul':
  80. $formatted_terms = theme('item_list', array('items' => $terms));
  81. break;
  82. case 'inline-delimited':
  83. $delimiter = isset($conf['term_delimiter']) ? $conf['term_delimiter'] : ', ';
  84. $processed_terms = array();
  85. foreach ($terms as $key => $term) {
  86. if (is_string($term)) {
  87. $processed_terms[$key] = $term;
  88. }
  89. else {
  90. $terms[$key] = l($term['title'], $term['href'], $term);
  91. }
  92. }
  93. $formatted_terms = implode($delimiter, $processed_terms);
  94. break;
  95. }
  96. // Build the content type block.
  97. $block = new stdClass();
  98. $block->module = 'node_terms';
  99. $block->delta = $node->nid;
  100. $block->title = t('Terms');
  101. $block->content = $formatted_terms;
  102. return $block;
  103. }
  104. /**
  105. * Returns an edit form for node terms display settings.
  106. *
  107. * The first question is if they want to display all terms or restrict it to a
  108. * specific taxonomy vocabulary.
  109. *
  110. * Then, they're presented with a set of radios to find out how they want the
  111. * terms formatted, which can be either be via theme('links'), a regular item
  112. * list (ul), or inline with a delimiter. Depending on which radio they
  113. * choose, some other settings might appear. If they're doing either the ul or
  114. * inline, we ask if they want the terms to appear as links or not. If they
  115. * want it inline, we ask what delimiter they want to use.
  116. */
  117. function ctools_node_terms_content_type_edit_form($form, &$form_state) {
  118. ctools_include('dependent');
  119. $conf = $form_state['conf'];
  120. $options = array();
  121. $options[0] = t('- All vocabularies -');
  122. foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
  123. $options[$vid] = $vocabulary->name;
  124. }
  125. $form['vid'] = array(
  126. '#title' => t('Vocabulary'),
  127. '#type' => 'select',
  128. '#options' => $options,
  129. '#default_value' => $conf['vid'],
  130. '#description' => t('Optionally restrict the terms to a specific vocabulary, or allow terms from all vocabularies.'),
  131. '#prefix' => '<div class="clearfix">',
  132. '#suffix' => '</div>',
  133. );
  134. $form['term_format'] = array(
  135. '#type' => 'radios',
  136. '#title' => t('Term formatting'),
  137. '#options' => array(
  138. 'term-links' => t("Taxonomy links (uses theme('links'))"),
  139. 'ul' => t('Unordered list'),
  140. 'inline-delimited' => t('Inline, delimited'),
  141. ),
  142. '#default_value' => $conf['term_format'],
  143. '#prefix' => '<div class="clearfix">',
  144. '#suffix' => '</div>',
  145. );
  146. $form['link'] = array(
  147. '#title' => t('Link to terms'),
  148. '#type' => 'checkbox',
  149. '#default_value' => $conf['link'],
  150. '#description' => t('Check here to make the terms link to the term paths.'),
  151. '#dependency' => array('radio:term_format' => array('inline-delimited', 'ul')),
  152. '#prefix' => '<div class="clearfix">',
  153. '#suffix' => '</div>',
  154. );
  155. $form['term_delimiter'] = array(
  156. '#type' => 'textfield',
  157. '#title' => t('Term delimiter'),
  158. '#default_value' => $conf['term_delimiter'],
  159. '#size' => 10,
  160. '#dependency' => array('radio:term_format' => array('inline-delimited')),
  161. );
  162. return $form;
  163. }
  164. /**
  165. * Submit handler for the custom type settings form.
  166. */
  167. function ctools_node_terms_content_type_edit_form_submit($form, &$form_state) {
  168. // Copy everything from our defaults.
  169. foreach (array_keys($form_state['plugin']['defaults']) as $key) {
  170. $form_state['conf'][$key] = $form_state['values'][$key];
  171. }
  172. }
  173. /**
  174. * Returns the administrative title for a type.
  175. */
  176. function ctools_node_terms_content_type_admin_title($subtype, $conf, $context) {
  177. $placeholders['@s'] = $context->identifier;
  178. if (!empty($conf['vid'])) {
  179. $vocabulary = taxonomy_vocabulary_load($conf['vid']);
  180. $placeholders['@vocabulary'] = $vocabulary->name;
  181. return t('"@s" terms from @vocabulary', $placeholders);
  182. }
  183. return t('"@s" terms', $placeholders);
  184. }