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

Doesn't work with multi-theme sites #308

Open WidgetsBurritos opened 3 years ago

WidgetsBurritos commented 3 years ago

Problem Statement: Our website uses the context module to use multiple themes depending on various criteria (such as content type). When used in combination with the component_blocks + ui_patterns modules, we never see any component blocks in the toolbar, unless they're associated with the default theme. The reason for this is this block of code:

  /**
   * Create a list of all directories to scan.
   *
   * This includes all module directories and directories of the default theme
   * and all of its possible base themes.
   *
   * @return array
   *   An array containing directory paths keyed by their extension name.
   */
  protected function getDirectories() {
    $default_theme = $this->themeHandler->getDefault();
    $base_themes = $this->themeHandler->getBaseThemes($this->themeHandler->listInfo(), $default_theme);
    $theme_directories = $this->themeHandler->getThemeDirectories();

    $directories = [];
    if (isset($theme_directories[$default_theme])) {
      $directories[$default_theme] = $theme_directories[$default_theme];
      foreach ($base_themes as $name => $theme) {
        $directories[$name] = $theme_directories[$name];
      }
    }

    return $directories + $this->moduleHandler->getModuleDirectories();
  }

It's explicitly only ever looking for the default theme, so it doesn't let sites with multiple themes to use patterns.

Proposed Solution

This is not the same issue as #304 but both could be solved the same way, per @vever001's suggestion: https://github.com/nuvoleweb/ui_patterns/issues/304#issuecomment-687164731

And the logic could be simplified to:

protected function getDirectories() {
  return $this->moduleHandler->getModuleDirectories() + $this->themeHandler->getThemeDirectories();
}

Same logic as \Drupal\Core\Layout\LayoutPluginManager and \Drupal\breakpoint\BreakpointManager.

Since that's the way core does it, it makes the most sense to replicate that behavior.

It is also worth noting this would not get fixed by the PR in #305.

WidgetsBurritos commented 3 years ago

Oh also, FWIW we can't just look at the active theme instead of the default theme either. I tested swapping $this->themeHandler->getDefault(); with \Drupal::service('theme.manager')->getActiveTheme()->getName();, but when used with layout builder it seemed to be grabbing the admin theme, not the theme of the current page.