8

I have node A with a node reference to node B.

In a view I show all nodes A that have particular node B referenced (nid: 123, title: node-B-title).

Instead of using the nid in the contextual filter I would like to use the node title. For example, instead of:

view-name/123

I would like to use

view-name/node-B-title

How could I do that?

6 Answers 6

10

I had a very similar problem and I not just found the solution to it, but also learned new things about how view works. Let me share what I learned and the solution to your problem too.

When you are creating a view with one or more contextual filters, the view must receive values of those filters from somewhere. This can be done by -

  1. Picking the context filter value(s) from the URL
    • Typically you would have a page view for such purposes one that has URL of the form foo/%/bar or foo/bar/% for single contextual filter OR of the form foo/%/bar/% or foo/bar/%/% for multiple filters
    • In case you have multiple filters, the url value that appears first is mapped to the first filter, next to the second filter and so on.
  2. Views allows you to tell what to do if the filter values are not available in the URL. This is typically used for block views.
    • You can choose from different actions and then configure the choice too. You can choose from different actions and then configure the choice too. asdfj kajsdf
  3. Another thing views allows you is to tell what to do with the filter values if they are found in the URL as described in #1 above. Typical use cases are - Making dynamic titles based on filter values, Validating filter values etc.
    • When validating filter values, you can change the values too! (That is where our solution lies btw). This is very akin to the way we can change $form values in the drupal form validation callbacks. enter image description here

Solution to your specific problem lies in using a very similar code in the sections pointed to by the arrows in the above image.

This link proved out to be great help to me. Here is an excerpt from it:

$np = explode('/', drupal_get_normal_path($argument));
if (!empty($np[1])) {
  $handler->argument = $np[1];
  return TRUE;
} else {
  return FALSE;
}
1
  • This did not work for me, but rather caused an ajax error in the view upon saving.
    – DrCord
    Commented Mar 25, 2016 at 17:06
1

This will not work in all circumstances, but one approach to the problem in the question is to use the actual node display of node B (or node type B) and set its path how you want it with the path or pathauto module. Then use views block to add the content relationship and the view you want.

Use a contextual filter on NID and then setting Provide default value > Content ID from URL. Then add the views block to your page or set of pages.

If your context allows you to use a views block on the node itself and not a views page this will make it much easier to get the path you want and the views display you want.

1

I edited the accepted answer, but wanted to add the actual code here:

Use PHP validated option under the validation options in the contextual filter.

$np = explode('/', drupal_get_normal_path($argument));
if (!empty($np[1])) {
  $handler->argument = $np[1];
  return TRUE;
} else {
  return FALSE;
}
1

The view argument must be the field reference of content type, then select the option "Provide default argument" and check the option Id Node (something like that).

1) Add argument "X field reference" 2) Check ID node from url (in add default argument) 3) In validation section, check the content type of node, and on setion "Node type" select "Node Id

And is done. :)

4
  • "content ID from URL" only gives the nid. If I would use a term reference I have a could use "validation criteria" with "term name converted to term ID". For the node reference there is no similar option to translate the title to nid. Probably because titles are not necessarily unique.
    – uwe
    Commented Oct 9, 2012 at 2:23
  • Yes is correct, but is necessary the argument must be the field reference (nid), try that.
    – Eleo
    Commented Oct 10, 2012 at 15:25
  • 1) Add argument "X field reference" 2) Check ID node from url (in add default argument) 3) In validation section, check the content type of node, and on setion "Node type" select "Node Id"
    – Eleo
    Commented Oct 10, 2012 at 15:28
  • The actual url can have the nid, and path alias could be the node title. This does work isn't it?
    – SGhosh
    Commented Jan 26, 2013 at 7:05
0

Old question, but found it through Google and figured I'd share my solution for anyone else who finds it in future:

There is now a module named safeword which basically does exactly what Sumeet Pareek suggested: (Automatically) creates a URL friendly field.

0

I would recommend the Extended Path Aliases (path_alias_xt) module. It fixes this problem purely by being installed with no configuration, it's basically magic, apparently. :D Not really, but the excerpt from the module page extolls how you just install it and it fixes this and other problems, but doesn't go into how the module does so behind the scenes... O, well, we could always read the code :D It doesn't specifically mention fixing views paths, but I have used it to do so and it works perfectly.

List of problems it solves, from the module page:

  • Have you ever wondered why, after defining an alias like about-us for, say, node/123, your pages are still riddled with those ugly machine-generated tabs and links like /node/123/edit, node/123/revisions etc.?
  • Likewise for /taxonomy/term/% and /user/% links: did you expect to see /dries/track but got /user/5/track?
  • Do you find that your browser address bar also shows those ugly numbers instead of your aliases?

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.