function webform_library_info_alter in Webform 6.x
Same name and namespace in other branches
- 8.5 includes/webform.libraries.inc \webform_library_info_alter()
Implements hook_library_info_alter().
File
- includes/
webform.libraries.inc, line 49 - Webform libraries.
Code
function webform_library_info_alter(&$libraries, $extension) {
// Only alter modules that declare webform libraries.
// @see hook_webform_libraries_info()
$webform_libraries_modules = \Drupal::moduleHandler()
->getImplementations('webform_libraries_info');
$webform_libraries_modules[] = 'webform';
if (!in_array($extension, $webform_libraries_modules)) {
return;
}
// Use Tippy.js 6.x which is compatible with Drupal 9.x.
if (isset($libraries['libraries.tippyjs']) && floatval(\Drupal::VERSION) >= 9) {
/** @var \Drupal\webform\WebformLibrariesManagerInterface $libraries_manager */
$libraries_manager = \Drupal::service('webform.libraries_manager');
$tippyjs_library = $libraries_manager
->getLibrary('tippyjs/6.x');
$libraries['libraries.tippyjs']['directory'] = 'tippyjs/6.x';
$libraries['libraries.tippyjs']['version'] = $tippyjs_library['version'];
$libraries['libraries.tippyjs']['cdn'] = [
'/libraries/tippyjs/6.x/' => 'https://unpkg.com/tippy.js@' . $tippyjs_library['version'] . '/dist/',
];
$libraries['libraries.tippyjs']['js'] = [
'/libraries/tippyjs/6.x/' . basename($tippyjs_library['download_url']
->toString()) => [],
];
}
// If webform date element in D8 use the old dependency on 'core/drupal.date'.
if (isset($libraries['webform.element.date']) && floatval(\Drupal::VERSION) < 9 && !\Drupal::moduleHandler()
->moduleExists('jquery_ui_datepicker')) {
$libraries['webform.element.date']['dependencies'] = [
'core/drupal.date',
'core/drupalSettings',
];
}
// If chosen_lib.module is installed, then update the dependency.
if (\Drupal::moduleHandler()
->moduleExists('chosen_lib')) {
if (isset($libraries['webform.element.chosen'])) {
$dependencies =& $libraries['webform.element.chosen']['dependencies'];
foreach ($dependencies as $index => $dependency) {
if ($dependency === 'webform/libraries.jquery.chosen') {
$dependencies[$index] = 'chosen_lib/chosen';
$dependencies[] = 'chosen_lib/chosen.css';
break;
}
}
}
}
// If select2.module is installed, then update the dependency.
if (\Drupal::moduleHandler()
->moduleExists('select2')) {
if (isset($libraries['webform.element.select2'])) {
$dependencies =& $libraries['webform.element.select2']['dependencies'];
foreach ($dependencies as $index => $dependency) {
if ($dependency === 'webform/libraries.jquery.select2') {
$dependencies[$index] = 'select2/select2';
break;
}
}
}
}
/** @var \Drupal\webform\WebformLibrariesManagerInterface $libraries_manager */
$libraries_manager = \Drupal::service('webform.libraries_manager');
// Map /library/* paths to CDN.
// @see webform.libraries.yml.
foreach ($libraries as $library_name => &$library) {
// Remove excluded libraries.
if ($libraries_manager
->isExcluded($library_name)) {
unset($libraries[$library_name]);
continue;
}
// Skip libraries installed by other modules.
if (isset($library['module'])) {
continue;
}
if (!empty($library['dependencies'])) {
// Remove excluded libraries from dependencies.
foreach ($library['dependencies'] as $dependency_index => $dependency_name) {
if ($libraries_manager
->isExcluded($dependency_name)) {
$library['dependencies'][$dependency_index] = NULL;
$library['dependencies'] = array_filter($library['dependencies']);
}
}
}
// Handle CDN support.
if (isset($library['cdn']) && isset($library['directory']) && !$libraries_manager
->exists($library['directory'])) {
_webform_library_info_alter_recursive($library, $library['cdn']);
}
}
}