I'm trying to create a link to a page with a view that takes two arguments from the URL of the page the link is on.

This link needs to be in a block on several different pages each with a URL like this: content/city/area

Each page is a different city and area like, content/chicago/southside for example.

City and area are the arguments to be passed to the view when the link is clicked, opening a page with the view at a URL like /content/chicago/southside/buildings.

I want to use something like drupal_goto($path=[node-path]/buildings), invoked when the link button is pressed. The idea is that the token [node-path] gets the content/city/area from the URL of the current page.

Conceptually could I use javascript to invoke PHP from an onclick event?

Is there a way to do this just with jQuery or javascript?

Update: I realized js is not going to work without AJAX, but it's really much simpler to do, only I get this error. I posted this on the API site (http://api.drupal.org/api/function/drupal_get_destination/6#comment-5889) for the function "drupal_get_destination", slightly modified to return the path alias.

I have a similar use for this code but it produces a parsing error:

Parse error: syntax error, unexpected T_IF, expecting '{' in /usr/local/www/apache22/data/includes/common.inc(1695) : eval()'d code on line 3

I pasted this code into a Views Custom Field, to be displayed as a link button in a block:

function drupal_get_destination_alias() {
  if (isset($_REQUEST['destination'])) {
    return 'destination='. urlencode(drupal_get_path_alias($_REQUEST['destination']));
  else {
    // Use $_GET here to retrieve the original path in source form.
    $path = isset($_GET['q']) ? $_GET['q'] : '';
    $query = drupal_query_string_encode($_GET, array('q'));
    if ($query != '') {
      $path .= '?'. $query;
    return 'destination='. urlencode(drupal_get_path_alias($path));

$path1 = drupal_get_destination_alias();
print ("<a href=\"" . $path1 . "/buildings\" title=\"View\">View</a>");

The idea is to take the path from the page the Views block display is on, like "content/chicago/southside" and when the button is clicked, take the user to a page with a view with a path like "content/chicago/southside/buildings". "Chicago" and "southside" are passed as arguments to the view.

Shouldn't be that hard to do this, but I can't get anything I try to work.


alpritt’s picture

Using a button as a link is not really good practice and will complicate your problem. (Unless you are doing something else when you click the button.) The following should just create a link for you.

This is not the cleanest way of doing this, but probably the simplest:

$city = arg(1);
$area = arg(2);
print l('View buildings', "content/$city/$area/buildings");

or perhaps return instead of the print depending on where you are doing this.

The link will also obviously break if the url of the page isn't formed as you'd expect, so you will have to take care of that in some manner.

druplicate’s picture

It turned out that there is a bug in Views Custom Field that creates a parsing error if you try to define a function.

I just put this code in a regular block with the PHP input filter enabled and it now works:

function aarg($index = NULL, $path = NULL)
  if (!isset($path)) {
    $path = check_plain(drupal_get_path_alias($_GET['q']));
  if (!isset($arguments[$path])) {
    $arguments[$path] = explode('/', $path);
  if (!isset($index)) {
    return $arguments[$path];
  if (isset($arguments[$path][$index])) {
    return $arguments[$path][$index];

$city = aarg(1);
$area = aarg(2);
print l('View buildings', "content/$city/$area/buildings");


I got the aarg function from a comment here: http://drupal.org/node/345877 and added the checkplain function to sanitize for XSS. It's a modified version of arg() to return the path alias - arg() by itself returns "node/[nid]" and I needed the alias to feed to Views as arguments.