I am trying to get a page view to show different information in the header depending on the arguments. How would I get the view args data via a php script in the header?

I would perfer not to use the args() function, as that seems too much of a hack, and would break if I ever use this view in a block.

Comments

merlinofchaos’s picture

Oh crap, I didn't restore the global variable stuff from Views 1. I will put that back in, since the only way to get data to the PHP filter is via globals.

merlinofchaos’s picture

Status: Active » Fixed

Ok, finally checked this in -- views_set_current_view is now utilized during preview and execute_display; that should make it easy to use $view = views_get_current_view() during headers.

RoboPhred’s picture

Status: Fixed » Active

Re-opening the support request, I am unable to use this:

$view = views_get_current_view();
print (isset($view))?'TRUE':'FALSE';

The above code always prints false when set in the view header.

RoboPhred’s picture

I did some debugging. While the $cache variable in views_set_current_view is set properly when called, it fails "isset" on the second call from views_get_current_view.

It looks like something is clearing it, but it is only called twice: one for the set, and one for the get.

RoboPhred’s picture

I found the problem, it looks like the unset statement in views_set_current_view is removing the 'static' property of the variable.
The following code replacement works (line 3 commented out):

  if (isset($view)) {
    // be really safe with references here.
    //unset($cache);
    $cache = $view;
  }

Of course, you have the warning about references, I don't really know if this will cause Untold Bad Things to happen.

PHP confirms this: http://us3.php.net/unset

If a static variable is unset() inside of a function, unset() destroys the variable only in the context of the rest of a function. Following calls will restore the previous value of a variable.

merlinofchaos’s picture

Oh well hopefully the static nature will also cause it not to overwrite references then.

merlinofchaos’s picture

Status: Active » Fixed

Ok, let's see if that does the trick.

RoboPhred’s picture

Status: Fixed » Active

It broke again...

You changed it to

$cache = &$view;

which isn't working on my site.

I changed it back to

$cache = $view;

and it started working again.

PHP really doesn't like this area of the module...

merlinofchaos’s picture

Status: Active » Fixed

Bah! More reference pain. :/

Ah well. This will just have to have problems under PHP4, and that's all there is to it.

Anonymous’s picture

Status: Fixed » Closed (fixed)

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

greg@beargroup.com’s picture

Version: 6.x-2.x-dev » 6.x-2.0-beta4
Status: Closed (fixed) » Active

Running into this on Beta4, running PHP 5.1

Have some themes that make decisions on the view id

This has no value

global $current_view;

This returns true - but the print_r is empty

$view = views_get_current_view();
print (isset($view))?'TRUE':'FALSE';
print_r($view);

Thanks,
-Greg

merlinofchaos’s picture

Status: Active » Fixed

It looks like attachments could unset the view early. I checked in a fix to -dev.

Anonymous’s picture

Status: Fixed » Closed (fixed)

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