7

Is it possible to make URL arguments, e.g.

http://mysite.com/arg1/arg2/arg3

available as tokens in the rewrite results settings for a field in views e.g. for the above these would be available for use (among other existing tokens) as:

[arg1] [arg2] [arg3]

Background

I am developing Views using OpenLayers maps with filtered items place in an overlay on them. When the user interacts with the map to remove items, I want to retain the position of the map (an not have it revert to a default zoom and position) when the page reloads. By being able to access the URL arguments i.e. longitude and latitude, I can then rewrite the filter URL so that these are sent back to the page.

But I'm sure there are other scenarios that don't use maps that people would find answers to the question useful for.

6

If you do not want to actually use a contextual filter, you can use a "Global: Null" contextual filter, use the Contextual filters options to assign the value from the argument position you are interested in and then that becomes available as a %1/!1 replacement token without actually affecting the query.

| improve this answer | |
4

If arg2 and arg3 are, or can be, arguments/contextual filters in the View in question, you should be able to use !1 and !2 in the rewrite.

I am ignoring arg1 as that really can't easily be dynamic.

ADDITION:

This could be another case of my assuming D7 hasn't gotten rid of D6 features, but a quick screen grab here (I don't have a D7 testbed handy right at this sec, sorry) shows this as part of rewrite when arguments are present:

enter image description here

This is a quick View with two arguments, a node nid and type, and when I rewrite this field (the title), I can put in Hello [title] where nid is !1 and type is !2 and it works.

2ND ADDITION:

Even though this answer no longer is valid to the issue at hand (see abounding comments on both answers), @Molot did confirm that these replacement patterns are available in D7:

enter image description here

so perhaps this and his answer will help someone else looking out there.

| improve this answer | |
  • Jimajamma so would I, for example, put !2 or [!2] directly in the text box for 'rewrite this field' - as in: i44.tinypic.com/4rrgy0.png - this doesn't seem to work, as this screen shot shows - !2 or [!2] is output. By the way I am (necessarily) using aggregation so don't know whether this is interfering - see screenshot: i44.tinypic.com/2vmc84m.png Reason for aggregation here: drupal.stackexchange.com/a/79064/1082 (thanks for your input so far Jimajamma) – therobyouknow Jul 12 '13 at 13:24
  • I've never seen the !2 or !3 notation before as tokens in the rewrite results for a view, I have to admit though I hope it works with some advice on where I am going wrong with using it as per last comment. – therobyouknow Jul 12 '13 at 13:32
  • 3
    i.stack.imgur.com/e5X2y.png - screenshot from D7 - works. NID is forst contextual filter there – Mołot Jul 12 '13 at 14:09
  • Thanks Mołot (+1) and Jimajamma - this has now started to appear and I'll experiment further to confirm - I think something is a bit 'flakey' with my setup as these did not show before. If all's well I would like to accept Jimajamma's answer (thanks Mołot - I'll upvote all your posts, when I come back to confirm...) – therobyouknow Jul 12 '13 at 14:32
  • 1
    Ah. Well let us all know if we can take another whack at it. If it is something like www.yourhost.com/some/path#latlong the #latlong is in fact being sent to the backend, that's an anchor tag, it will just be basically ignored if it doesn't exist on the server (but it certainly can be seen by drupal). Cheers! – Jimajamma Jul 12 '13 at 20:35
3

Assuming arguments you want to use are not views arguments or contextual filters, if you wantthem as tokens, you basically need to create a module that implements hook_tokens() and hook_token_info(), as described in the API. I don't think anyone did it already.

Here is how I'd do it:

function yourmodule_token_info() {
  $type = array(
    'name' => t('GET'),
    'description' => t('Tokens related to $_GET superglobal.'),
  );
  $tokens = array();
  foreach($_GET as $key => $val) { //discard $val now
    $tokens[$key] = array(
      'name' => $key,
      'description' => "\$_GET['$key']",
    );
  }
  return array(
    'types' => array('get' => $type),
    'tokens' => array('get' => $tokens),
  );
}

function yourmodule_tokens($type, $tokens, array $data = array(), array $options = array()) {
  if($type == 'get') {
    $replacements = array();
    foreach ($tokens as $name => $original) {
        $replacements[$original] = (string)$_GET[$name];
    }
    return $replacements;
  }
}

Of course code above is not production ready, it ignores proper type handling, security issues and so on. It's meant to set you on the right track, but you need to apply your own sanity before deploying it. I take no responsibility for abuse. Read about the reasons it was removed in the first place before you decide to add it back, and be careful to restore only what you really need.

If you want parameters accesible by arg() instead of $_GET, of course in hook_tokens iterate over them with while:

function yourmodule_tokens($type, $tokens, array $data = array(), array $options = array()) {
  if($type == 'arg') {
    $replacements = array();
    $i = 0;
    while ( NULL !== ($arg = arg($i))) {
        $replacements["arg:$i"] = (string)$arg;
    }
    return $replacements;
  }
}

Change yourmodule_token_info() accordingly.

| improve this answer | |
  • +1 thank you very much @Mołot So, had this feature been considered for views by the developers involved, in the past and decided against? It is likely I will accept your answer, I'll give the code you gave a try. – therobyouknow Jul 12 '13 at 12:31
  • @therobyouknow tokens from $_GET are costly and messes with cache, afaik. And see my updated answer before you'll try. – Mołot Jul 12 '13 at 12:34
  • 1
    if arg2 and arg3 are actually arguments or contextual filters for the View in question, you should be able to just use !2, !3 in the rewrite (I am ignoring arg1 as that really can't easily be dynamic) – Jimajamma Jul 12 '13 at 12:34
  • 1
    @therobyouknow i.stack.imgur.com/e5X2y.png D7 supports this too, just tested it. Dunno why it does not work for you. Jimajamma's answer is OK if arguments are in contextual filters. At least works for me. (LOL 'm fighting against my answer being accepted) – Mołot Jul 12 '13 at 14:09
  • 1
    @therobyouknow DO NOT SERIAL UPVOTE ME! And honestly, upvotes on this post earned me 2x more rep than acceptance would, so no need to worry ;) After all I asked Jimajamma to post his comment as an answer, right? And I was fighting to prove his way is easier :P So it should be obvious I don't mind his post to be accepted. – Mołot Jul 12 '13 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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