I'm trying to change displays for certain tids, so that some tids use teasers and others use fields. I'm thinking that the proper place to do this is in the argument handling code.

Would something like this work?

$view->execute_display('my_new_display', $args);

I've tried

$display_id = 'my_new_display';
$view->set_display($display_id);

with no results.

Sorry to post here, but after endless searching I can't find anyone trying to switch views displays programatically. Note that my_new_display is a page type display, using the same path taxonomy/term/% as the display called "Page".

Any pointers? Thank you in advance.

Comments

916Designs’s picture

What I'm trying to do:

I have a large site with a location (not the module) based hierarchical vocabulary. Terms with less depth are book pages, for which I would like to use teasers. Terms with more depth are cck nodes for which it is more appropriate to use fields in a table.

The problem is that all of this content is accessed purely with taxonomy... I need to either change the views display programmatically , or dynamically switch to and display a different view- based on the depth of the tid found in the argument handling code. Is there a way to switch to another view once a first view has started executing? Switch displays?

merlinofchaos’s picture

Status: Active » Fixed

No, you can't use execute_display like that in the argument handling code. In order to get a different display based upon an argument, you have to either write an access plugin (currently fairly difficult though hopefully there will be a writeup of how to do this eventually) and create multiple displays with the same path and use access control to do it, or you need to write your own menu handler (This is frankly easier right now) and pick the right display based upon the argument and display the view yourself.

klaasvw’s picture

I had a similar problem where I had to switch view displays. I had a view set up that used a page views as the "root" display and a bunch of other page views as "sub displays". The intention of the sub displays was simply to provide some basic alternatives for sorting or filtering the view without the need of a table style and/or exposed filters, or without resorting to multiple views.

Anyway, here's how I did the display switching:

I created a mymodule.views.inc file and implemented the hook_views_pre_view hook in it. Inside it you simply call $view->set_display('name of your display'); when you want to switch the display.

So you would need to write your own tiny module ans implement the hook_views_pre_view hook, that's all there is to it.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

cluther’s picture

Regarding your solution for creating additional view displays.... I have previously created custom content types and displays using CCK and Views. But now I have a desire to create a custom node type with multiple alternative node displays (other than node.tpl.php). Your recommendation for creating a mymodule.views.inc file and implementing hooks_views_pre_views hook is at the edge of my current understanding. Can you elaborate more?

giorgio79’s picture

And #3 works to this day, even in Drupal 7. Fabulous. Thanks merlinofchaos and every other Views developer!!! :)