This seems really basic and apologies if it is or is documented already... I need to create a menu block that points to various Views that use taxonomy term arguments. I have a section that gives information for a city so the url would be content/cities/'city_name' I would set a block of links (menu) to appear for all the city urls ../content/cities/* and the links would be to Views I created for restaurants, hotels, car hire, parks, etc. where the content displayed would use the city name as the argument for the View. For each city, the menu item titles would be the same, but obviously each time would point to the content for which ever city was being viewed... looking at a page for Paris, the urls in the menu block the link Restaurants would point to the view that lists the restaurants in Paris... the Taxis link to the View listing taxis for Paris... Switching to another city the menu names remain the same, but they all point to info for the new city. Another example would be the menu links at the bottom of the Drupal Project pages for "Support" showing - "View all support requests", "View pending support requests", "View pending bug reports" etc. Same menu items, different projects. Thanks.
Comments
Comment #1
Anonymous (not verified) commentedWhat you'd probably want to do is create a view that outputs the list of links (menu) so that it's dynamically generated (you could use 'Global: Custom text' as the field to output). You'd then use something like arguments to specify the URL the links point to. I'm not sure how arguments work with blocks, or if you'd be able to use the NID of the node in the corresponding page instead... But hopefully that'll point you in the right direction.
Comment #2
tjb74 commentedThanks for the suggestion. The Global: Custom Text creates the menu item no problem and in the Live Preview it picks up the argument using the path content/something/%1, however the published block doesn't pick up the argument... The urls for the the links showing as content/something/ . I know that Quick Tabs blocks can pick up arguments from the url as %0, %1, %2, %3 and was hoping that a View block could do the same... taking the last part of the url as the argument. Am I missing something or any other suggestions appreciated?
Comment #3
Anonymous (not verified) commentedThis is what I'm not sure on, how to get the arguments to the block. Apparently it is possible (though that solution was for Views 1), but this page might have some helpful, up-to-date code...
Now that you know you just need to pass a page's arguments to a block, it shouldn't be too hard to find a post in the Views issue queue where someone asked to do just that (you're not the first).
Comment #4
tjb74 commentedI appreciate that this is possible but I am having a problem achieving it and as this didn't work when I first tried it I presumed I was missing something... I understand that arguments cannot be passed to blocks so code need to go get the argument and pass it. I also created a page view for testing the picking up the argument from the url too with url ../content/foo/bar/test (for which only the view appears). In the Global: box I use content/something/%1 for taxonomy Term. Then Under Arguments I select Taxonomy Term then for: Taxonomy Term Action to take if argument is not present, I check Provide default argument and add to PHP argument code: "return arg(3);" and for testing purposes have Action to take if argument does not validate: Display all values.
When I use the url ../content/foo/bar/test/baz (baz being a taxonomy term) both the view and block appear and show the right link with baz at the end... I have a node that will appear for the url ../content/foo/bar/baz However, when I then remove the 'test' from the url I type in (which would then shows node content) and reduce the argument value from 3 to 2 (as one less url segment) I get the block without the baz - and the ../content/foo/bar/test/baz no longer shows the block but still shows the page view with the links... Adding the 'test' to the baz node url and making "return arg(3);" and the node replaces the page view for url for baz, but the block loses the arguments. For boo which has no node at ..../content/foo/bar/test/boo again the pageview and block view show the correct urls.
I am still missing something... Using pathauto, does my php code need to be more than just return arg(); for the nodes?
Comment #5
tjb74 commentedThis is the code that needed to be used instead of return arg() that gets the alias path and breaks it up so it can be used for arguments to be passed to the view block.
$alias = drupal_get_path_alias($_GET['q']);
$alias_array = explode('/', $alias);
return $alias_array[2];
Comment #6
merlinofchaos commentedFor a views block to pick up arguments from the URL you need to set 'provide default' for action to take if there is no argument, and use one of the argument-finders there. You may need to use PHP code.
Comment #7
Anonymous (not verified) commentedA website I was working on the other day required a block to know the node's ID. I was able to 'Provide [a] default argument' of 'Node ID from URL'. As I understand it, this gets the node ID from the URL (as opposed to the displayed path alias, if any) and so only works for blocks on a node page and that need the Node ID. For other arguments (like taxonomy terms, etc.) you'll need to use some PHP, like possibly
return arg(1);.Comment #8
tjb74 commentedThe menu block works well except for taxonomy terms with spaces such as New York, which is presented as new-york in the url. For Views not using the php code and with the ' Transform spaces to dashes in URL' box is checked returns the right display...
However for Views using the php code:
$alias = drupal_get_path_alias($_GET['q']);
$alias_array = explode('/', $alias);
return $alias_array[2];
The display is not shown even though the Live Preview query shows the term is taken and separated from a url ../site/content/new-york :
(term_data.name = 'new york')
Title New York
Comment #9
tjb74 commentedActually it does work. I had a bunch of nodes associated to the taxonomy term which the menu block should have appeared for and I did have views for all the links in the menu block... But the menu block only started to show after I created another content type node for some reason... Caching issue after I turned on the Transform spaces to dashes in URL (which in fact does the opposite and transforms the dashes to spaces...)?
The menu block shows, however the links with the taxonomy term have a space in them (they still work) but it would be better to have the - dash put back in them. How is that possible?
Comment #10
pavlos commentedGreat insight here...
I am having a similar issue here... Even though I managed to make it work fine, when I construct links using custom text, the argument is passed in the url of the link using its Taxonomy Name (with space) rather than non-capitalised first letter words separated by -
So I think this must be processed using some of PHP's functions.
In structured english...
If argument contains space
-> break into 2 variables
-> transform into lowercase
-> output - Restaurants in %1
Not very advanced programmer here but what I am not sure of is how to do this using the block view with the Global:Custom Text fields.
Or another idea is to have some php validation rules and 301 redirect for when the URL contains '%20' for example... One thing I know from php is that there is always more than one way to do something...
-------------
Another issue I am having is that in the view header, the php that gets the argument from the view, displays the area as new-york instead of New York.... But might be something I am missing here (my first views2 project).
Comment #11
tjb74 commentedBy using Global text I can create a 'fixed' menu link to a view page url, however these links are static and are there even if there is no content. By using node titles as links I can create links to nodes, which only appear if the node type is published, so no content no link... What I would like to do is have links to view pages but only appear if the view has content. I don't see how to do this... The options I was looking for and are not there are: for title in the field value to be "View Title" and to link to the view url and the filter to be "Show view if there is content". Perhaps there is another way to do this but I don't see it?
Comment #12
ron_sparks commentedThanks BWPanda, I thought this was going to be crazy hard, and was looking at how to pass vars to the template, turns out making a global var was very simple.
I now have dynamic links,
center/!%/clients
center/!%/staff
center/!%/[view url alias]
Thanks so much for posting this!
I used this with block attachment, and pasted the argument from the view the block is attached to then added my block to a menu region!
Comment #13
Anonymous (not verified) commentedComment #14
netourish commentedHi Tjbcham,
Thank you very much for this.
$alias = drupal_get_path_alias($_GET['q']);
$alias_array = explode('/', $alias);
return $alias_array[2];
It really worked for me.
Thank you BWPanda for posting this.
Thanking you all,
Pradeep.