Beispiel #1
0
 /**
  * {@inheritdoc}
  */
 public function alter(&$types, &$context1 = NULL, &$context2 = NULL)
 {
     // Sort the types for easier debugging.
     ksort($types, SORT_NATURAL);
     $process_manager = new ProcessManager($this->theme);
     $pre_render_manager = new PrerenderManager($this->theme);
     foreach (array_keys($types) as $type) {
         $element =& $types[$type];
         // Ensure elements that have a base type with the #input set match.
         if (isset($element['#base_type']) && isset($types[$element['#base_type']]) && isset($types[$element['#base_type']]['#input'])) {
             $element['#input'] = $types[$element['#base_type']]['#input'];
         }
         // Core does not actually use the "description_display" property on the
         // "details" or "fieldset" element types because the positioning of the
         // description is never used in core templates. However, the form builder
         // automatically applies the value of "after", thus making it impossible
         // to detect a valid value later in the rendering process. It looks better
         // for the "details" and "fieldset" element types to display as "before".
         // @see \Drupal\Core\Form\FormBuilder::doBuildForm()
         if ($type === 'details' || $type === 'fieldset') {
             $element['#description_display'] = 'before';
             $element['#panel_type'] = 'default';
         }
         // Add extra variables to all elements.
         foreach (Bootstrap::extraVariables() as $key => $value) {
             if (!isset($variables["#{$key}"])) {
                 $variables["#{$key}"] = $value;
             }
         }
         // Only continue if the type isn't "form" (as it messes up AJAX).
         if ($type !== 'form') {
             $regex = "/^{$type}/";
             // Add necessary #process callbacks.
             $element['#process'][] = [get_class($process_manager), 'process'];
             $definitions = $process_manager->getDefinitionsLike($regex);
             foreach ($definitions as $definition) {
                 Bootstrap::addCallback($element['#process'], [$definition['class'], 'process'], $definition['replace'], $definition['action']);
             }
             // Add necessary #pre_render callbacks.
             $element['#pre_render'][] = [get_class($pre_render_manager), 'preRender'];
             foreach ($pre_render_manager->getDefinitionsLike($regex) as $definition) {
                 Bootstrap::addCallback($element['#pre_render'], [$definition['class'], 'preRender'], $definition['replace'], $definition['action']);
             }
         }
     }
 }
Beispiel #2
0
 /**
  * {@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);
 }