vocabulary_terms.inc 3.3 KB

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