ui_patterns icon indicating copy to clipboard operation
ui_patterns copied to clipboard

Leading slash in theme paths

Open donquixote opened this issue 3 years ago • 6 comments

See my comment in https://github.com/nuvoleweb/ui_patterns/issues/298#issuecomment-843501000.

I defined a pattern in a module using a yml file, and I noticed that the theme hook had a path starting with a leading slash. E.g. /modules/custom/my_module/templates/patterns/my_pattern/pattern-my-pattern.html.twig. The problem seems to be the str_replace() in PatternBase and LibraryPattern, which is meant to remove the absolute part from the pattern path.

$definition_base_path = '/var/www/html/web/modules/custom/my_patterns/templates/patterns/my_pattern';
$root = '/var/www/html/web';
$path = str_replace($root, '', $definition_base_path);
print $path === '/modules/custom/my_patterns/templates/patterns/my_pattern' ? 'problem' : 'ok';

I am a bit confused why this was not reported as broken before. Am I doing something wrong?

Going to post a minimal PR and we'll see which test blows up.

donquixote avatar May 18 '21 20:05 donquixote

It seems that in many cases the path with the leading slash is overridden with the correct path, e.g. if the pattern is provided by a theme. This might be the reason why this was undetected so far.

donquixote avatar May 18 '21 21:05 donquixote

I would add tests, but we first need to fix existing tests..

donquixote avatar May 18 '21 21:05 donquixote

It seems there was already a PR that attemps to fix it https://github.com/nuvoleweb/ui_patterns/pull/327 But that one is not OK IMO as $this->root is used in 2 places:

  1. LibraryPattern.php#L81
  2. PatternBase.php#L82 - called if the pattern definition specifies a "base path".

Your PR will work in both cases where $this->root is used. My only concern is that adding a trailing slash doesn't seem to follow the convention. E.g.: if someone extends PatternBase and expects no trailing slash, and does $this->root . DIRECTORY_SEPARATOR ... it may break. Maybe we should fix it in both places by adding the slash?

vever001 avatar May 21 '21 15:05 vever001

Maybe we should fix it in both places by adding the slash?

Yes we can do that. My PR was the most lazy solution possible :)

donquixote avatar May 21 '21 15:05 donquixote

It seems there was already a PR that attemps to fix it #327

Good catch! I did not find this one before. Perhaps we should start from #327, also to give credit to @mistermoper.

donquixote avatar May 21 '21 15:05 donquixote

Hi.

I've updated the PR with the feedback from https://github.com/nuvoleweb/ui_patterns/issues/328#issuecomment-846032467:

Knowing that $this->root comes from the app.root service container parameter, it is reasonable to keep the property without the slash, so that it has an expected value for those who extend PatternBase.

omarlopesino avatar May 21 '21 16:05 omarlopesino