nuvoleweb / ui_patterns

[NOTE] Development has moved to https://drupal.org/project/ui_patterns
https://drupal.org/project/ui_patterns
GNU General Public License v2.0
85 stars 56 forks source link

Incorrect "path" in hook_theme definitions when the theme that contains patterns is not active #298

Open vever001 opened 4 years ago

vever001 commented 4 years ago

If we are on the admin theme, and we have pattern definitions inside another theme (e.g: your main theme), then the path in hook_theme() definitions are not correct. This is because LibraryPattern::processCustomThemeHookProperty only takes into account modules.

protected function processCustomThemeHookProperty(PatternDefinition $definition) {
  /** @var \Drupal\Core\Extension\Extension $module */
  $return = [];
  if (!$definition->hasCustomThemeHook() && $this->moduleHandler->moduleExists($definition->getProvider())) {
    $module = $this->moduleHandler->getModule($definition->getProvider());
    $return['path'] = $module->getPath() . '/templates';
    if ($this->templateExists($definition->getBasePath(), $definition->getTemplate())) {
      $return['path'] = str_replace($this->root, '', $definition->getBasePath());
    }
  }
  return $return;
}

This breaks for example the preview when we are in the Views admin and other patterns usages when the active theme is not the one that contains the definitions. I will create a PR shortly.

donquixote commented 3 years ago

There is something else going on here.

        $return['path'] = str_replace($this->root, '', $definition->getBasePath());

The str_replace() produces a path with leading slash. E.g.

$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 assume it is normal that $root won't have a trailing slash) I wonder why this is not more wildly occuring.

@vever001 From looking at the code, I don't think your PR would fix this..

mika2na commented 1 year ago

Moved to drupal.org : https://www.drupal.org/project/ui_patterns/issues/3315219