diff --git a/core/core.libraries.yml b/core/core.libraries.yml index 633bb87..b610162 100644 --- a/core/core.libraries.yml +++ b/core/core.libraries.yml @@ -138,7 +138,6 @@ drupal.date: misc/date.js: {} dependencies: - core/drupal - - core/drupalSettings - core/modernizr - core/jquery.ui.datepicker diff --git a/core/lib/Drupal/Core/Render/Element/Date.php b/core/lib/Drupal/Core/Render/Element/Date.php index db04fa9..607d243 100644 --- a/core/lib/Drupal/Core/Render/Element/Date.php +++ b/core/lib/Drupal/Core/Render/Element/Date.php @@ -60,19 +60,7 @@ public static function processDate(&$element, FormStateInterface $form_state, &$ // Attach JS support, if we can determine which date format should be used. if ($element['#attributes']['type'] == 'date' && !empty($element['#date_date_format'])) { $element['#attached']['library'][] = 'core/drupal.date'; - $format = $element['#date_date_format']; - // Convert the format into jQuery UI settings. - $format = str_replace('Y', 'yy', $format); - $format = str_replace('m', 'mm', $format); - $format = str_replace('d', 'dd', $format); - $settings = array('dateFormat' => $format); - if (!empty($element['#attributes']['min'])) { - $settings['minDate'] = $element['#attributes']['min']; - } - if (!empty($element['#attributes']['max'])) { - $settings['maxDate'] = $element['#attributes']['max']; - } - $element['#attached']['drupalSettings']['date'][$element['#id']] = $settings; + $element['#attributes']['data-drupal-date-format'] = array($element['#date_date_format']); } return $element; } diff --git a/core/misc/date.js b/core/misc/date.js index e02041c..75f446b 100644 --- a/core/misc/date.js +++ b/core/misc/date.js @@ -1,4 +1,4 @@ -(function ($, Modernizr, Drupal, drupalSettings) { +(function ($, Modernizr, Drupal) { "use strict"; @@ -18,11 +18,37 @@ var $context = $(context); // Only act if the browser doesn't support date input type. if (Modernizr.inputtypes.date === false) { - $.each(settings.date, function(id, datepickerSettings) { - $context.find("#" + id).datepicker(datepickerSettings); + $context.find('input[data-drupal-date-format]').each(function() { + var datepickerSettings = {}, dateFormat; + dateFormat = $(this).data('drupalDateFormat'); + // The date format is saved in PHP style, we need to convert to jQery + // datepicker. + dateFormat = dateFormat + .replace('Y', 'yy') + .replace('m', 'mm') + .replace('d', 'dd'); + datepickerSettings.dateFormat = dateFormat; + // Add min and max date if set on the input. + if ($(this).attr('min')) { + datepickerSettings.minDate = $(this).attr('min'); + } + if ($(this).attr('max')) { + datepickerSettings.maxDate = $(this).attr('max'); + } + $(this).datepicker(datepickerSettings); }); } + }, + /** + * Detach the behavior destroying datepickers on effected elements. + */ + detach: function (context, settings, trigger) { + if (trigger === 'unload') { + $(context).find('input[data-drupal-date-format].hasDatepicker') + .datepicker("destroy") + .removeClass("hasDatepicker"); + } } }; -})(jQuery, Modernizr, Drupal, drupalSettings); +})(jQuery, Modernizr, Drupal);