diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 511cefb..1efcc18 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -76,6 +76,7 @@ public function getFunctions() { new \Twig_SimpleFunction('path', array($this, 'getPath'), array('is_safe_callback' => array($this, 'isUrlGenerationSafe'))), new \Twig_SimpleFunction('url_from_path', array($this, 'getUrlFromPath'), array('is_safe_callback' => array($this, 'isUrlGenerationSafe'))), new \Twig_SimpleFunction('link', array($this, 'getLink')), + new \Twig_SimpleFunction('add_libraries', array($this, 'addLibraries'), array('needs_context' => true)), ); } @@ -254,4 +255,15 @@ public function isUrlGenerationSafe(\Twig_Node $args_node) { return array(); } + /** + * Adds libraries to the page. + * + * @param array $context + * The template context. + */ + public function addLibraries($context) { + $variables = $context['reference']->getElement(); + $variables['elements']['#attached']['library'] = array_merge($variables['elements']['#attached']['library'], $context['libraries']); + } + } diff --git a/core/lib/Drupal/Core/Template/VariableStorage.php b/core/lib/Drupal/Core/Template/VariableStorage.php new file mode 100644 index 0000000..9c82078 --- /dev/null +++ b/core/lib/Drupal/Core/Template/VariableStorage.php @@ -0,0 +1,15 @@ +elements = $elements; + } + + public function &getElement() { + return $this->elements; + } +} diff --git a/core/lib/Drupal/Core/Theme/ThemeManager.php b/core/lib/Drupal/Core/Theme/ThemeManager.php index a100374..3aee3fa 100644 --- a/core/lib/Drupal/Core/Theme/ThemeManager.php +++ b/core/lib/Drupal/Core/Theme/ThemeManager.php @@ -13,6 +13,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Template\Attribute; use Drupal\Component\Utility\SafeMarkup; +use Drupal\Core\Template\VariableStorage; /** * Provides the default implementation of a theme manager. @@ -372,6 +373,7 @@ protected function theme($hook, $variables = array()) { if (isset($theme_hook_suggestion)) { $variables['theme_hook_suggestion'] = $theme_hook_suggestion; } + $variables['reference'] = new VariableStorage($variables); $output = $render_function($template_file, $variables); }