contao / core-bundle

[READ-ONLY] Contao Core Bundle
GNU Lesser General Public License v3.0
122 stars 57 forks source link

template selection shows full path to extension template folder under Windows #837

Closed fritzmg closed 7 years ago

fritzmg commented 7 years ago

Yet another Windows path problem: when an extension under system/modules is present with a templates folder and you go to Templates » New template, the full path to those extension templates will be shown in the <optgroup> label:

newtemplate

Responsible code: src/Resources/contao/dca/tl_templates.php#L249

/** @var SplFileInfo[] $files */
$files = System::getContainer()->get('contao.resource_finder')->findIn('templates')->files()->name('/\.(' . implode('|', $arrAllowed) . ')$/');

foreach ($files as $file)
{
    $strRelpath = str_replace(TL_ROOT . DIRECTORY_SEPARATOR, '', $file->getPathname());
    $strModule = preg_replace('@^(vendor|system/modules)/([^/]+(/.*-bundle)?)/.*$@', '$2', strtr($strRelpath, '\\', '/'));
    $arrAllTemplates[$strModule][$strRelpath] = basename($strRelpath);
}

$file->getPathname() will be

C:\xampp\htdocs\contao4/system/modules/changelanguage/templates\block_alternate_links.html5

for example, thus

str_replace(TL_ROOT . DIRECTORY_SEPARATOR, '', $file->getPathname());

will not have any effect, because it tries to remove

C:\xampp\htdocs\contao4\

from the full path. Due to the failure of the str_replace, the preg_replace also won't have any effect.

fritzmg commented 7 years ago

Example output of $file->getPathname() for a vendor package (e.g. the core-bundle):

C:\xampp\htdocs\contao4\vendor\contao\core-bundle\src/Resources/contao/templates\analytics\analytics_google.html5
fritzmg commented 7 years ago

It could be fixed by changing src/DependencyInjection/Compiler/AddResourcesPathsPass.php#L45 from

$paths[] = sprintf('%s/system/modules/%s', $rootDir, $name);

to

$paths[] = sprintf('%ssystem/modules/%s', $rootDir . DIRECTORY_SEPARATOR, $name);

Not sure if this is the best way to go though.

leofeyer commented 7 years ago

I assume it also works if you replace DIRECTORY_SEPARATOR with '/' in line 249?

fritzmg commented 7 years ago

No, then it won't work for the vendor paths, because there the root path has a backslash at the end:

C:\xampp\htdocs\contao4\vendor\…
                       ^

vs

C:\xampp\htdocs\contao4/system/…
                       ^
ausi commented 7 years ago

These kind of issues should disappear in Contao 4.4, because we added a more robust StringUtil::stripRootDir() in 8a74bb16f659e5c79b6c428c33360dc3f42264a6.

fritzmg commented 7 years ago

@ausi: looks good :)

leofeyer commented 7 years ago

Please close the ticket then, so you can re-open it in case the issue occurs in Contao 4.4 as well.

fritzmg commented 7 years ago

The assignment to the 4.3.11 milestone should probably be removed.