If I’m accessing an entry’s URL (the 4th check in Routing), how can I get the entry that the URL matched from a plugin?
1 Answer
You can get the request’s matched element via:
$element = craft()->urlManager->getMatchedElement();
There are two things to remember though:
- There may not be a matched element
- The matched element might not be an entry. (It could also be a category, and if you have any plugins installed that add their own element types that can have their own URLs, it could be one of those.)
To defend against those possibilities, just use the following conditional:
if ($element && $element->getElementType() == ElementType::Entry)
{
// There was a matched element, and it's an entry!
// ...
}
-
-
@joomkit No, in that case the matched element(s) are passed directly to your tranformer function, so there's no need to have a separate way to access them. If you're having trouble trying to get something set up please post as a new question :) Commented Jul 24, 2016 at 13:54