You are here

public function ComponentLibraryLoader::__construct in Components! 8

Constructs a new ComponentsLoader object.

Parameters

string|array $paths: A path or an array of paths to check for templates.

\Drupal\Core\Extension\ModuleHandlerInterface $module_handler: The module handler service.

\Drupal\Core\Extension\ThemeHandlerInterface $theme_handler: The theme handler service.

File

src/Template/Loader/ComponentLibraryLoader.php, line 35
Contains \Drupal\components\Template\Loader\ComponentLibraryLoader.

Class

ComponentLibraryLoader
Loads templates from the filesystem.

Namespace

Drupal\components\Template\Loader

Code

public function __construct($paths = [], ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {

  // Don't pass $paths to __contruct() or it will create the default Twig
  // namespace in this Twig loader.
  parent::__construct();

  // Retrieve config settings for module to determine prefixed namespaces.
  // See Drupal\components\Form\ComponentsSettingsForm.php
  $config = \Drupal::config('components.settings');

  // The Drupal\Core\Template\Loader\FilesystemLoader makes a Twig namespace
  // for each module and theme, so we re-create that list here.
  $existing_namespaces = [];

  // Look at each module and theme.
  $extension_types = [
    'module' => [
      'handler' => $module_handler,
      'method' => 'getModuleList',
    ],
    'theme' => [
      'handler' => $theme_handler,
      'method' => 'listInfo',
    ],
  ];
  foreach ($extension_types as $type => $extension_type) {
    foreach ($extension_type['handler']
      ->{$extension_type['method']}() as $name => $extension) {
      $existing_namespaces[] = $name;

      // If type is 'module' we need to get the info.
      if ($type == 'module') {
        $info = system_get_info($type, $name);
      }
      else {
        $info = $extension->info;
      }

      // For each library listed in the .info file's component-libraries
      // section, determine the namespace and the path.
      if (isset($info['component-libraries'])) {
        foreach ($info['component-libraries'] as $namespace => $library) {
          $paths = isset($library['paths']) ? $library['paths'] : [];

          // Allow paths to be an array or a string.
          if (!is_array($paths)) {
            $paths = [
              $paths,
            ];
          }

          // Add the extension's path to the library paths.
          foreach ($paths as $key => $path) {
            $paths[$key] = $extension
              ->getPath() . '/' . $path;
          }

          // Prefix the namespace with original name.
          // Prevents overriding components with duplicate valid namespaces.
          if (!empty($config
            ->get('namespace_prefix'))) {
            $namespace = "{$name}_{$namespace}";
          }
          $this->libraries[] = [
            'type' => $type,
            'name' => $name,
            'namespace' => $namespace,
            'paths' => $paths,
            'error' => FALSE,
          ];
        }
      }
    }
  }

  // Register the library paths.
  foreach ($this->libraries as &$library) {
    if (isset($library['paths'])) {
      foreach ($library['paths'] as $path) {
        $this
          ->addPath($path, $library['namespace']);
      }
    }
  }
}