Hi

I know it must be very easy but I can't figure how to add view arguments into the view header.

I simply write:

<p>The argument passed is: <?php print args[0]; ?></p>

and it doesn't work.

I'm trying to override the default taxonomy terms view with my own and I would like to put the term passed as argument in the title.

Thanks in advance for your help.

Comments

merlinofchaos’s picture

Status: Active » Fixed

Because the header uses the default Drupal PHP interpreter, Views variables aren't available there like they are in the arg php code. You can get them by doing


global $current_view;
print $current_view->args[0];

NoRandom’s picture

It worked, thanks a lot for the information.

Anonymous’s picture

Status: Fixed » Closed (fixed)

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

pcave’s picture

How is this accomplished in drupal 6 with views 2?

pcave’s picture

Bump

a_c_m’s picture

bump/subscribe

clivesj’s picture

Version: 5.x-1.6 » 6.x-2.2

If have dumped the view object to see where to grab the arguments, but it seems like the arguments-value is not populated:

view Object ( 
  [db_table] => views_view 
  [base_table] => node 
  [built] => 1 
  [executed] => 1 
  [args] => Array ()

I think $current_view->arg[n] should lead to the arguments. If so, this might be a bug and we should open a seperate issue.
I will investigate further and report here if I find anything.
Anyone?

btw: For the time being I will grab the arguments from the URL using drupal arg() function.

clivesj’s picture

This will work:

  $view = views_get_current_view();
  // [0] is first arg, [1] is second etc.
  $new_var = $view-> args[0];
  echo $new_var;
OneTwoTait’s picture

Works really well. :)

But, how do we capitalize the first letter of the results?

alienzed’s picture

Should it be possible to do the following in a view header?

//retrieve current view and set items per page
$view = views_get_current_view();
$view->set_display('default');
$view->display_handler->set_option('items_per_page', 10);

I know that kind of thing works when embedding a view, but it doesn't seem to have any effect.
If that isn't possible, how could I allow a user to choose the items per page without using a different display or embedding the view into another page?
I COULD embed the view but then paths and arguments become a lot more complicated to use properly.

mohammed j. razem’s picture

@taite11

echo ucwords($view->arg[0]);
dawehner’s picture

mh i think $view should be editable see

<?php
    $view = &$this->view;
    $argument = &$this->argument;
    ob_start();
    $result = eval($this->argument->options[$this->option_name]);
?>
OneTwoTait’s picture

I just tried using that instead and it didn't output anything.

weber01’s picture

Where do you type the php code? I cannot type php into the header, it is formated html

Thanks

Anonymous’s picture

weber01 - Have you checked that the PHP filter module is enabled and you have the necessary permissions ?

Marko B’s picture

#8 works great in d6, saved my day :-)

zkrebs’s picture

#8 worked, but for PHP noobs here's a snippet on how to add html to the result - I am using this to show the letter we're browsing by in a view, which is a views attachment

<?php
  
  $view = views_get_current_view();
  // [0] is first arg, [1] is second etc.
  $new_var = $view-> args[0];
  echo "<span style=\"text-align:center;\"><h1>Browsing Articles that begin with <span style=\"text-transform: uppercase;\">\"$new_var\"</span></h1></span>";
?>

JRF’s picture

Issue summary: View changes

In the header of a view, is it possible to get the fields from an entity reference passed as an argument ? And if so, does someone know what php code should I write ?

In my case, the argument (or contextual filter) of the view is a taxonomy term. So I would like to get the description (or body) field of this taxonomy term in the views header.

manmohandream’s picture

#8 worked, Worked for me.
Thanks clivesj

webt’s picture

Try
<?php print_r ($view->args); ?>