Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 

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);
}
Impacts: 
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done

Comments

revagomes’s picture

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 are whom will change the world. Stand up!
revagomes.com.br is powered by Drupal.

Artusamak’s picture

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

Twitter account: @Artusamak
http://happyculture.coop

RobLoach’s picture