Created
April 13, 2022 17:58
-
-
Save paboden/72e40964f63d590b30fafc7700b1a618 to your computer and use it in GitHub Desktop.
Drupal 8/9: Change the view mode used in taxonomy view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Implements hook_views_post_build(). | |
| */ | |
| function MYMODULE_views_post_build(ViewExecutable $view) { | |
| // Check if we are dealing with the taxonomy term view. | |
| if ($view->id() == 'taxonomy_term' && $view->current_display == 'page_1') { | |
| // Get the term ID from the current page and load up the term entity. | |
| $tid = \Drupal::routeMatch()->getRawParameter('taxonomy_term'); | |
| $term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($tid); | |
| $vocabulary = $term->bundle(); | |
| // If the current term is in vocab_one, then display rows in view_mode_one. | |
| if ($vocabulary == 'vocab_one') { | |
| $view->rowPlugin->options['view_mode'] = 'view_mode_one'; | |
| } | |
| // If the current term is in vocab_two, then display rows in view_mode_two. | |
| if ($vocabulary == 'vocab_two') { | |
| $view->rowPlugin->options['view_mode'] = 'view_mode_two'; | |
| } | |
| // If the current term is in vocab_three, and is not the "Example" term (term/11) | |
| // then display rows in view_mode_three. | |
| if ($vocabulary == 'vocab_three' && $tid != '11') { | |
| $view->rowPlugin->options['view_mode'] = 'view_mode_three'; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment