vocabulary_terms.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. if (module_exists('taxonomy')) {
  3. /**
  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('Vocabulary terms'),
  10. 'icon' => 'icon_vocabulary.png',
  11. 'description' => t('All the terms in a vocabulary.'),
  12. 'required context' => new ctools_context_required(t('Vocabulary'), 'entity:taxonomy_vocabulary'),
  13. 'category' => t('Vocabulary'),
  14. 'defaults' => array('max_depth' => 0, 'tree' => 1),
  15. );
  16. }
  17. /**
  18. * Output function for the 'vocabulary terms' content type. Outputs a
  19. * list of terms for the input vocabulary.
  20. */
  21. function ctools_vocabulary_terms_content_type_render($subtype, $conf, $panel_args, $context) {
  22. $vocab = isset($context->data) ? clone $context->data : NULL;
  23. $max_depth = (!empty($conf['max_depth']) ? (int)$conf['max_depth'] : NULL);
  24. if ($conf['tree'] == FALSE) {
  25. $terms = taxonomy_get_tree($vocab->vid, 0, $max_depth);
  26. $items = array();
  27. foreach ($terms as $term) {
  28. $items[] = l($term->name, 'taxonomy/term/' . $term->tid);
  29. }
  30. $output = theme('item_list', array('items' => $items));
  31. }
  32. else {
  33. $output = theme('item_list', array('items' => _ctools_content_vocabulary_terms($vocab->vid, $max_depth)));
  34. }
  35. $block = new stdClass();
  36. $block->module = 'node_type';
  37. $block->title = check_plain($vocab->name);
  38. $block->content = $output;
  39. $block->delta = $vocab->vid;
  40. return $block;
  41. }
  42. function _ctools_content_vocabulary_terms($vid, $max_depth, $depth = -1, $tid = 0) {
  43. $depth++;
  44. if ($max_depth != NULL && $depth == $max_depth) {
  45. return array();
  46. }
  47. $return = array();
  48. $query = db_select('taxonomy_term_data', 't')->fields('t', array('tid'));
  49. $query->join('taxonomy_term_hierarchy', 'h', ' t.tid = h.tid');
  50. $query->condition('t.vid', $vid)
  51. ->condition('h.parent', $tid)
  52. ->orderBy('t.weight')
  53. ->orderBy('t.name');
  54. $tids = $query->execute()->fetchCol();
  55. $terms = taxonomy_term_load_multiple($tids);
  56. foreach ($terms as $term) {
  57. $return[] = array(
  58. 'data' => l($term->name, 'taxonomy/term/' . $term->tid),
  59. 'children' => _ctools_content_vocabulary_terms($vid, $max_depth, $depth, $term->tid),
  60. );
  61. }
  62. return $return;
  63. }
  64. function ctools_vocabulary_terms_content_type_admin_title($subtype, $conf, $context) {
  65. return t('"@s" terms', array('@s' => $context->identifier));
  66. }
  67. function ctools_vocabulary_terms_content_type_edit_form($form, &$form_state) {
  68. $conf = $form_state['conf'];
  69. $form['max_depth'] = array(
  70. '#type' => 'select',
  71. '#title' => t('Maximum depth'),
  72. '#options' => array_merge(array(t('unlimited')), range(1, 9)),
  73. '#default_value' => $conf['max_depth'],
  74. '#description' => t('Define the maximum depth of terms being displayed.'),
  75. );
  76. $form['tree'] = array(
  77. '#type' => 'checkbox',
  78. '#title' => t('Display as tree'),
  79. '#default_value' => $conf['tree'],
  80. '#description' => t('If checked, the terms are displayed in a tree, otherwise in a flat list.'),
  81. );
  82. return $form;
  83. }
  84. function ctools_vocabulary_terms_content_type_edit_form_submit($form, &$form_state) {
  85. // Copy everything from our defaults.
  86. foreach (array_keys($form_state['plugin']['defaults']) as $key) {
  87. $form_state['conf'][$key] = $form_state['values'][$key];
  88. }
  89. }