diff --git a/core/includes/common.inc b/core/includes/common.inc index 66329f4..eb6593e 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -2698,6 +2698,10 @@ function drupal_add_library($module, $name, $every_page = NULL) { // Only process the library if it exists and it was not added already. if (!isset($added[$module][$name])) { if ($library = drupal_get_library($module, $name)) { + // Allow modules and themes to dynamically attach request and context + // specific data for this library; e.g., localization. + drupal_alter('library', $library, $module, $name); + // Add all components within the library. $elements['#attached'] = array( 'library' => $library['dependencies'], diff --git a/core/modules/locale/locale.module b/core/modules/locale/locale.module index 14f214a..b9f8c51 100644 --- a/core/modules/locale/locale.module +++ b/core/modules/locale/locale.module @@ -631,21 +631,25 @@ function locale_js_translate(array $files = array()) { } /** - * Implement hook_library_info_alter(). + * Implements hook_library_alter(). * * Provides the language support for the jQuery UI Date Picker. */ -function locale_library_info_alter(&$libraries, $module) { - if ($module == 'core' && isset($libraries['jquery.ui.datepicker'])) { - $language_interface = language(Language::TYPE_INTERFACE); +function locale_library_alter(array &$library, $module, $name) { + if ($module == 'core' && $name == 'jquery.ui.datepicker') { // locale.datepicker.js should be added in the JS_LIBRARY group, so that - // this attach behavior will execute early. JS_LIBRARY is the default for - // hook_library_info_alter(), thus does not have to be specified explicitly. - $libraries['jquery.ui.datepicker']['dependencies'][] = array('locale', 'drupal.locale.datepicker'); - $libraries['jquery.ui.datepicker']['settings']['jquery']['ui']['datepicker'] = array( + // the behavior executes early. JS_LIBRARY is the default. + $library['dependencies'][] = array('locale', 'drupal.locale.datepicker'); + + $language_interface = language(Language::TYPE_INTERFACE); + $settings['jquery']['ui']['datepicker'] = array( 'isRTL' => $language_interface->direction == Language::DIRECTION_RTL, 'firstDay' => \Drupal::config('system.date')->get('first_day'), ); + $library['js'][] = array( + 'type' => 'setting', + 'data' => $settings, + ); } } diff --git a/core/modules/system/system.api.php b/core/modules/system/system.api.php index 1a4f1c7..325a1df 100644 --- a/core/modules/system/system.api.php +++ b/core/modules/system/system.api.php @@ -325,6 +325,42 @@ function hook_library_info_alter(&$libraries, $module) { } /** + * Alters a JavaScript/CSS library before it is attached. + * + * Allows modules and themes to dynamically attach further assets to a library + * when it is added to the page; e.g., to add JavaScript settings. + * + * This hook is only invoked once per library and page. + * + * @param array $library + * The JavaScript/CSS library that is being added. + * @param string $extension + * The name of the extension that registered the library. + * @param string $name + * The name of the library. + * + * @see drupal_add_library() + */ +function hook_library_alter(array &$library, $extension, $name) { + if ($extension == 'core' && $name == 'jquery.ui.datepicker') { + // Note: If the added assets do not depend on additional request-specific + // data supplied here, consider to statically register it directly via + // hook_library_info_alter() already. + $library['dependencies'][] = array('locale', 'drupal.locale.datepicker'); + + $language_interface = language(Language::TYPE_INTERFACE); + $settings['jquery']['ui']['datepicker'] = array( + 'isRTL' => $language_interface->direction == Language::DIRECTION_RTL, + 'firstDay' => \Drupal::config('system.date')->get('first_day'), + ); + $library['js'][] = array( + 'type' => 'setting', + 'data' => $settings, + ); + } +} + +/** * Alter CSS files before they are output on the page. * * @param $css