Skip to content

Commit

Permalink
Merge branch 'datamintsGmbH-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mattab committed May 9, 2014
2 parents 47f7d2c + 0cbd5f0 commit 8e45483
Showing 1 changed file with 51 additions and 5 deletions.
56 changes: 51 additions & 5 deletions core/Twig.php 100644 → 100755
Expand Up @@ -38,11 +38,25 @@ class Twig
public function __construct()
{
$loader = $this->getDefaultThemeLoader();

$this->addPluginNamespaces($loader);

// If theme != default we need to chain
$chainLoader = new Twig_Loader_Chain(array($loader));
$this->addPluginNamespaces($loader);

//get current theme
$manager = Plugin\Manager::getInstance();
$theme = $manager->getThemeEnabled();
$loaders = array();

//create loader for custom theme to overwrite twig templates
if($theme->getPluginName() != \Piwik\Plugin\Manager::DEFAULT_THEME){
$customLoader = $this->getCustomThemeLoader($theme);
if($customLoader){
//make it possible to overwrite plugin templates
$this->addCustomPluginNamespaces($customLoader,$theme->getPluginName());
$loaders[] = $customLoader;
}
}
$loaders[] = $loader;

$chainLoader = new Twig_Loader_Chain($loaders);

// Create new Twig Environment and set cache dir
$templatesCompiledPath = PIWIK_USER_PATH . '/tmp/templates_c';
Expand Down Expand Up @@ -166,6 +180,22 @@ private function getDefaultThemeLoader()
return $themeLoader;
}

/**
* create template loader for a custom theme
* @param \Piwik\Plugin $theme
* @return \Twig_Loader_Filesystem
*/
protected function getCustomThemeLoader(Plugin $theme){
if(!file_exists(sprintf("%s/plugins/%s/templates/", PIWIK_INCLUDE_PATH, $theme->getPluginName()))){
return false;
}
$themeLoader = new Twig_Loader_Filesystem(array(
sprintf("%s/plugins/%s/templates/", PIWIK_INCLUDE_PATH, $theme->getPluginName())
));

return $themeLoader;
}

public function getTwigEnvironment()
{
return $this->twig;
Expand Down Expand Up @@ -300,6 +330,22 @@ private function addPluginNamespaces(Twig_Loader_Filesystem $loader)
}
}

/**
*
* Plugin-Templates can be overwritten by putting identically named templates in plugins/[theme]/templates/plugins/[plugin]/
*
*/
private function addCustomPluginNamespaces(Twig_Loader_Filesystem $loader, $pluginName)
{
$plugins = \Piwik\Plugin\Manager::getInstance()->getAllPluginsNames();
foreach ($plugins as $name) {
$path = sprintf("%s/plugins/%s/templates/plugins/%s/", PIWIK_INCLUDE_PATH, $pluginName, $name);
if (is_dir($path)) {
$loader->addPath(PIWIK_INCLUDE_PATH . '/plugins/' . $pluginName . '/templates/plugins/'. $name , $name);
}
}
}

/**
* Prepend relative paths with absolute Piwik path
*
Expand Down

1 comment on commit 8e45483

@MatomoForumNotifications

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on Matomo forums. There might be relevant details there:

https://forum.matomo.org/t/override-template-of-email-report/54246/1

Please sign in to comment.