Is it possible in twig to get the route name from a path given (not the current one).
I know that to get the current route, it is like this :
{% set current_path = app.request.get('_route') %}
but this is not what I am looking for. I want to give another path than the current one.
Here is come code of what I want to do
With javascript (Mootools), I add an event on some buttons.
element.addEvent("click", function(event) {
event.preventDefault();
event.stopPropagation();
AjaxFormValideEtEnregistrement(element.get('href'));
});
function AjaxFormValideEtEnregistrement(href) {
//href is a path and I want to get the route..
if i create a route filter I cannot do this :
{{ href|route }}
I guess it is not possible .
}
Not possible out of the box. You are supposed to create your own Twig extension and develop a function, eg router_generate which will:
Match a route by provided path
Return the name of the matched route
Also, if you could explain better what's the use case for such functionality maybe we could assist you with some other, possibly better way of achieving your goal.
Related
I've seen a few answers on how to pass a parameter using iron:router as the third parameter, for example: href="{{pathFor 'article' _id=this._id }}"
What I would like to do is pass a parameter as the second variable (where 'article' is in the example above).
I have a collection of posts that contain a title, a body, and a type. There are three types of articles in the project I'm working on, and each type needs to be routed to a different template because the formatting is significantly different.
What I would like to be able to do is route to a particular url based on the type. An example that captures my intent but doesn't work would be this:
link
One of the "types" is "inquiry", and the route looks like this:
#route "inquiryPost",
path: "inquiry/:_id"
data: ->
Posts.findOne #params._id
waitOn: ->
[
Meteor.subscribe "posts"
]
I've tried using a helper to pass the value into the pathFor, but that didn't work either:
path: ->
type = #type
"pathFor '#{type}'"
...with this on the front end:
link
I can think of a few work arounds, but it seems to me like there should be an easier way to pass in parameters...
So, is it possible to pass in parameters into the second value of a pathFor?
Try with
<template name="example">
link
</template>
Where type is the helper
Template.example.examples({
type:function(){
return type;
}
})
Also you can try with this for example
link with many parameters
Here you where accessing for example to the route
/myRoute/type/rewr8671qew for example
I am updating my website to a Meteor application with iron-router, and need to change my urls. The old scheme had capitalized page names like this:
mysite.com/Contact
but I've changed everything to be lowercase:
mysite.com/contact
That contact route isn't complicated, so it's set up like this:
this.route('contact');
but I want the /Contact url to be redirected. I know I could just do this:
this.route('Contact', {
onBeforeAction: function() {
Router.go('contact');
}
});
but it's so much messier. I'd prefer to do something like this:
this.route('contact', {
path: ['/contact', '/Contact']
});
where the route simply is mapped to multiple paths.
Is there a feature like this? Or is my onBeforeAction the best I'm going to get?
https://github.com/EventedMind/iron-router/blob/devel/DOCS.md#dynamic-path-segments
You can use a Regular Expression for your path segment (see the last example in the Dynamic Path Segments link).
Your path would be:
this.route('contact', {
path: /contact/i
});
Where the 'i' after the forward-slash is the regular expression modifier to be case insensitive allowing you to accept any variation of 'contact' (whether cOntact, Contact, or conTACT).
See http://www.w3schools.com/jsref/jsref_regexp_i.asp for details on the RegEx modifier.
I have a question. I'm creating a view file for product listing which have filter function. For example, My current Url is
mysite/category/electronics?p=1
And in my template I have a tag to add a some filter criteria. When click on a, I want my uri will be mysite/category/electronics?p=1&c=2 for example.
But I don't know what's the best practice to generate href for a tag, by current uri(app.request.uri) or generate uri from route (use path())..... Waiting for help!
Let's say you are on the route name product_show I'd say you should use path()
path("product_show", { p : 1, c : 2, i : "another_parameter"})
PS: If you have a large amount of parameters your should use url_encode to make it more readable.
Ref: http://twig.sensiolabs.org/doc/filters/url_encode.html#url-encode
And finally and good to know. app.request.query.all will give you all current query parameters in your current url.
I have a Drupal site and I have setup a view to power the front page.
My goal is to be able to pass 0-2 arguments to the home page, that get passed into the view. However, I still need the normal Drupal pages to work. The list of arguments is known.
For example:
mysite.com/berlin/birds would pass in "berlin" as the first argument and "birds" as the second argument to the view that powers the front page.
mysite.com/berlin would just pass in one argument, "berlin"
mysite.com/admin would load the normal admin pages in Drupal
I'm not clear on how to achieve this. Is there a hook I can use? I can't find one or think of one. Is there a way to specify this in the argument for the view itself? Perhaps I can write a hook that interjects when the URL is being loaded, and rewrite in the background?
The solution I currently have is to add these paths (since my arguments are known) to the menu system. This works, except that when I the pages they aren't the front page, so the pages don't use the node themes I want (they use the node details theme).
I don't think you can really do this without custom code. The View needs a path before it starts taking arguments, and your URLs start with the arguments. What you can do is fake it with custom code. I've used something like this:
/**
* Implements hook_init().
*/
function mymodule_custom_init() {
$item = menu_get_item(); // Matching Drupal path
if (!$item) { // There is no matching Drupal path.
$_GET['q'] = 'view_path/' . $_GET['q']; // Fake path path.
} // if
} // mymodule_custom_init
Then you give the view a path of "view_path" and it responds to everything that doesn't match anything else in Drupal's paths.
There is a spot in the views UI for "Argument handling code" that takes a small snippet of php - http://drupal.org/node/70145
You could run some code there that checks to see if you are on the front page (or arguments are not present or whatever)and insert the arguments you want.
Another way is to set arguments via a hook_views_pre_view or hook_views_pre_build. Is better because you are sure you don't break other stuff (like another view block).
function MYMODULE_views_pre_view(&$view){
if ($view->name == 'your_view_name' && drupal_is_front_page()) {
$view->set_arguments(array('you_argument','you_second_argument'));
}
}
How can I build a block in Drupal which is able to show the node ID of the view page the block is currently sitting on?
I'm using views to build a large chunk of my site, but I need to be able to make "intelligent" blocks in PHP mode which will have dynamic content depending on what the view is displaying.
How can I find the $nid which a view is currently displaying?
Here is a more-robust way of getting the node ID:
<?php
// Check that the current URL is for a specific node:
if(arg(0) == 'node' && is_numeric(arg(1))) {
return arg(1); // Return the NID
}
else { // Whatever it is we're looking at, it's not a node
return NULL; // Return an invalid NID
}
?>
This method works even if you have a custom path for your node with the path and/or pathauto modules.
Just for reference, if you don't turn on the path module, the default URLs that Drupal generates are called "system paths" in the documentation. If you do turn on the path module, you are able to set custom paths which are called "aliases" in the documentation.
Since I always have the path module turned on, one thing that confused me at first was whether it was ever possible for the arg function to return part of an alias rather than part of system path.
As it turns out, the arg function will always return a system path because the arg function is based on $_GET['q']... After a bit of research it seems that $_GET['q'] will always return a system path.
If you want to get the path from the actual page request, you need to use $_REQUEST['q']. If the path module is enabled, $_REQUEST['q'] may return either an alias or a system path.
For a solution, especially one that involves a view argument in the midst of a path like department/%/list, see the blog post Node ID as View Argument from SEO-friendly URL Path.
In the end this snippet did the job - it just stripped the clean URL and reported back the very last argument.
<?php
$refer= $_SERVER ['REQUEST_URI'];
$nid = explode("/", $refer);
$nid = $nid[3];
?>
Given the comment reply, the above was probably reduced to this, using the Drupal arg() function to get a part of the request path:
<?php
$nid = arg(3);
?>
You should considder the panels module. It is a very big module and requires some work before you really can tap into it's potential. So take that into considderation.
You can use it to setup a page containing several views/blocks that can be placed in different regions. It uses a concept called context which can be anything related to what you are viewing. You can use that context to determine which node is being viewed and not only change blocks but also layout. It is also a bit more clean since you can move the PHP code away from admin interface.
On a side note, it's also written by the views author.
There are a couple of ways to go about this:
You can make your blocks with Views and pass the nid in through an argument.
You can manually pass in the nid by accessing the $view object using the code below. It's an array at $view->result. Each row in the view is an object in that array, and the nid is in that object for each one. So you could run a foreach on that and get all of the nid of all rows in the view pretty easily.
The first option is a lot easier, so if that suits your needs I would go with that.
New about Drupal 7: The correct way to get the node id is using the function menu_get_object();
Example:
$node = menu_get_object();
$contentType = node_type_get_name($node);
Drupal 8 has another method. Check this out:
arg() is deprecated