diff --git a/core/includes/common.inc b/core/includes/common.inc index 11f8a9d..d04ac66 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -33,6 +33,7 @@ use Drupal\Core\Template\Attribute; use Drupal\Core\Render\Element; use Drupal\Core\Session\AnonymousUserSession; +use Drupal\Core\Template\AttachmentManager; /** * @defgroup php_wrappers PHP wrapper functions @@ -1670,6 +1671,12 @@ function drupal_process_attached(array $elements, $dependency_check = FALSE) { 'library' => array(), ); + // Check for attachments added from a template. + if ($template_attachments = AttachmentManager::getAttachments('library')) { + $elements['#attached']['library'] = array_merge($elements['#attached']['library'], $template_attachments); + AttachmentManager::clearAttachments('library'); + } + // Add the libraries first. $success = TRUE; foreach ($elements['#attached']['library'] as $library) { diff --git a/core/lib/Drupal/Core/Template/AttachmentManager.php b/core/lib/Drupal/Core/Template/AttachmentManager.php new file mode 100644 index 0000000..fe4814a --- /dev/null +++ b/core/lib/Drupal/Core/Template/AttachmentManager.php @@ -0,0 +1,57 @@ + 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, 'addLibraries')), ); } @@ -254,4 +256,14 @@ public function isUrlGenerationSafe(\Twig_Node $args_node) { return array(); } + /** + * Adds libraries to the page. + * + * @param array $libraries + * An array of libraries. + */ + public function addLibraries($libraries = array()) { + AttachmentManager::addAttachments($libraries, 'library'); + } + } diff --git a/core/tests/Drupal/Tests/Core/Template/AttachmentManagerTest.php b/core/tests/Drupal/Tests/Core/Template/AttachmentManagerTest.php new file mode 100644 index 0000000..da8c32a --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Template/AttachmentManagerTest.php @@ -0,0 +1,43 @@ +assertArrayEquals(array(), $result); + $attachments = array( + 'special-css-library-1', + 'special-js-library-1', + ); + AttachmentManager::addAttachments($attachments); + $result = AttachmentManager::getAttachments(); + $this->assertTrue(in_array($attachments[0], $result)); + $this->assertTrue(in_array($attachments[1], $result)); + $new_attachment = 'new-css-library'; + AttachmentManager::addAttachments($new_attachment); + $result = AttachmentManager::getAttachments(); + $this->assertTrue(in_array($new_attachment, $result)); + $this->assertEquals(3, count($result)); + AttachmentManager::clearAttachments(); + $result = AttachmentManager::getAttachments(); + $this->assertArrayEquals(array(), $result); + } + +}