term_list.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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('List of related terms'),
  9. 'icon' => 'icon_term.png',
  10. 'description' => t('Terms related to an existing term; may be child, siblings or top level.'),
  11. 'required context' => new ctools_context_required(t('Term'), array('term', 'taxonomy_term')),
  12. 'category' => t('Taxonomy term'),
  13. 'defaults' => array(
  14. 'title' => '',
  15. 'type' => 'child',
  16. 'include_current_term' => FALSE,
  17. 'list_type' => 'ul',
  18. 'path' => 'taxonomy/term',
  19. ),
  20. );
  21. function ctools_term_list_content_type_render($subtype, $conf, $panel_args, $context) {
  22. $term = isset($context->data) ? clone $context->data : NULL;
  23. $block = new stdClass();
  24. $block->module = 'term-list';
  25. $path = empty($conf['path']) ? 'taxonomy/term/%tid' : $conf['path'];
  26. if (strpos($path, '%tid') === FALSE) {
  27. if (substr($path, -1) != '/') {
  28. $path .= '/';
  29. }
  30. $path .= '%tid';
  31. }
  32. $options = ctools_admin_term_list_options();
  33. $skip = array();
  34. if ($term) {
  35. $block->title = $options[$conf['type']];
  36. $block->delta = $conf['type'];
  37. switch ($conf['type']) {
  38. case 'related':
  39. // FIXME this no longer exists, must be done with Field API
  40. // $terms = taxonomy_get_related($term->tid);
  41. break;
  42. case 'child':
  43. default:
  44. if (!empty($conf['include_current_term'])) {
  45. $terms[] = $term;
  46. }
  47. $terms = taxonomy_get_children($term->tid);
  48. break;
  49. case 'top':
  50. $terms = taxonomy_get_tree($term->vid, 0, 1);
  51. break;
  52. case 'parent':
  53. $terms = taxonomy_get_parents($term->tid);
  54. if (!empty($conf['include_current_term'])) {
  55. $terms[] = $term;
  56. }
  57. $block->title = count($terms) == 1 ? t('Parent term') : t('Parent terms');
  58. break;
  59. case 'sibling':
  60. $parent = db_query('SELECT parent FROM {taxonomy_term_hierarchy} WHERE tid = :tid', array(':tid' => $term->tid))->fetchField();
  61. if ($parent) {
  62. $terms = taxonomy_get_children($parent, $term->vid);
  63. }
  64. else {
  65. $terms = taxonomy_get_tree($term->vid, 0, 1);
  66. }
  67. $skip[$term->tid] = $term->tid;
  68. break;
  69. case 'synonyms':
  70. // FIXME this no longer exists, must be done with Field API
  71. // $terms = taxonomy_get_synonyms($term->tid);
  72. break;
  73. }
  74. if (!empty($terms)) {
  75. foreach ($terms as $related) {
  76. if (empty($skip[$related->tid])) {
  77. $items[] = l($related->name, str_replace('%tid', $related->tid, $path), array('rel' => 'tag', 'title' => strip_tags($related->description)));
  78. }
  79. }
  80. if (!empty($items)) {
  81. $block->content = theme('item_list', array('items' => $items, 'type' => $conf['list_type']));
  82. }
  83. }
  84. }
  85. else {
  86. $block->content = t('Term description goes here.');
  87. $block->delta = 'unknown';
  88. }
  89. return $block;
  90. }
  91. function ctools_admin_term_list_options() {
  92. return array(
  93. 'child' => t('Child terms'),
  94. 'related' => t('Related terms (does not work in D7)'),
  95. 'sibling' => t('Sibling terms'),
  96. 'top' => t('Top level terms'),
  97. 'synonyms' => t('Term synonyms (does not work in D7)'),
  98. 'parent' => t('Parent term(s)'),
  99. );
  100. }
  101. /**
  102. * Returns an edit form for the custom type.
  103. */
  104. function ctools_term_list_content_type_edit_form($form, &$form_state) {
  105. $conf = $form_state['conf'];
  106. $form['type'] = array(
  107. '#type' => 'radios',
  108. '#title' => t('Which terms'),
  109. '#options' => ctools_admin_term_list_options(),
  110. '#default_value' => $conf['type'],
  111. '#prefix' => '<div class="clearfix no-float">',
  112. '#suffix' => '</div>',
  113. );
  114. $form['include_current_term'] = array(
  115. '#type' => 'checkbox',
  116. '#title' => t('Include the current term in the list'),
  117. '#default_value' => !empty($conf['include_current_term']),
  118. '#prefix' => '<div class="clearfix no-float">',
  119. '#suffix' => '</div>',
  120. '#states' => array(
  121. 'invisible' => array(
  122. ':input[name="type"], unique1' => array('!value' => 'child'),
  123. ':input[name="type"], unique2' => array('!value' => 'parent'),
  124. ),
  125. ),
  126. );
  127. $form['list_type'] = array(
  128. '#type' => 'select',
  129. '#title' => t('List type'),
  130. '#options' => array('ul' => t('Unordered'), 'ol' => t('Ordered')),
  131. '#default_value' => $conf['list_type'],
  132. );
  133. $form['path'] = array(
  134. '#type' => 'textfield',
  135. '#title' => t('Path'),
  136. '#default_value' => empty($conf['path']) ? 'taxonomy/term/%tid' : $conf['path'],
  137. '#description' => t('The path to use for the terms. You may use %tid to place the term id as part of the path; if let off, it will be appended to the end.'),
  138. );
  139. return $form;
  140. }
  141. function ctools_term_list_content_type_admin_title($subtype, $conf, $context) {
  142. $options = ctools_admin_term_list_options();
  143. return t('"@s" @type', array('@s' => $context->identifier, '@type' => drupal_strtolower($options[$conf['type']])));
  144. }
  145. function ctools_term_list_content_type_edit_form_submit($form, &$form_state) {
  146. // Copy everything from our defaults.
  147. foreach (array_keys($form_state['plugin']['defaults']) as $key) {
  148. $form_state['conf'][$key] = $form_state['values'][$key];
  149. }
  150. }