1

I asked a similar question about a month or so ago, and the answer was to use the Views Modes module. That module required the PURL module. The problem is the PURL module leaves parameters on your URL all over the site, and have not figured out a way to turn it off. Others have posted a support issue and it goes ignored.

So, I'm back to the drawing board. I have nodes that are rendered, using view modes defined in Display Suite. Is there a way I can toggle between view modes in Views? What I'm really trying to do is provide a bunch of filter checkboxes, along with the ability to toggle between view modes, but I need to find a solution for the view modes first.

Is there a module that makes this easy, or do I need to write some code. If I do need to write some code, how should I approach it?

11
  • Do you mean if you display 3 nodes in a view then you want to show 2 of them with view mode A and the 3rd with view mode B ? Depending on a field value in the node?
    – donquixote
    Commented Nov 13, 2013 at 18:35
  • Should the view mode depend on a field value or other entity property, or should it depend on the position in the view? (e.g. zebra striping). That's an important distinction.
    – donquixote
    Commented Nov 13, 2013 at 18:41
  • No, they all would display in the same view mode. I want the toggle functionality that you find at the top right of a lot of sites, where you can, for example, switch between a grid view and a list view of the results.
    – vintorg
    Commented Nov 13, 2013 at 21:00
  • It would, from the UI standpoint, depend on if they click an icon that determines the view modes of the results.
    – vintorg
    Commented Nov 13, 2013 at 21:01
  • Then I would make it two separate Views displays. And some mechanic to switch between them. Either two separate urls, or some ajax magic.
    – donquixote
    Commented Nov 13, 2013 at 21:10

4 Answers 4

2
+50

Solution that I use sometimes.

  1. Create a content type with one or more multiple-value block reference fields.
    This is like a "cheap" version of panels..
  2. Create your views displays as blocks. One for each version.
  3. Create a custom module to define a block that acts as the "switcher" between the two. Put some dummy content for now.
  4. Create two nodes with the block reference.
    • One that has the switcher and the Views display A.
    • One that has the switcher and the Views display B.
  5. Set url aliases for both nodes.
  6. Flesh out the "switcher" block. E.g. make it keep the url query parameters. Make it look different depending on whether you are on A or B.
  7. If you want, you can have a separate block for the exposed filters, that you also add via the blockreference.
  8. Use Display suite or some other way to arrange the different blocks on the page.

The benefit of block reference + ds over theme regions is that you don't spam your theme regions with one-page-only blocks.

7
  • Boy... that's ugly. I'm wondering if it would make sense to use the ajax views API and just do it in code. I'm using Display Suite. I'd rather write a cleaner solution and hand it off or post it in sandbox. Can you tell me where I should start?
    – vintorg
    Commented Nov 13, 2013 at 23:19
  • And still I get the bounty? I think I can come up with something better :) The block reference is still my favorite solution, but for other reasons than your requirements. So it might be distracting.
    – donquixote
    Commented Nov 13, 2013 at 23:30
  • I generally find nodes easier to work with than page views.
    – donquixote
    Commented Nov 13, 2013 at 23:34
  • Yeah... is there no way to signal the nodes to render using another view mode, defined by DS? Is there a way to do it via AJAX?
    – vintorg
    Commented Nov 13, 2013 at 23:36
  • Ah.. the ajax is important to you? Having the views on different urls is not an option?
    – donquixote
    Commented Nov 13, 2013 at 23:38
1

Just an idea and I might be mis-understanding what you need here but could you create multiple views, one for each view you need, expose the filters on them and then combine all these views with the quicktabs module?

7
  • The issue is it can't be part of the exposed block of filters, because the filters need to be on the left, and the view mode toggle at the top right somewhere. As I understand it, that can't be done because that would break up the form element.
    – vintorg
    Commented Nov 12, 2013 at 22:24
  • yeah my bad, I was mistaken about what you were trying to do. Dont know about the toggling of views modes but better exposed filters might help with the other part. which isnt what you asked but.... Commented Nov 12, 2013 at 23:04
  • Yeah, I think I am going to try Better Exposed Filters. I just need to figure out this view modes thing. If I have to do it in code, that's fine, I just need someone to point me in the right direction.
    – vintorg
    Commented Nov 13, 2013 at 0:12
  • I have just been looking into Display Suite and in the configuration page of Display suite extras (admin/structure/ds/list/extras) under the Other tab there is a "View Mode Switcher" check box. Could this help? Commented Nov 14, 2013 at 15:26
  • not sure... I couldn't figure out what that was for... I'm going to look further...
    – vintorg
    Commented Nov 14, 2013 at 20:04
1

So, it seems the general idea will be to have multiple views displays, and something to switch between those views displays.

The problem seems to be

  1. How to create the switcher.
  2. How to display the switcher in the intended location next to the exposed filters.

One solution could be hook_form_views_exposed_form_alter() to sneak the switcher into the form.

EDIT: Another solution can be to put the switcher as custom html in the View header. Or you can write your own Views header plugin to have it reusable.

1
  • No. The switcher can be located anywhere. I can render the switcher. The requirement is it can't be bound with the exposed filters.
    – vintorg
    Commented Nov 13, 2013 at 23:12
0

What about creating a custom select that appends arguments to the current url, and then implementing hook_entity_view_mode_alter and checking the paths?

function mymodule_entity_view_mode_alter(&$view_mode, $context) {
  if ($context['entity_type'] == 'node') {
    if (arg(0) == 'myview' && arg(1) == 'mode' && arg(2) == 'custom') {
      $view_mode = 'custom_view_mode';
    }
  }
}

There must be a cleaner way to do this. But this should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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