Skip to content

Commit

Permalink
fixed issues with templates in Resources
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Sep 30, 2024
1 parent ce7b618 commit 33cb5eb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
25 changes: 19 additions & 6 deletions src/Filesystem/TwigTemplateLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ protected function generateContaoTwigTemplatePaths(bool $extension = false): arr
foreach ($bundles as $key => $bundle) {
$path = $bundle->getPath();

foreach (['/templates', '/Resources/views',] as $subpath) {
foreach (['/templates', '/src/Resources/views',] as $subpath) {
if (!is_dir($dir = rtrim($path, '/').$subpath)) {
continue;
}
Expand All @@ -463,11 +463,8 @@ protected function generateContaoTwigTemplatePaths(bool $extension = false): arr
foreach ($resourcePaths as $bundle => $paths) {
foreach ($paths as $path) {
$path = Path::canonicalize($path);
$templates = $this->templateLocator->findTemplates($path);
if (empty($templates)) {
continue;
}

$useContaoTemplateLocator = true;
if ('App' === $bundle) {
if (str_contains($path, '/contao/templates')) {
$namespace = 'Contao_App';
Expand All @@ -479,8 +476,22 @@ protected function generateContaoTwigTemplatePaths(bool $extension = false): arr
$namespace = 'Contao_'.$bundle;
} else {
$namespace = preg_replace('/Bundle$/', '', $bundle);
$useContaoTemplateLocator = false;
}
}

if ($useContaoTemplateLocator) {
$templates = $this->templateLocator->findTemplates($path);
} else {
$templates = [];
$foundPaths = (new Finder())->in($path)->files()->followLinks()->name('*.twig')->sortByName()->getIterator();
foreach ($foundPaths as $file) {
$templates[$file->getBasename()] = $file->getPathname();
}
}
if (empty($templates)) {
continue;
}

foreach ($templates as $name => $templatePath) {
if (str_ends_with($name, '.html5')) {
Expand All @@ -496,6 +507,8 @@ protected function generateContaoTwigTemplatePaths(bool $extension = false): arr
$prefix = 'Contao_Theme_'.$parts[0];
$name = Path::makeRelative($templatePath, $contaoThemePaths[$parts[0]]);
}
} elseif (!$useContaoTemplateLocator) {
$name = Path::makeRelative($templatePath, $path);
}

$twigPath = ($prefix ? "@$prefix/" : '').$name;
Expand All @@ -515,7 +528,7 @@ protected function generateContaoTwigTemplatePaths(bool $extension = false): arr

$file = new \SplFileInfo($templatePath);
$name = $file->getBasename();
if (str_ends_with($name, '.html.twig')) {
if (!$extension && str_ends_with($name, '.html.twig')) {
$name = substr($name, 0, -10);
}

Expand Down
8 changes: 7 additions & 1 deletion tests/Filesystem/TwigTemplateLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,13 @@ public function testGetTemplatePath()
{
$instance = $this->createTestInstance($this->prepareTemplateLoader([]));
$this->assertSame('@Contao_App/content_element/text.html.twig', $instance->getTemplatePath('text', ['disableCache' => true]));
$this->assertSame('@Contao_App/content_element/text.html.twig', $instance->getTemplatePath('text.html.twig', ['disableCache' => true]));
$this->assertSame('@Contao_App/content_element/text.html.twig', $instance->getTemplatePath('content_element/text', ['disableCache' => true]));
$this->assertSame('@Contao_App/content_element/text.html.twig', $instance->getTemplatePath('content_element/text.html.twig', ['disableCache' => true]));
$this->assertSame('@Contao_App/form_text.html.twig', $instance->getTemplatePath('form_text', ['disableCache' => true]));
$this->assertSame('@Contao_App/form_text.html.twig', $instance->getTemplatePath('form_text.html.twig', ['disableCache' => true]));

$this->assertSame('ce_text.html.twig', $instance->getTemplatePath('ce_text', ['disableCache' => true]));
$this->assertSame('ce_text.html.twig', $instance->getTemplatePath('ce_text', ['disableCache' => true]));

$parameters = $this->prepareTemplateLoader([]);
Expand All @@ -209,6 +214,8 @@ public function testGetTemplatePath()
$this->assertSame('ce_text.html.twig', $instance->getTemplatePath('ce_text', ['disableCache' => true]));
$this->assertSame('@Contao_App/ce_headline.html.twig', $instance->getTemplatePath('ce_headline', ['disableCache' => true]));
$this->assertSame('@Contao_a/ce_html.html.twig', $instance->getTemplatePath('ce_html', ['disableCache' => true]));
$this->assertSame('@b/elements/ce_image.html.twig', $instance->getTemplatePath('ce_image', ['disableCache' => true]));
$this->assertSame('@b/elements/ce_image.html.twig', $instance->getTemplatePath('ce_image.html.twig', ['disableCache' => true]));

$GLOBALS['objPage'] = (object) ['templateGroup' => 'customtheme'];
$this->assertSame('@Contao_Theme_customtheme/ce_text.html.twig', $instance->getTemplatePath('ce_text', ['disableCache' => true]));
Expand Down Expand Up @@ -282,7 +289,6 @@ private function prepareTemplateLoader(array $parameters): array
return $parameters;
}


protected function buildKernelAndResourceFinderForBundlesAndPath(array $bundles, string $subpath)
{
$kernel = $this->createMock(Kernel::class);
Expand Down

0 comments on commit 33cb5eb

Please sign in to comment.