Given the URL mysite.com/foo/bar/term-name I was attempting to create a block view that displayed some related content based on the term name.
I set the contextual filter to:
Content: Has taxonomy term ID.
I set the WHEN THE FILTER VALUE IS AVAILABLE OR A DEFAULT IS PROVIDED to:
specify validation criteria: Validator -> taxonomy term and restricted it to the appropriate category, with Filter value type set to "Term name converted to Term ID" and "Transform dashed in URL to spaces in term name filter values."
This works splendid when I enter in the term name into the "preview with contextual filters".
In order to get the Term passed to the block, I set the WHEN THE FILTER VALUE IS NOT AVAILABLE to "Provide default value", and selected Raw value from URL, and selected 3 for the path component. (again, so it would work on foo/bar/TERM-NAME or foo/bar/TERM-NAME/baz etc..)
The issue is in views_plugin_argument_default_raw.inc :
if ($arg = arg($this->options['index'])) {
return $arg;
}
Drupal's arg function is going to give the *internal path* arguments, so in my case foo/bar/TERM-NAME got unaliased back to node/9999 and TERM-NAME was not being passed to my block.
Either views_plugin_argument_default_raw.inc needs to be adjusted to go back to the original $_SERVER['REQUEST_URI'], or it should be renamed to Raw value from internal drupal path after aliasing. Because how it works now is *not* actually from the URL.
Comments
Comment #1
forsythes CreditAttribution: forsythes commentedI just ran into this problem. Is there a way to work around it? I need it to work from the URL alias, not the internal Drupal path.
Comment #2
rvdtuin CreditAttribution: rvdtuin commentedsub
Comment #3
Red Sky Tom CreditAttribution: Red Sky Tom commentedI had the same problem. I switched the Provide default value to PHP and used the drupal_get_path_alias() function to retrieve the url and then ran it through a preg_replace before returning. Sounds like a lot but it was quick and simple and worked fine.
Comment #4
dawehnerWell there is an option "Use path alias" at least on the current dev version which does exactly what you all wanted.
Comment #6
jramby CreditAttribution: jramby commentedHi,
I always get the same issue even though my views version is at 7.x-3.x-dev (2012-Sep-27)... or did I missed something?
Thanks
Comment #7
jramby CreditAttribution: jramby commentedSorry... I really missed something, ... I don't know if this is yet related to this issue, but my purpose is not really the same :
In my case the view block is shown on a node page with an automatic alias : "content/title-of-the-node" (pathauto)... When I add a contextual filter to the block view like specified in this current issue, I attended somthing like "content/title-of-the-node/term-name" to work... (but 404 Error). Though "node/%nid/term-name" works correctly.
Can someone help?
Thanks in advance.
Comment #8
jramby CreditAttribution: jramby commented... Or could someone tell if this won't be possible? thanks
Comment #9
RunePhilosof CreditAttribution: RunePhilosof commentedjramby - it seems your problem isn't with views but with aliases. You don't have an alias for content/title/term-name pointing to /node/nid/term-name.
Comment #10
jamescook CreditAttribution: jamescook commented#3 and #4
just to confirm, with older version of views I had to use code like:
but in the latest views version there is a select box option:
which does this for you.
In other words, if there is an alias for node/345 you'll be dealing with the alias.
Comment #11
ahtoge CreditAttribution: ahtoge commentedI'm having trouble with a multilanguage site and Views contextual filters. The non-default language versions of the site get their own path components, which seems to interfere with retrieving content ID from URL.
The default language of the site is missing the extra path component, making the path too inconsistent for the "Raw value from URL" option (however, when I just use "Content ID from URL", the filter works on the default language version atleast).
Is there some magic PHP code I could use with "Provide default value [as] PHP Code" instead, or is this perhaps a bug which can be fixed?
My view is a block, and the field I'm running the context filter on is a Entity Reference field.
Comment #12
Jarviss CreditAttribution: Jarviss commentedI confirm that Issue still exist!
Views 7.x-3.7
Pathauto 7.x-1.2
May anyone confirm that it was fixed in dev versions?
I try to filter block with integer field, using pathauto for node urls, so when I test in Views Preview url like: site.com/type/cat/1 it shows filtered results, when 3-d argument is aliased with pathauto it shows nothing! And Of course I set Use path alias! I tried to use Integer field value in path Pattern but still see no option how to do this. May anyone help with detailed Example of PHP code Argument variant of solution untill this will be fixed?
I tried to use this code to provide Default Argument:
Maybe I miss something?
Comment #13
RunePhilosof CreditAttribution: RunePhilosof commentedCould you provide precise guidelines to reproduce the problem?
Comment #14
Jarviss CreditAttribution: Jarviss commentedHi Rune!
I try to make views similar content views block, and I have integer select field in content type that I want to use as argument in views block.
I generate pathauto url for node like this: site.com/type/section/title So in the path I get section as integer field, and label of field is aliased with pathauto.
I create block views and add integer field as argument (contextual filter) and try to set default value as Raw value from node url. Argument number 2 and I set option to use pathauto, as I suppose it should take aliased field label and use it as argumnet, and option to use pathauto makes the magic -> gives value from aliased label.
You may say tha I could use Field value token in path but there is some problem with it https://drupal.org/node/2010424
Field tokens Issue https://drupal.org/node/691078
So as a solution I would like not to use Raw value from path and provide default argument from $node load variable or something...
I tried to use this code but it doesn't work for me.
Token 7.x-1.5
Views 7.x-3.7
Pathauto 7.x-1.2
I made a test: I manually changed url path from site.com/type/section/title to site.com/type/2/title
And Raw value from Url № 2 + set Use path alias filters the view!!! So somehow integer field value will work in path, and integer field label will not!
Thanks for your interest Rune!
Comment #15
LiCra CreditAttribution: LiCra commented@ RunePhilosof
Yes, this is still an issue
I set up a small example on a test site of mine. Here is the layout, it is very simplistic.
So I have Jobs that are posted on the site (content type: Job Posting) ex. example.com/content/manager-job (path auto)
I have applications (content type: Job Application)
I have a few views setup with relationships so that the site administrators can see who applied to what jobs
But take for instance that my site STAFF (role) that would be reviewing the jobs that were applied for, needed a TAB on the actual job post so they could see who applied for it, just by clicking the tab.
Here is where the view issue happens.
I create a view (related jobs) with the fields I need and a contextual filter so that I can see just the applications that were for that job.
Page settings: Path: /content/%/applicants (path to the job post, plus the applicants part which will be the tab
menu: Tab: applicants
Contextual filter: Content:Job (which is a field from the application content type)
when filter value is not in URL: provide default value: RAW value from URL => path component: 2 and Use path alias is checked
(I do this because the job posting that I want the tab to be on is here: ex. example.com/content/manager-job (path auto) >>> manager-job should be what the filter uses to know the applications for that job.
I saved the filter. and no queries are being made to the database. If I use the node/45 in the preview area it works, but not if I type content/manager-job
It does not seem to use the manager-job as the component it looks for.
Help would be appreciated.
Comment #16
ericras CreditAttribution: ericras commentedHere's an export based on the latest dev version of Views. It displays a node based on the URL /exampleview/% using the "Content: NID" contextual filter.
The problem is that "Use path alias" setting in the Content: NID contextual filter doesn't work.
Expected behavior:
For a node with nid 34 and path alias mypagealias: Visiting /exampleview/mypagealias should display the node.
Actual Behavior:
Visiting /exampleview/mypagealias does not display the node but /exampleview/34 works, the opposite of what is expected based on the "Use path alias" setting being checked.
Comment #17
MixologicThats not the expected behavior actually. What you are doing is telling views to filter based on the NID, So it expects a numeric NID. If there isn't a NID in the URL for some reason, then you can set how it should behave. In the case of exampleview/mypathalias since mypathalias isnt a nid, you are saying "get the argument from the path, and use the path alias of the current page which in this case is simply 'exampleview/%'. The 'get raw value from the url' does just that - gets exactly what is in the URL. If you happen to be on a node page or taxonomy page, the 'URL' could be either what the user sees in their browser window, or what is stored behind the scenes. But there isnt any function to turn 'mypathalias' back into a NID. In order to do that you'd have to 'specifiy validation criteria', select PHP code, and turn mypathalias back into a NID with some code. (i.e
$handler->argument = drupal_get_normal_path($argument)
etc. (I havent tested that, but thats the idea.It might be more clear if these were two options -
In any case what you are trying to do isn't built into views out of the box, because on *your* site 'mypathalias' happens to be an alias for a node, and it doesnt have any other parts, but many, many sites have much longer aliases for nodes, like /content/mypathalias or /articles/category/mypathalias - so views cant assume that a single url part is actually a whole alias.
Comment #18
joshuautley CreditAttribution: joshuautley commentedI can't get a simple block to use the url to display content that has the term... uggg....
Comment #18.0
joshuautley CreditAttribution: joshuautley commentedtypo fix
Comment #19
colanDoes Views URL Path Arguments work around this? Just wondering if it's the same problem.
Comment #20
joshuautley CreditAttribution: joshuautley commentedIt's been awhile. I'll review the issue.
Comment #21
allsite CreditAttribution: allsite as a volunteer commented#19 Thank you this is a much better solution vs using Title in situations where you're overriding node display with a view. Perfect!