Commit c84e8551 authored by Vishal Khode's avatar Vishal Khode Committed by Jonathan Smith
Browse files

Issue #3314158 by jonathan1055, vishalkhode: Update javascript default time to...

Issue #3314158 by jonathan1055, vishalkhode: Update javascript default time to use core/once for D10
parent c2ad4e14
Loading
Loading
Loading
Loading
+3 −5
Changes for js/scheduler_default_time.js: 3 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@
 * JQuery to set default time for Scheduler DateTime Widget.
 */

(function ($, drupalSettings) {
(function ($, drupalSettings, once) {

  'use strict';

@@ -22,9 +22,7 @@
      // Drupal.behaviors are called many times per page. Using .once() adds the
      // class onto the matched DOM element and uses this to prevent it running
      // on subsequent calls.
      const $default_time = $(context)
        .find('#edit-scheduler-settings')
        .once('default-time-done');
      const $default_time = once('default-time-done', '#edit-scheduler-settings', context);

      if ($default_time.length && typeof drupalSettings.schedulerDefaultTime !== "undefined") {
        var operations = ["publish", "unpublish"];
@@ -51,4 +49,4 @@

    }
  };
})(jQuery, drupalSettings);
})(jQuery, drupalSettings, once);
+54 −0
Changes for js/scheduler_default_time_8x.js: 54 added lines, 0 removed lines.
Original line number Diff line number Diff line
/**
 * @file
 * JQuery to set default time for Scheduler DateTime Widget.
 *
 * This is a legacy version to maintain compatibility with Drupal 8.9.
 */

(function ($, drupalSettings) {

  'use strict';

  /**
   * Provide default time if schedulerDefaultTime is set.
   *
   * schedulerDefaultTime is defined in _scheduler_entity_form_alter when the
   * user is allowed to enter just a date. The values need to be pre-filled here
   * to avoid the browser validation 'please fill in this field' pop-up error
   * which is produced before the date widget valueCallback() can set the value.
   * @see https://www.drupal.org/project/scheduler/issues/2913829
   */
  Drupal.behaviors.setSchedulerDefaultTime = {
    attach: function (context) {

      // Drupal.behaviors are called many times per page. Using .once() adds the
      // class onto the matched DOM element and uses this to prevent it running
      // on subsequent calls.
      const $default_time = $(context).find('#edit-scheduler-settings').once('default-time-done');

      if ($default_time.length && typeof drupalSettings.schedulerDefaultTime !== "undefined") {
        var operations = ["publish", "unpublish"];
        operations.forEach(function (value) {
          var element = $("input#edit-" + value + "-on-0-value-time", context);
          // Only set the time when there is no value and the field is required.
          if (element.val() === "" && element.prop("required")) {
            element.val(drupalSettings.schedulerDefaultTime);
          }
        });
      }

      // Also use this jQuery behaviors function to set any pre-existing time
      // values with seconds removed if those drupalSettings values exist. This
      // is required by some browsers to make the seconds hidden.
      if (typeof drupalSettings.schedulerHideSecondsPublishOn !== "undefined") {
        var element = $("input#edit-publish-on-0-value-time", context);
        element.val(drupalSettings.schedulerHideSecondsPublishOn);
      }
      if (typeof drupalSettings.schedulerHideSecondsUnpublishOn !== "undefined") {
        var element = $("input#edit-unpublish-on-0-value-time", context);
        element.val(drupalSettings.schedulerHideSecondsUnpublishOn);
      }

    }
  };
})(jQuery, drupalSettings);
+6 −0
Changes for scheduler.libraries.yml: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -9,6 +9,12 @@ default-time:
    js/scheduler_default_time.js: {}
  dependencies:
    - core/jquery
    - core/once
default-time-8x:
  js:
    js/scheduler_default_time_8x.js: {}
  dependencies:
    - core/jquery
admin-css:
  css:
    component:
+8 −3
Changes for scheduler.module: 8 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -217,6 +217,11 @@ function _scheduler_entity_form_alter(&$form, FormStateInterface $form_state, $f
    $form[$scheduler_field_group]['#attached']['library'][] = 'scheduler/vertical-tabs';
  }

  // The 'once' library was moved from jQuery into core at 9.2. The original js
  // library file is kept to maintain compatibility with Drupal 8.9.
  // @see https://www.drupal.org/project/scheduler/issues/3314158
  $default_time_library = version_compare(\Drupal::VERSION, '9.2', '>=') ? 'scheduler/default-time' : 'scheduler/default-time-8x';

  // Define the descriptions depending on whether the time can be skipped.
  $descriptions = [];
  if ($allow_date_only) {
@@ -230,7 +235,7 @@ function _scheduler_entity_form_alter(&$form, FormStateInterface $form_state, $f
    // Use javascript to pre-fill the time parts if the dates are required.
    // See js/scheduler_default_time.js for more details.
    if ($publishing_required || $unpublishing_required) {
      $form[$scheduler_field_group]['#attached']['library'][] = 'scheduler/default-time';
      $form[$scheduler_field_group]['#attached']['library'][] = $default_time_library;
      $form[$scheduler_field_group]['#attached']['drupalSettings']['schedulerDefaultTime'] = $config->get('default_time');
    }
  }
@@ -264,12 +269,12 @@ function _scheduler_entity_form_alter(&$form, FormStateInterface $form_state, $f
  if ($config->get('hide_seconds')) {
    // If there is a publish_on time, then use jQuery to remove the seconds.
    if (isset($entity->publish_on->value)) {
      $form[$scheduler_field_group]['#attached']['library'][] = 'scheduler/default-time';
      $form[$scheduler_field_group]['#attached']['library'][] = $default_time_library;
      $form[$scheduler_field_group]['#attached']['drupalSettings']['schedulerHideSecondsPublishOn'] = date('H:i', $entity->publish_on->value);
    }
    // Likewise for the unpublish_on time.
    if (isset($entity->unpublish_on->value)) {
      $form[$scheduler_field_group]['#attached']['library'][] = 'scheduler/default-time';
      $form[$scheduler_field_group]['#attached']['library'][] = $default_time_library;
      $form[$scheduler_field_group]['#attached']['drupalSettings']['schedulerHideSecondsUnpublishOn'] = date('H:i', $entity->unpublish_on->value);
    }
  }