diff -u b/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php --- b/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -76,7 +76,7 @@ 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('addLib', array($this, 'addLib'), array('needs_context' => true)), + new \Twig_SimpleFunction('addLib', array($this, 'addLibraries')), ); } @@ -255,8 +255,14 @@ return array(); } - public function addLib(&$context, $libraries = null) { - $context['#attached']['library'][] = $libraries; + /** + * Adds libraries to the page. + * + * @param array $libraries + * An array of libraries. + */ + public function addLibraries($libraries = array()) { + \Drupal::service('template.attachment.manager')->addAttachments($libraries, 'library'); } } only in patch2: unchanged: --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1098,6 +1098,8 @@ services: twig.loader.filesystem: class: Twig_Loader_Filesystem arguments: ['@app.root'] + template.attachment.manager: + class: Drupal\Core\Template\TemplateAttachmentManager element_info: alias: plugin.manager.element_info file.mime_type.guesser: only in patch2: unchanged: --- /dev/null +++ b/core/lib/Drupal/Core/Template/TemplateAttachmentManager.php @@ -0,0 +1,51 @@ +attachments = array(); + } + + /** + * Gets the page attachments. + * + * @param string $type + * The attachment type. + * + * @return array + * The page attachments. + */ + public function getAttachments($type = 'library') { + return $this->attachments[$type]; + } + + /** + * Adds page attachments. + * + * @param mixed $attachments + * An attachment name or an array of attachment names. + * @param string $type + * The attachment type. + */ + public function addAttachments($attachments, $type = 'library') { + $this->attachments[$type][] = $attachments; + } +}