Skip to content

Instantly share code, notes, and snippets.

@markhalliwell
Created July 8, 2014 15:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markhalliwell/fbf19bbfa83ca1134da7 to your computer and use it in GitHub Desktop.
Save markhalliwell/fbf19bbfa83ca1134da7 to your computer and use it in GitHub Desktop.
Adding external/inline resources via #attached in a Drupal 8 theme
<?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);
}
@markhalliwell
Copy link
Author

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 in hook_page_alter() and use $page['#attached'].

@bastnic
Copy link

bastnic commented Oct 21, 2014

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