7

I have a view which shows a list of articles, located at:

mypage.com/articles

Each article is linked to its main sub page view, like this:

mypage.com/articles/name-of-my-artile-yay

The URL path is created with autopath & URL aliases.

In views, how do I create a contextual filter to show only the selected sub page view?

I have this as my path in views:

articles/%

but don't know where to go from here. I tried title and making it lowercase and replacing spaces with a hyphen, but that won't always work and the URL aliases change the URL such that it removes words like:

a, an, as, at, before, but, by

And if a page title is:

This is my page title - with this

The URL alias becomes:

my-page-title

Therefore it won't be able to match in my contextual filter using the page title.

What am I missing?

4
  • What would you want to have happen if 2 nodes had the same title? Feb 11, 2013 at 17:26
  • The URL path alias would be different. That is one of the reasons I want to use URL path Alias and not title. Feb 11, 2013 at 17:30
  • Ah, so are you looking to match articles/my-page-title and load in the corresponding node? OR are you looking for articles/My%20Page%20Title to work? Feb 11, 2013 at 17:38
  • articles/my-page-title which is the URL path alias. Feb 11, 2013 at 17:59

6 Answers 6

8
+50

I think the simpliest way is to use Panels, and override the Node page. Add a variant and put your view in it, and send the node's nid argument to it, using node:nid contextual argument.

  1. (install page manager)
  2. Enable node view page (node/%node)
  3. Add a variant, set a validation criteria selecting your concerned node type
  4. Use simple one-column display
  5. Add your view in your column
  6. It asks for an argument, send the node:nid, (or entity_id, or whatever)Send your node's nid to your view
  7. In your view, use node:nid contextual filterYour view arg

All is done.

7
  • Hi @Gregory - I installed panels and watch a few tutorials but cant find anywhere how to do what I need. All the tutorials seem to use taxonomy. I just want to use Url Path Alias. Any suggestions on how i do this? Feb 11, 2013 at 16:29
  • I edited the answer. Feb 11, 2013 at 17:45
  • Hi @Gregory - I followed your instructions and I get a blank page on the node view. I guess that's something but I'm still doing something wrong. Any ideas? With #3 Add a variant, set a validation criteria selecting your concerned node type I only had one option under Variant type which was Panel. is that correct? Feb 11, 2013 at 18:31
  • This is correct. If it's a blank page, then your variant is selected, the problem is toward the displaying of the view or the argument you're sending to it. I updated the answer. Feb 11, 2013 at 18:40
  • Awesome @Gregory - This working.. I just have one issue. It's effecting ALL my nodes and not just my article content type. I'm assuming it has to do with #3 but I only had one option under Variant type which was Panel What have i missed? Feb 11, 2013 at 19:14
3

Set path alias for the content type to something like articles/[node:title]

Option 1: using views to run detail pages

In views:

  1. Create a Page view with the path set to the /articles/%
  2. Add Contextual filters Content: Nid and set it to the following:

    • When the filter value is NOT in the URL => Provide default value
    • Type => Raw value from URL
    • Path => Component 1 (Check "use path alias")

This does a path lookup on the last url segment (something like "my-article-title-path-alias") and returns the correct node ID.

Option 2: By-pass views to run detail pages

Instead of setting the path as /articles/% simply set it as /articles and let the all the path aliased /articles/% nodes resolve normally.

2
  • How confusing - When the filter value is NOT in the URL then use a filter value from the URL!?!?! Thanks for clearing this up though...
    – Felix Eve
    Oct 15, 2013 at 8:46
  • I think this has to do with the fact that views uses the system url (eg: node/2) not the path alias. So in this case the filter value (path alias) is actually not in the url which is why we pass it explicitly
    – Kara
    Oct 15, 2013 at 15:27
1

Add a contextual filter for the node ID and provide the default using PHP:

if (isset($this->view['args'][0])) {
  $path = db_query("SELECT source FROM {url_alias} WHERE alias = :alias LIMIT 1", array(':alias' => 'article/' . $this->view['args'][0]))->fetchColumn();
  if ($path) {
    $parts = explode("/", $path);
    if (isset($parts[1]) && is_numeric($parts[1])) {
      return $parts[1];
    }
  }
}

return FALSE;

This will return, for example, node/123 if node 123's title was My the Page and Title and you accessed the View at example.com/the_view/my-page-title.

5
  • Thanks @Charlie - I don't seem to have the option for adding a contextual filter for 'path'. Do i need to install something extra to get that? Feb 11, 2013 at 18:01
  • I shouldn't have made that assumption that it would be available to you. Would you be content to just use the node ID and explode the resulting machine path from the query, like $parts = explode($path); return $parts[1]; ? Feb 11, 2013 at 18:30
  • I updated my answer to work for the node ID. Feb 11, 2013 at 19:55
  • Thanks @Charlie - I ended up going with Gregory Kapustin's panels solution but this was a close second for me so I wanted to say thanks... voted 1 up!! Feb 11, 2013 at 23:45
  • 1
    This is actually the first answer I gave you, and deleted it. Thx @charlieS ! Feb 12, 2013 at 1:03
0
**Contextual filters**

1) Global: Null 2) (article) content/node: nid When the filter value is NOT available Provide default value : Raw value from url

1
  • Hi @Somik Thanks for stepping in to help with this. Could you please expand on your answer a little. I tried to work it out myself but didn't work so I'm assuming i've done it incorectly Feb 11, 2013 at 16:41
0

Try this approach...

you first grab the normal drupal path "node/nid" then, you take the nid to load the full load and then use the node title as contextual filter.

1) Get the normal path of the current node being viewed...

<?php
$ogpath = drupal_lookup_path("source",$_GET['q']);

2) This may be expensive but try it ... get the current nid

$ogarray = explode('/', $ogpath);
$node = node_load($ogarry[1]);
return $node->title;

Tell me if it works.

Your pathauto is stripping words in a way I haven't come across yet, are you sure it is configured correctly?

2
  • Hi @Chiko - Pathauto is as it comes default. I will try this now. thanks Feb 11, 2013 at 17:24
  • oh.. but I dont want to use node titles in my url.. it has spaces etc. Sorry if i;ve missunderstood. Can you please expand on your method. Thanks again. C Feb 11, 2013 at 17:28
0

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.

Here is the link that proved out to be great help to me - http://www.drupalfoo.com/drupal-7-views-contextual-filter-argument-node-reference-using-url-alias

Your Answer

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

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