Список вложенных терминов таксономии

Задача: создать список вложенных терминов таксономии, для отображения в блоке

Создаем вьюху методом импорта. Написаное актуально для Drupal 7

  1. $view = new view;
  2. $view->name = 'child_terms';
  3. $view->description = '';
  4. $view->tag = 'default';
  5. $view->base_table = 'taxonomy_term_data';
  6. $view->human_name = 'Child Terms';
  7. $view->core = 7;
  8. $view->api_version = '3.0-alpha1';
  9. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  10.  
  11. /* Display: Master */
  12. $handler = $view->new_display('default', 'Master', 'default');
  13. $handler->display->display_options['title'] = '%1 Sub-Categories';
  14. $handler->display->display_options['access']['type'] = 'perm';
  15. $handler->display->display_options['cache']['type'] = 'none';
  16. $handler->display->display_options['query']['type'] = 'views_query';
  17. $handler->display->display_options['query']['options']['query_comment'] = FALSE;
  18. $handler->display->display_options['exposed_form']['type'] = 'basic';
  19. $handler->display->display_options['pager']['type'] = 'none';
  20. $handler->display->display_options['pager']['options']['offset'] = '0';
  21. $handler->display->display_options['style_plugin'] = 'list';
  22. $handler->display->display_options['style_options']['class'] = 'sub-cat';
  23. $handler->display->display_options['row_plugin'] = 'fields';
  24. /* Relationship: Taxonomy term: Parent term */
  25. $handler->display->display_options['relationships']['parent']['id'] = 'parent';
  26. $handler->display->display_options['relationships']['parent']['table'] = 'taxonomy_term_hierarchy';
  27. $handler->display->display_options['relationships']['parent']['field'] = 'parent';
  28. $handler->display->display_options['relationships']['parent']['required'] = 0;
  29. /* Field: Taxonomy term: Name */
  30. $handler->display->display_options['fields']['name_1']['id'] = 'name_1';
  31. $handler->display->display_options['fields']['name_1']['table'] = 'taxonomy_term_data';
  32. $handler->display->display_options['fields']['name_1']['field'] = 'name';
  33. $handler->display->display_options['fields']['name_1']['label'] = '';
  34. $handler->display->display_options['fields']['name_1']['alter']['alter_text'] = 0;
  35. $handler->display->display_options['fields']['name_1']['alter']['make_link'] = 0;
  36. $handler->display->display_options['fields']['name_1']['alter']['absolute'] = 0;
  37. $handler->display->display_options['fields']['name_1']['alter']['external'] = 0;
  38. $handler->display->display_options['fields']['name_1']['alter']['replace_spaces'] = 0;
  39. $handler->display->display_options['fields']['name_1']['alter']['nl2br'] = 0;
  40. $handler->display->display_options['fields']['name_1']['alter']['word_boundary'] = 1;
  41. $handler->display->display_options['fields']['name_1']['alter']['ellipsis'] = 1;
  42. $handler->display->display_options['fields']['name_1']['alter']['strip_tags'] = 0;
  43. $handler->display->display_options['fields']['name_1']['alter']['trim'] = 0;
  44. $handler->display->display_options['fields']['name_1']['alter']['html'] = 0;
  45. $handler->display->display_options['fields']['name_1']['element_label_colon'] = FALSE;
  46. $handler->display->display_options['fields']['name_1']['element_default_classes'] = 1;
  47. $handler->display->display_options['fields']['name_1']['hide_empty'] = 1;
  48. $handler->display->display_options['fields']['name_1']['empty_zero'] = 1;
  49. $handler->display->display_options['fields']['name_1']['link_to_taxonomy'] = 1;
  50. /* Sort criterion: Taxonomy term: Name */
  51. $handler->display->display_options['sorts']['name']['id'] = 'name';
  52. $handler->display->display_options['sorts']['name']['table'] = 'taxonomy_term_data';
  53. $handler->display->display_options['sorts']['name']['field'] = 'name';
  54. $handler->display->display_options['sorts']['name']['order'] = 'DESC';
  55. /* Contextual filter: Taxonomy term: Term ID */
  56. $handler->display->display_options['arguments']['tid']['id'] = 'tid';
  57. $handler->display->display_options['arguments']['tid']['table'] = 'taxonomy_term_data';
  58. $handler->display->display_options['arguments']['tid']['field'] = 'tid';
  59. $handler->display->display_options['arguments']['tid']['relationship'] = 'parent';
  60. $handler->display->display_options['arguments']['tid']['default_action'] = 'default';
  61. $handler->display->display_options['arguments']['tid']['exception']['title_enable'] = 1;
  62. $handler->display->display_options['arguments']['tid']['exception']['title'] = '%1 Sub-Categories';
  63. $handler->display->display_options['arguments']['tid']['default_argument_type'] = 'taxonomy_tid';
  64. $handler->display->display_options['arguments']['tid']['default_argument_options']['term_page'] = 1;
  65. $handler->display->display_options['arguments']['tid']['default_argument_options']['node'] = 0;
  66. $handler->display->display_options['arguments']['tid']['default_argument_options']['limit'] = 0;
  67. $handler->display->display_options['arguments']['tid']['default_argument_skip_url'] = 0;
  68. $handler->display->display_options['arguments']['tid']['summary']['number_of_records'] = '0';
  69. $handler->display->display_options['arguments']['tid']['summary']['format'] = 'default_summary';
  70. $handler->display->display_options['arguments']['tid']['summary_options']['items_per_page'] = '25';
  71. $handler->display->display_options['arguments']['tid']['specify_validation'] = 1;
  72. $handler->display->display_options['arguments']['tid']['validate']['type'] = 'taxonomy_term';
  73. $handler->display->display_options['arguments']['tid']['validate_options']['vocabularies'] = array(
  74.   'product_categories' => 0,
  75.   'categories' => 0,
  76.   'tags' => 0,
  77.   'plant_types' => 0,
  78.   'health_categories' => 0,
  79.   'faq_category' => 0,
  80.   'gallery_collections' => 0,
  81.   'newsletter' => 0,
  82.   'sources' => 0,
  83. );
  84. $handler->display->display_options['arguments']['tid']['validate_options']['type'] = 'tids';
  85. $handler->display->display_options['arguments']['tid']['validate_options']['transform'] = 0;
  86. $handler->display->display_options['arguments']['tid']['break_phrase'] = 1;
  87. $handler->display->display_options['arguments']['tid']['not'] = 0;
  88.  
  89. /* Display: Block */
  90. $handler = $view->new_display('block', 'Block', 'block');
  91. $handler->display->display_options['block_caching'] = '4';
  92.  
  93. /* Display: Block2 */
  94. $handler = $view->new_display('block', 'Block2', 'block_1');
  95. $translatables['child_terms'] = array(
  96.   t('Master'),
  97.   t('%1 Sub-Categories'),
  98.   t('more'),
  99.   t('Apply'),
  100.   t('Reset'),
  101.   t('Sort by'),
  102.   t('Asc'),
  103.   t('Desc'),
  104.   t('Parent'),
  105.   t('Block'),
  106.   t('Block2'),
  107. );

