You are here

function hook_library_info_build in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Render/theme.api.php \hook_library_info_build()
  2. 9 core/lib/Drupal/Core/Render/theme.api.php \hook_library_info_build()

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 value

array[] An array of library definitions to register, keyed by library ID. The library ID will be prefixed with the module name automatically.

See also

core.libraries.yml

hook_library_info_alter()

Related topics

1 function implements hook_library_info_build()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

common_test_library_info_build in core/modules/system/tests/modules/common_test/common_test.module
Implements hook_library_info_build().

File

core/lib/Drupal/Core/Render/theme.api.php, line 849
Hooks and documentation related to the theme and render system.

Code

function hook_library_info_build() {
  $libraries = [];

  // Add a library whose information changes depending on certain conditions.
  $libraries['mymodule.zombie'] = [
    'dependencies' => [
      'core/once',
    ],
  ];
  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;
}