Drupal 7 allowed to add stylesheets and JavaScript files via the .info file of a module:
mymodule.info
:
stylesheets[all][] = css/mymodule.theme.css
scripts[] = js/mymodule.js
All assets being defined in module .info files were loaded unconditionally on all pages.
Drupal 8
The module .info.yml file properties stylesheets
and scripts
have been removed.
Module developers should declare their assets as proper libraries in a *.libraries.yml file. More information can be found in #2201089
Attach the library where needed via #attached
:
NOTE: The sample below is no longer correct, due to Added hook_page_attachments(_alter)() and removed hook_page_build/alter(). Now use hook_page_attachments()
instead.
$build['#attached']['library'][] = array('mymodule', 'drupal.mymodule');
Use hook_page_build() to attach your library/assets to every page (i.e., resemble the previous effect of the stylesheets[]
and scripts[]
module .info file properties):
/**
* Implements hook_page_build().
*/
function mymodule_page_build(&$page) {
$path = drupal_get_path('module', 'mymodule');
// Declaring a proper library including its dependencies, and
// attaching it is the recommended way:
$page['#attached']['library'][] = array('mymodule', 'drupal.mymodule');
// Deprecated: Add individual CSS/JS files to every page:
$page['#attached']['css'][$path . '/theme/foo.css'] = array('every_page' => TRUE);
$page['#attached']['js'][$path . '/js/foo.js'] = array('every_page' => TRUE);
}
Comments
Which means that only themes
Which means that only themes are able to add the stylesheets and scripts via .info.yml files.
@see https://drupal.org/node/1935708
Keep in mind that people (us) are the ones whom will change the world. Stand up!
revagomes.com.br.
Themes can no longer declare
Themes can no longer declare there scripts directly in the .info.yml file either.
To find out how to do it, here are the changes to read:
* https://drupal.org/node/2228783
* https://drupal.org/node/2201089
Mastodon account: @Artusamak
https://happyculture.coop
mytheme.libraries.yml
It's now in themename.libraries.yml:
https://www.drupal.org/docs/8/theming-drupal-8/adding-stylesheets-css-an...