Change record status: 
Project: 
Introduced in branch: 
8.0.x
Introduced in version: 
8.0.0-beta4
Description: 

There was no way to dynamically register libraries. Using hook_library_info_alter() to register new library is possible but doesn't end well in alter/weight.

Therefore a new hook is created namely hook_library_info_build()

D8

/**
 * Add dynamic library definitions.
 *
 * Modules may implement this hook to add dynamic library definitions. Static
 * libraries, which do not depend on any runtime information, should be declared
 * in a modulename.libraries.yml file instead.
 *
 * @return array[]
 *   An array of library definitions to register, keyed by library ID. The
 *   library ID will be prefixed with the module name automatically.
 *
 * @see core.libraries.yml
 * @see hook_library_info_alter()
 */
function hook_library_info_build() {
  $libraries = [];
  // Add a library whose information changes depending on certain conditions.
  $libraries['mymodule.zombie'] = [
    'dependencies' => [
      'core/backbone',
    ],
  ];
  if (Drupal::moduleHandler()->moduleExists('minifyzombies')) {
    $libraries['mymodule.zombie'] += [
      'js' => [
        'mymodule.zombie.min.js' => [],
      ],
      'css' => [
        'base' => [
          'mymodule.zombie.min.css' => [],
        ],
      ],
    ];
  }
  else {
    $libraries['mymodule.zombie'] += [
      'js' => [
        'mymodule.zombie.js' => [],
      ],
      'css' => [
        'base' => [
          'mymodule.zombie.css' => [],
        ],
      ],
    ];
  }

  // Add a library only if a certain condition is met. If code wants to
  // integrate with this library it is safe to (try to) load it unconditionally
  // without reproducing this check. If the library definition does not exist
  // the library (of course) not be loaded but no notices or errors will be
  // triggered.
  if (Drupal::moduleHandler()->moduleExists('vampirize')) {
    $libraries['mymodule.vampire'] = [
      'js' => [
        'js/vampire.js' => [],
      ],
      'css' => [
        'base' => [
          'css/vampire.css',
        ],
      ],
      'dependencies' => [
        'core/jquery',
      ],
    ];
  }
  return $libraries;
}
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

zuhair_ak’s picture

There is a confusion on the way to include library to render array as described in the above example

$build['the_element_that_needs_the_asset_library']['#attached']['library'][] = 'mymodule/mymodule.vampire';

"mymodule" namespace is required for including libraries in D8.

There is an issue to clarify the docs about including dynamic library - https://www.drupal.org/project/drupal/issues/2690991

rbilinda’s picture

Not sure if it will help someone, but for the library to work it needs to have an array of options like weight or just an empty array
something line this

$libraries['module_name.library_name'] = [
    'css' => [
      'theme' => [
        'css/css_file.css'=> [],
      ],
    ],
  ];

just like it is declared in the yaml file

library.name:
  css:
    theme:
      css/css_file.css: { }

Note the brackets in the yaml

arno_vgh’s picture

As stated in hook_library_info_alter():

The library ID will be prefixed with the module name automatically

Maybe helpful to note once more in case anyone is wondering why the library won't add.