node_terms.inc 6.2 KB

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