I'm pretty new to any coding with views and am trying to create a filter that can sit in a block. This is for the Views calendar (although my question is related to the 'core' views module).

I am using this code:

<?php
$view = views_get_view('calendar');
  $view->set_display('calendar_period_2');
  $view->init_handlers();
  $form_state = array(
    'view' => $view,
    'display' => $view->display_handler->display,
    'method' => 'get',
    'rerender' => TRUE,
    'no_redirect' => TRUE,
  );
  $output = drupal_build_form('views_exposed_form', $form_state);
  return $output;
?>

In principle, this works fine, but I need to be able to dynamically set the display to match the current active display, so that it doesn't always default to 'calendar_period_2'.

So, what I need to know is whether there is a way to access the 'display' for the currently active view?

I know there is a views_get_page_view() method, but I don't know what I can do with this.

I'd appreciate any advice.

CommentFileSizeAuthor
#2 screen.jpg109.8 KBgmak

Comments

merlinofchaos’s picture

Status: Active » Postponed (maintainer needs more info)

What do you mean current display? There isn't a current display at any point in the code you link until a view is actually executed.

gmak’s picture

StatusFileSize
new109.8 KB

Hmmm,

Maybe a screenshot will help explain (see attached).

The calendar view on the page has a 'display'. For example, the month display is calendar_period_2. But if I switch to week view, this will become calendar_period_4.

With the filter block that I have defined, I currently have the problem that the display name for the calendar is hard-coded. What I would like the filter block to be able to do is know which display is current in the calendar view, so that clicking the filter will not default back to some other view display.

Does that make sense? Is it possible?

Many thanks

gmak’s picture

I realise that I can 'expose' filters in a block and have done this, which gives me a whole set of blocks which correspond to the different 'displays'. The problem I've found is that I can't set these blocks to only appear when their corresponding 'display' is active.

If there is a way to do this, I could stop trying to figure out the above.

Is there some way of setting up some php for the block visibility that might be able to recognise which display is active?

My guess is that I need to parse the arguments, but I can't understand how to do this. The URL's look like this:

for month view (calendar_period_2) - http://example.com/calendar/2010-01?tid[0]=58
for week view (calendar_period_4) - http://example.com/calendar/2010-W01?tid[0]=58
for day view (calendar_period_3) - http://example.com/calendar/2009-12-28?tid[0]=58

in any of the above, the tid could be from none to 4 values based on the filter.

Again, any advice would be greatly appreciated.

esmerel’s picture

Status: Postponed (maintainer needs more info) » Active
dawehner’s picture

Status: Active » Fixed
$page_view = views_get_page_view();
$display_id = $page_view->current_display;

should do it.

Status: Fixed » Closed (fixed)

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

MissterX’s picture

I got NULL.

This worked for me:

$c_v = views_get_current_view();
$c_d = $c_v->current_display;
$current_display = $c_v->display[$c_d]->display_title;

That is when I use it in a overriden theme file such as views-view-fields--page.tpl.php

$c_d contains the machine name while $current_display is the title I gave the page in the views UI.