term_list.inc 5.2 KB

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