Еще один вариант, так же рабочий, отличается тем, что id термина берет из url php сниппетом drupal. В принципе тоже самое, кому что больше нравится…

  1. $view = new view();
  2. $view->name = 'child_list';
  3. $view->description = '';
  4. $view->tag = 'default';
  5. $view->base_table = 'taxonomy_term_data';
  6. $view->human_name = 'Список вложенных терминов';
  7. $view->core = 7;
  8. $view->api_version = '3.0';
  9. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  10.  
  11. /* Display: Master */
  12. $handler = $view->new_display('default', 'Master', 'default');
  13. $handler->display->display_options['use_more_always'] = FALSE;
  14. $handler->display->display_options['use_more_text'] = 'ещё';
  15. $handler->display->display_options['access']['type'] = 'perm';
  16. $handler->display->display_options['cache']['type'] = 'time';
  17. $handler->display->display_options['cache']['results_lifespan'] = 'custom';
  18. $handler->display->display_options['cache']['results_lifespan_custom'] = '86400';
  19. $handler->display->display_options['cache']['output_lifespan'] = 'custom';
  20. $handler->display->display_options['cache']['output_lifespan_custom'] = '86400';
  21. $handler->display->display_options['query']['type'] = 'views_query';
  22. $handler->display->display_options['exposed_form']['type'] = 'basic';
  23. $handler->display->display_options['exposed_form']['options']['submit_button'] = 'Применить';
  24. $handler->display->display_options['exposed_form']['options']['reset_button_label'] = 'Сбросить';
  25. $handler->display->display_options['exposed_form']['options']['exposed_sorts_label'] = 'Сортировать по';
  26. $handler->display->display_options['exposed_form']['options']['sort_asc_label'] = 'По возрастанию';
  27. $handler->display->display_options['exposed_form']['options']['sort_desc_label'] = 'По убыванию';
  28. $handler->display->display_options['pager']['type'] = 'none';
  29. $handler->display->display_options['pager']['options']['offset'] = '0';
  30. $handler->display->display_options['style_plugin'] = 'default';
  31. $handler->display->display_options['row_plugin'] = 'fields';
  32. /* Связь: Термин таксономии: Родительский термин */
  33. $handler->display->display_options['relationships']['parent']['id'] = 'parent';
  34. $handler->display->display_options['relationships']['parent']['table'] = 'taxonomy_term_hierarchy';
  35. $handler->display->display_options['relationships']['parent']['field'] = 'parent';
  36. /* Поле: Термин таксономии: Имя */
  37. $handler->display->display_options['fields']['name']['id'] = 'name';
  38. $handler->display->display_options['fields']['name']['table'] = 'taxonomy_term_data';
  39. $handler->display->display_options['fields']['name']['field'] = 'name';
  40. $handler->display->display_options['fields']['name']['label'] = '';
  41. $handler->display->display_options['fields']['name']['alter']['word_boundary'] = FALSE;
  42. $handler->display->display_options['fields']['name']['alter']['ellipsis'] = FALSE;
  43. $handler->display->display_options['fields']['name']['link_to_taxonomy'] = TRUE;
  44. /* Контекстный фильтр: Термин таксономии: ID термина */
  45. $handler->display->display_options['arguments']['tid']['id'] = 'tid';
  46. $handler->display->display_options['arguments']['tid']['table'] = 'taxonomy_term_data';
  47. $handler->display->display_options['arguments']['tid']['field'] = 'tid';
  48. $handler->display->display_options['arguments']['tid']['relationship'] = 'parent';
  49. $handler->display->display_options['arguments']['tid']['default_action'] = 'default';
  50. $handler->display->display_options['arguments']['tid']['exception']['title'] = 'Все';
  51. $handler->display->display_options['arguments']['tid']['default_argument_type'] = 'php';
  52. $handler->display->display_options['arguments']['tid']['default_argument_options']['code'] = 'return(arg(2));';
  53. $handler->display->display_options['arguments']['tid']['summary']['number_of_records'] = '0';
  54. $handler->display->display_options['arguments']['tid']['summary']['format'] = 'default_summary';
  55. $handler->display->display_options['arguments']['tid']['summary_options']['items_per_page'] = '25';
  56.  
  57. /* Display: Блок */
  58. $handler = $view->new_display('block', 'Блок', 'block_1');
  59. $translatables['child_list'] = array(
  60.   t('Master'),
  61.   t('ещё'),
  62.   t('Применить'),
  63.   t('Сбросить'),
  64.   t('Сортировать по'),
  65.   t('По возрастанию'),
  66.   t('По убыванию'),
  67.   t('Родитель'),
  68.   t('Все'),
  69.   t('Блок'),
  70. );
Теги: