6

Quick Summary: how do I pass a alias in a URL to a view's context filter, when that view is located somewhere in the site.

I've read dozens of similar articles, but nothing that directly makes this clear for me, so if someone has some insight, I'd be grateful.

I have a Content Type: "Artists" with an pathauto alias as so: artists/picasso I have a Content Type: Artwork with an entity reference to artists. A given Artwork might have an alias as such: artwork/minotaur (which is a work referencing picasso)

What I want to do is create a view, have it live here: (artwork/artists/picasso) that shows all Artwork by Picasso.

My trouble is: I can get the view set up, but it only displays correctly if I have the NiD in the URL path. For example: /artwork/artists/1 (with 1 being the Nid for the Picasso node)

How can I get the view to accept "picasso" as an argument to use in the location I want (artwork/artists/) for instance.

I have no trouble if I attach a view to the artists/picasso page as a block. I can show all works by him there, but I want to show works by him in another location.

It seems like this should be obvious, but I cannot find a clear answer as to how to implement. Your help is greatly appreciated."

6
  • 1
    I've done this by using the node's title as a token into pathauto, eg, the alias is set up to be artists/[title]. then, assuming all titles are unique, you can use the node title as your view's argument/contextual filter, eg, artwork/artists/% as the path for the view and using the title for the % substitution. just beware special characters in titles. they don't translate well into urls :)
    – Jimajamma
    Commented Apr 16, 2013 at 2:32
  • Thank you @Jimajamma that really helped. I'm going to detail some other settings below just to make it explicit how I got it to function, but your tip really cleared it up for me. I never thought to pass the title to the filter. Commented Apr 16, 2013 at 22:21
  • Here are the steps I took, following @Jimajamma advice. I set a pathauto pattern for "Artist" pages so that the URL would be: artists/picasso. Taking care to have unique titles. Then, I created a view page with a path of "artwork/artists/%". I added a contextual filter as follows: Commented Apr 16, 2013 at 22:21
  • 1
    Then I added a Contextual Filter for: (Content entity referenced from field_artists) Content: Title ). When filter is not in URL -> Provide Default Value -> Raw Value from URL -> Path Component=3. Use Path Alias=Checked. Then, When Filter Value is In URL: Specify Validation Criteria= "PHP Code" with the following PHP code: $handler->argument = str_replace("-"," ",$argument); return true; That did the trick. the PHP code converts the dashes to spaces so it can be evaluated correctly. Commented Apr 16, 2013 at 22:30
  • @ScottEdmonds How do you get the "PHP Code" option in " Specify validation criteria"? I dont see that as an option.
    – CocoSkin
    Commented Apr 26, 2018 at 14:28

2 Answers 2

1

A more robust solution that avoids needing to ensure unique node titles would be to write a custom Views Plugin Argument Validator that converts a contextual filter to node ID.

In your case in which you want to validate node path aliases, you would want to extend the class views_plugin_argument_validate_node in views/modules/node/views_plugin_argument_validate_node.inc to overwrite options_form() to provide your own option. You probably want something like this:

function options_form(&$form, &$form_state) {
  parent::options_form(&$form, &$form_state);
  $form['type'] = array(
    '#type' => 'select',
    '#title' => t('Filter value type'),
    '#options' => array(
      'convert_path_alias' => t('Node path alias converted to Node ID'),
    ),
    '#default_value' => $this->options['type'],
  );
}

You would also need to overwrite validate_argument() to convert the argument (path alias) passed into the function into a node nid, set $this->argument->argument to the converted node nid, and return TRUE if the path alias was successfully converted.

1

This can be done with hook_views_query_alter(). The contextual filters are nothing but where conditions (technically in MySql Slang). So you can implement the mentioned hook and alter the where condition.

But How to get the nid from alias?

Use node_get_object() inside the hook_views_query_alter, you will get the nid. This can be set as the where condition.

Hope this helps you out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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