Right now I'm trying to accomplish routing users through a controller action before rendering the plugin's CP template...
[Plugin]Plugin.php
// ...
public function registerCpRoutes()
{
return [
'plugin/controller' => ['action' => 'plugin/controller/action']
];
}
// ...
The idea here is that I want to perform validation checks in the controller before I serve up the view.
I'm running into two issues when it comes to rendering the template from the controller:
- The template path is set to the core template directory (which makes plugin templates inaccessible)
- If I reset the template path to, say, the plugin's directory then I can no-longer extend core layouts (like
_layouts/cp
) within my template.
My solution was to then just fetch the plugin's template as a string and then render it using the renderString
method so that way I can use the template without modifying any path variables...
[Plugin]Controller.php
// ...
protected function renderPluginTemplate($template, $variables = array())
{
$oldPath = craft()->path->getTemplatesPath();
$newPath = craft()->path->getPluginsPath() . 'plugin/templates';
craft()->path->setTemplatesPath($newPath);
if(!$templatePath = craft()->templates->findTemplate($template)) {
throw new Exception(Craft::t('Could not find template with path: ' . $template));
} else {
$templateContent = file_get_contents($templatePath);
}
craft()->path->setTemplatesPath($oldPath);
echo craft()->templates->renderString($templateContent, $variables);
}
// ...
This works for the most part. Now the layout isn't pulling in the right Javascript dependencies – no JS gets fired – so the panel content is hidden and the dropdown menus don't work.
There must be a better way to do this. I'd be super grateful if anyone could provide any insight.
Jon