6

I have searched and not been able to find anything related to this specific situation.

I would like to show specific views for certain combinations of taxonomy terms in Drupal 7.

I have three different taxonomies each with different terms (I have changed the terms here to make them easier to understand):

Fruit
  - Apples
  - Oranges
  - Bananas

Numbers
  - 1
  - 2
  - 3

Letters
  - a
  - b
  - c

Using these terms I already have a view that shows all items tagged with specific terms based on url such as:

/fruit/apples/1/a
/fruit/apples/2/c
/fruit/bananas/3/b

I achieved this with a view which uses a url in the form of /fruit/%/%/% and multiple contextual filters of type 'Has taxonomy term ID'.

So far so good.

What I would like to do is have different view displays that show for specific urls/combinations of terms:

/fruit/apples/1/a  - display 1
/fruit/apples/1/b  - display 2
/fruit/bananas     - display 3
/fruit/oranges/1/a - display 1
/fruit/oranges/1/b - display 2

Does anyone know how I can achieve this or have any alternate solutions/ideas?

7
+25

In a custom module you could implement the hook hook_views_pre_view()

e.g.

function my_module_views_pre_view(&$view, &$display_id, &$args) {

    if ($view->name == 'the_view') {
        // if url matches certain pattern
        $view->set_display('your display');
    }

}
  • I think this should work. But am not sure if it is late in the request cycle. But worth trying. @Tims please update if this works for you. – Gokul N K Jun 22 '15 at 5:47
  • Thanks @Gokul. This contains some of the same info provided by Scott but not as targeted as his answer. – Tims Jun 22 '15 at 6:50
  • Hi @Tims, did you end up implementing any of these answers? – Scott Anderson Jun 29 '15 at 12:36
3

There is also another solution. You can use context module.

Context allows you to manage contextual conditions and reactions for different portions of your site. You can think of each context as representing a "section" of your site. For each context, you can choose the conditions that trigger this context to be active and choose different aspects of Drupal that should react to this active context.

Think of conditions as a set of rules that are checked during page load to see what context is active. Any reactions that are associated with active contexts are then fired.

After installing module and adding new context use Path as a condition and Template suggestions as a reaction.

In the Path you can define wildcards (*) so for every path pattern you can define special Template suggestions. As an example:

enter image description here

1

What about just use different views template to customise the output?

In your theme template.php, add:

function my_theme_preprocess_views_view(&$vars){
  if($vars['view']->name == 'my_view_name'){
    if($vars['view']->args[1]==1&&$vars['view']->args[2]=='a'){
      $vars['theme_hook_suggestions'][] = 'views_view__my_view_name_display_1__page';
    }elseif($vars['view']->args[1]==1&&$vars['view']->args[2]=='b'){
      $vars['theme_hook_suggestions'][] = 'views_view__my_view_name_display_2__page';
    }elseif(!isset($vars['view']->args[1])){
      $vars['theme_hook_suggestions'][] = 'views_view__my_view_name_display_3__page';
    }
  }
}

Then add those template file, like 'views-view--my_view_name_display_1--page.tpl.php' to customise the different output.

0

Another option if you want to use the UI would be to use Page Manager. You can create the /fruit/%/%/%/ path and use the arguments to choose which variant of the page to display (using selection criteria).

Each variant could simply show a different view display, or could be wildly different including layout if you so desire.

You needn't use the 'panel pane' display in views but that can make it easier to pass the arguments (once you've learned how to use it).

0

You can try hook_views_query_alter

Alter the query before executing the query.

This hook should be placed in MODULENAME.views.inc and it will be auto-loaded. MODULENAME.views.inc must be in the directory specified by the 'path' key returned by MODULENAME_views_api(), or the same directory as the .module file, if 'path' is unspecified.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.