Libraries
Libraries are a way to group and manage JavaScript, CSS, and other assets.
Attach a Library using hooks
Attach a library to a specific page
function THEMENAME_preprocess_page(&$variables){
$current_path = \Drupal::service('path.current')->getPath();
if ($current_path === '/mypath') {
$variables['#attached']['library'][] = 'library/name';
}
}Attach library to specific form
function MODULE_NAME_form_alter(&$form, &$form_state, $form_id)
{
if ($form_id == 'FORM_ID') {
$form['#attached']['library'][] = 'MODULE_NAME/LIBRARY_NAME';
}
}Attach library from twig file
Last updated