Commit ff20a6fa authored by webchick's avatar webchick

Issue #2390707 by Wim Leers: Remove hook_library_alter() implementations

parent 1419122c
......@@ -10,6 +10,7 @@
use Drupal\Component\Utility\Environment;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Utility\String;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Render\Element\Textfield;
......@@ -66,28 +67,37 @@ function color_form_system_theme_settings_alter(&$form, FormStateInterface $form
}
/**
* Implements hook_library_alter().
* Implements hook_library_info_alter().
*
* Replaces style sheets declared in libraries with color-altered style sheets.
*/
function color_library_alter(&$library, $name) {
$active_theme = \Drupal::theme()->getActiveTheme();
$theme_key = $active_theme->getName();
$theme_libraries = $active_theme->getLibraries();
if (in_array($name, $theme_libraries)) {
// Override stylesheets.
$color_paths = \Drupal::config('color.theme.' . $theme_key)->get('stylesheets');
function color_library_info_alter(&$libraries, $extension) {
$themes = array_keys(\Drupal::service('theme_handler')->listInfo());
if (in_array($extension, $themes)) {
$color_paths = \Drupal::config('color.theme.' . $extension)->get('stylesheets');
if (!empty($color_paths)) {
foreach ($library['css'] as &$css) {
// Loop over the path array with recolored CSS files to find matching
// paths which could replace the non-recolored paths.
foreach ($color_paths as $color_path) {
// Color module currently requires unique file names to be used,
// which allows us to compare different file paths.
if (drupal_basename($css['data']) == drupal_basename($color_path)) {
// Replace the path to the new css file.
// This keeps the order of the stylesheets intact.
$css['data'] = $color_path;
foreach (array_keys($libraries) as $name) {
// Override stylesheets.
foreach ($libraries[$name]['css'] as $category => $css_assets) {
foreach ($css_assets as $path => $metadata) {
// Loop over the path array with recolored CSS files to find matching
// paths which could replace the non-recolored paths.
foreach ($color_paths as $color_path) {
// Color module currently requires unique file names to be used,
// which allows us to compare different file paths.
if (drupal_basename($path) == drupal_basename($color_path)) {
// Replace the path to the new css file.
// This keeps the order of the stylesheets intact.
$index = array_search($path, array_keys($libraries[$name]['css'][$category]));
$preceding_css_assets = array_slice($libraries[$name]['css'][$category], 0, $index);
$succeeding_css_assets = array_slice($libraries[$name]['css'][$category], $index + 1);
$libraries[$name]['css'][$category] = array_merge(
$preceding_css_assets,
[$color_path => $metadata],
$succeeding_css_assets
);
}
}
}
}
}
......@@ -470,6 +480,9 @@ function color_scheme_form_submit($form, FormStateInterface $form_state) {
->set('stylesheets', $css)
->set('files', $paths['files'])
->save();
// Clear the library cache.
Cache::invalidateTags(['library_info']);
}
/**
......
......@@ -524,21 +524,30 @@ function locale_js_translate(array $files = array()) {
}
/**
* Implements hook_library_alter().
* Implements hook_library_info_alter().
*
* Provides the language support for the jQuery UI Date Picker.
*/
function locale_library_alter(array &$library, $name) {
if ($name == 'core/jquery.ui.datepicker') {
// locale.datepicker.js should be added in the JS_LIBRARY group, so that
// the behavior executes early. JS_LIBRARY is the default.
$library['dependencies'][] = 'locale/drupal.locale.datepicker';
function locale_library_info_alter(array &$libraries, $module) {
if ($module === 'core' && isset($libraries['jquery.ui.datepicker'])) {
$libraries['jquery.ui.datepicker']['dependencies'][] = 'locale/drupal.locale.datepicker';
$libraries['jquery.ui.datepicker']['drupalSettings']['jquery']['ui']['datepicker'] = [
'isRTL' => NULL,
'firstDay' => NULL,
];
}
}
/**
* Implements hook_js_settings_alter().
*
* Generates the values for the altered core/jquery.ui.datepicker library.
*/
function locale_js_settings_alter(&$settings) {
if (isset($settings['jquery']['ui']['datepicker'])) {
$language_interface = \Drupal::languageManager()->getCurrentLanguage();
$library['drupalSettings']['jquery']['ui']['datepicker'] = array(
'isRTL' => $language_interface->getDirection() == LanguageInterface::DIRECTION_RTL,
'firstDay' => \Drupal::config('system.date')->get('first_day'),
);
$settings['jquery']['ui']['datepicker']['isRTL'] = $language_interface->getDirection() == LanguageInterface::DIRECTION_RTL;
$settings['jquery']['ui']['datepicker']['firstDay'] = \Drupal::config('system.date')->get('first_day');
}
}
......
......@@ -55,7 +55,7 @@ function quickedit_page_attachments(array &$page) {
}
/**
* Implements hook_library_alter().
* Implements hook_library_info_alter().
*
* Includes additional stylesheets defined by the admin theme to allow it to
* customize the Quick Edit toolbar appearance.
......@@ -70,43 +70,28 @@ function quickedit_page_attachments(array &$page) {
* quickedit_stylesheets:
* - css/quickedit.css
* @endcode
*
* The library needs to be dynamically enhanced, because an admin theme normally
* does not participate in the front-end.
*
* @param array &$library
* The JavaScript/CSS library that is being altered.
* @param string $name
* The name of the library.
* @param string $theme
* (optional) Internal use only. A base theme name for which to retrieve the
* 'edit_stylesheets' property.
*
* @todo Remove this in favor of the 'stylesheets-additional' property proposed
* in https://drupal.org/node/1209958
*/
function quickedit_library_alter(array &$library, $name, $theme = NULL) {
if ($name == 'quickedit/quickedit') {
// Retrieve the admin theme.
if (!isset($theme)) {
$theme = Drupal::config('system.theme')->get('admin');
}
if ($theme && $theme_path = drupal_get_path('theme', $theme)) {
$info = system_get_info('theme', $theme);
// Recurse to process base theme(s) first.
if (isset($info['base theme'])) {
quickedit_library_alter($library, $name, $info['base theme']);
}
if (isset($info['quickedit_stylesheets']) && is_array($info['quickedit_stylesheets'])) {
foreach ($info['quickedit_stylesheets'] as $path) {
$library['css'][] = array(
'data' => $theme_path . '/' . $path,
'group' => CSS_AGGREGATE_THEME,
'weight' => CSS_THEME,
);
function quickedit_library_info_alter(&$libraries, $extension) {
if ($extension === 'quickedit' && isset($libraries['quickedit'])) {
$theme = Drupal::config('system.theme')->get('admin');
// First let the base theme modify the library, then the actual theme.
$alter_library = function(&$library, $theme) use (&$alter_library) {
if ($theme_path = drupal_get_path('theme', $theme)) {
$info = system_get_info('theme', $theme);
// Recurse to process base theme(s) first.
if (isset($info['base theme'])) {
$alter_library($library, $info['base theme']);
}
if (isset($info['quickedit_stylesheets'])) {
foreach ($info['quickedit_stylesheets'] as $path) {
$library['css']['theme']['/' . $theme_path . '/' . $path] = [];
}
}
}
}
};
$alter_library($libraries['quickedit'], $theme);
}
}
......
......@@ -801,6 +801,9 @@ function hook_library_info_alter(&$libraries, $module) {
* The name of the library.
*
* @see _drupal_add_library()
*
* @deprecated in Drupal 8.0.x, will be removed before Drupal 8.0.0
* Use hook_library_info_alter() and hook_js_settings_alter().
*/
function hook_library_alter(array &$library, $name) {
if ($name == 'core/jquery.ui.datepicker') {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment