/**
  * {@inheritdoc}
  */
 public function alter(&$cache, &$context1 = NULL, &$context2 = NULL)
 {
     // Sort the registry alphabetically (for easier debugging).
     ksort($cache);
     // Ensure paths to templates are set properly. This allows templates to
     // be moved around in a theme without having to constantly ensuring that
     // the theme's hook_theme() definitions have the correct static "path" set.
     foreach ($this->currentTheme->getAncestry() as $ancestor) {
         $current_theme = $ancestor->getName() === $this->currentTheme->getName();
         $theme_path = $ancestor->getPath();
         foreach ($ancestor->fileScan('/\\.html\\.twig$/', 'templates') as $file) {
             $hook = str_replace('-', '_', str_replace('.html.twig', '', $file->filename));
             $path = dirname($file->uri);
             $incomplete = !isset($cache[$hook]) || strrpos($hook, '__');
             if (!isset($cache[$hook])) {
                 $cache[$hook] = [];
             }
             $cache[$hook]['path'] = $path;
             $cache[$hook]['type'] = $current_theme ? 'theme' : 'base_theme';
             $cache[$hook]['theme path'] = $theme_path;
             if ($incomplete) {
                 $cache[$hook]['incomplete preprocess functions'] = TRUE;
             }
         }
     }
     // Discover all the theme's preprocess plugins.
     $preprocess_manager = new PreprocessManager($this->currentTheme);
     $plugins = $preprocess_manager->getDefinitions();
     ksort($plugins, SORT_NATURAL);
     // Iterate over the preprocess plugins.
     foreach ($plugins as $plugin_id => $definition) {
         $incomplete = !isset($cache[$plugin_id]) || strrpos($plugin_id, '__');
         if (!isset($cache[$plugin_id])) {
             $cache[$plugin_id] = [];
         }
         array_walk($cache, function (&$info, $hook) use($plugin_id, $definition) {
             if ($hook === $plugin_id || strpos($hook, $plugin_id . '__') === 0) {
                 if (!isset($info['preprocess functions'])) {
                     $info['preprocess functions'] = [];
                 }
                 // Due to a limitation in \Drupal\Core\Theme\ThemeManager::render,
                 // callbacks must be functions and not classes. We always specify
                 // "bootstrap_preprocess" here and then assign the plugin ID to a
                 // separate property that we can later intercept and properly invoke.
                 // @todo Revisit if/when preprocess callbacks can be any callable.
                 Bootstrap::addCallback($info['preprocess functions'], 'bootstrap_preprocess', $definition['replace'], $definition['action']);
                 $info['preprocess functions'] = array_unique($info['preprocess functions']);
                 $info['bootstrap preprocess'] = $plugin_id;
             }
         });
         if ($incomplete) {
             $cache[$plugin_id]['incomplete preprocess functions'] = TRUE;
         }
     }
     // Allow core to post process.
     $this->postProcessExtension($cache, $this->theme);
 }
 /**
  * {@inheritdoc}
  */
 public function __construct(Theme $theme)
 {
     parent::__construct($theme->getName() . '.settings', \Drupal::service('config.storage'), \Drupal::service('event_dispatcher'), \Drupal::service('config.typed'));
     $this->theme = $theme;
     // Retrieve cache.
     $cache = $theme->getCache('settings');
     // Use cached settings.
     if ($defaults = $cache->get('defaults')) {
         $this->defaults = $defaults;
         $this->initWithData($cache->get('data', []));
         return;
     }
     // Retrieve the global settings from configuration.
     $this->defaults = \Drupal::config('system.theme.global')->get();
     // Retrieve the theme setting plugin discovery defaults (code).
     foreach ($theme->getSettingPlugins() as $name => $setting) {
         $this->defaults[$name] = $setting->getDefaultValue();
     }
     // Retrieve the theme ancestry.
     $ancestry = $theme->getAncestry();
     // Remove the active theme from the ancestry.
     $active_theme = array_pop($ancestry);
     // Iterate and merge all ancestor theme config into the defaults.
     foreach ($ancestry as $ancestor) {
         $this->defaults = NestedArray::mergeDeepArray([$this->defaults, $this->getThemeConfig($ancestor)], TRUE);
     }
     // Merge the active theme config.
     $this->initWithData($this->getThemeConfig($active_theme, TRUE));
     // Cache the data and defaults.
     $cache->set('data', $this->data);
     $cache->set('defaults', $this->defaults);
 }
Exemple #3
0
 /**
  * Creates the discovery object.
  *
  * @param \Drupal\bootstrap\Theme $theme
  *   The theme to use for discovery.
  * @param string|bool $subdir
  *   The plugin's subdirectory, for example Plugin/views/filter.
  * @param string|null $plugin_interface
  *   (optional) The interface each plugin should implement.
  * @param string $plugin_definition_annotation_name
  *   (optional) Name of the annotation that contains the plugin definition.
  *   Defaults to 'Drupal\Component\Annotation\Plugin'.
  */
 public function __construct(Theme $theme, $subdir, $plugin_interface = NULL, $plugin_definition_annotation_name = 'Drupal\\Component\\Annotation\\Plugin')
 {
     // Get the active theme.
     $this->theme = $theme;
     // Determine the namespaces to search for.
     $namespaces = [];
     foreach ($theme->getAncestry() as $ancestor) {
         $namespaces['Drupal\\' . $ancestor->getName()] = [DRUPAL_ROOT . '/' . $ancestor->getPath() . '/src'];
     }
     $this->namespaces = new \ArrayObject($namespaces);
     $this->subdir = $subdir;
     $this->pluginDefinitionAnnotationName = $plugin_definition_annotation_name;
     $this->pluginInterface = $plugin_interface;
     $this->themeHandler = \Drupal::service('theme_handler');
     $this->themeManager = \Drupal::service('theme.manager');
 }