Created
July 8, 2014 15:45
-
-
Save markhalliwell/fbf19bbfa83ca1134da7 to your computer and use it in GitHub Desktop.
Adding external/inline resources via #attached in a Drupal 8 theme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Example of how to attach external and inline resources for a theme via the | |
* #attached render array method in 8.x. | |
*/ | |
/** | |
* Helper function for attaching resources to a render array. | |
*/ | |
function _stark2_attach_resources(&$element) { | |
$element['#attached']['css'][] = array( | |
'type' => 'external', | |
'data' => '//cdn.jsdelivr.net/alertify.js/0.4.0rc1/themes/alertify.css', | |
'every_page' => TRUE, | |
); | |
$element['#attached']['css'][] = array( | |
'type' => 'external', | |
'data' => '//cdn.jsdelivr.net/alertify.js/0.4.0rc1/themes/alertify.default.css', | |
'every_page' => TRUE, | |
); | |
$element['#attached']['js'][] = array( | |
'type' => 'external', | |
'data' => '//cdn.jsdelivr.net/alertify.js/0.4.0rc1/alertify.js', | |
'every_page' => TRUE, | |
); | |
$element['#attached']['js'][] = array( | |
'type' => 'inline', | |
'data' => '(function($){ $(document).ready(function () { Alertify.dialog.alert("This is the Alertify.js plugin. Drupal loaded external CSS and JS resources and then this inline script to display this message."); }); })(jQuery)', | |
'every_page' => TRUE, | |
); | |
} | |
/** | |
* Implements hook_page_alter(). | |
*/ | |
function stark2_page_alter(&$page) { | |
// Works. | |
_stark2_attach_resources($page); | |
} | |
/** | |
* Implements hook_preprocess_THEME_HOOK(). | |
*/ | |
function stark2_preprocess_page(&$variables) { | |
// Does not work. The main argument passed, $variables, is not a | |
// render array. It contains the variables sent to the page template. | |
// _stark2_attach_resources($variables); | |
// Does not work. I am assuming due to some early aggregation/caching of the | |
// page. Possible "bug", but very likely this is also "works as designed". | |
// _stark2_attach_resources($variables['page']); | |
// Works, however using hook_preprocess_page() to do this is not recommended. | |
// Use the above hook_page_alter() example instead. | |
// _stark2_attach_resources($variables['page']['content']); | |
} | |
/** | |
* Implements hook_page_build(). | |
*/ | |
function stark2_page_build(&$page) { | |
// Does not work. This is a module hook and will not be invoked in a theme. | |
// _stark2_attach_resources($page); | |
} |
In case of need, you should look at hook_page_attachments_alter.
/**
* Implements hook_page_attachments_alter().
*/
function <your module>_page_attachments_alter(array &$page)
{
$page['#attached']['css'][] = array(
'type' => 'external',
'data' => '//fonts.googleapis.com/css?family=Open+Sans',
'every_page' => TRUE,
);
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Keep in mind the helper function
_stark2_attach_resources()
I created is just so I didn't have to duplicate the code for each function and I could just comment in/out which hook I wanted to test. In reality, you'd just put the attachments directly inhook_page_alter()
and use$page['#attached']
.