Commit 1c1e4d81 authored by git's avatar git Committed by Jonathan Smith
Browse files

Issue #2913829 by jonathan1055, Stockticker, eworwa: Pre-fill default time via...

Issue #2913829 by jonathan1055, Stockticker, eworwa: Pre-fill default time via javascript if the date is required
parent 3ebe5337
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -12,4 +12,10 @@ build:
        halt-on-fail: false
    testing:
      run_tests.standard:
        types: 'PHPUnit-Functional'
        suppress-deprecations: true
      run_tests.js:
        types: 'PHPUnit-FunctionalJavascript'
        suppress-deprecations: true
        concurrency: 1
        halt-on-fail: false
+33 −0
Original line number Diff line number Diff line
/**
 * @file
 * JQuery to set default time for Scheduler DateTime Widget.
 */

(function ($, drupalSettings) {

  'use strict';

  /**
   * Provide default time if schedulerDefaultTime is set.
   *
   * schedulerDefaultTime is defined in scheduler_form_node_form_alter when the
   * user is allowed to enter just a date. The value 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) {
      if (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);
          }
        });
      }
    }
  };
})(jQuery, drupalSettings);
+1 −0
Original line number Diff line number Diff line
@@ -16,3 +16,4 @@ test_dependencies:
libraries:
  - scheduler/admin
  - vertical-tabs
  - default-time
+5 −0
Original line number Diff line number Diff line
@@ -4,3 +4,8 @@ vertical-tabs:
  dependencies:
      - core/jquery
      - core/drupal.ajax
default-time:
  js:
    js/scheduler_default_time.js: {}
  dependencies:
    - core/jquery
+11 −3
Original line number Diff line number Diff line
@@ -108,7 +108,7 @@ function scheduler_form_node_form_alter(&$form, FormStateInterface $form_state)
    return;
  }

  $date_only_allowed = $config->get('allow_date_only');
  $allow_date_only = $config->get('allow_date_only');

  // A publish_on date is required if the content type option is set and the
  // node is being created or it currently has a scheduled publishing date.
@@ -153,14 +153,22 @@ function scheduler_form_node_form_alter(&$form, FormStateInterface $form_state)
  // Define the descriptions depending on whether the time can be skipped.
  $date_formatter = \Drupal::service('date.formatter');
  $descriptions = [];
  if ($date_only_allowed) {
  if ($allow_date_only) {
    $descriptions['format'] = t('Enter a date. The time part is optional.');
    // Show the default time so users know what they will get if they do not
    // enter a time.
    $default_time = strtotime($config->get('default_time'));
    $default_time_formatted = $date_formatter->format($default_time, 'custom', 'H:i:s');
    $descriptions['default'] = t('The default time is @default_time.', [
      '@default_time' => $date_formatter->format($default_time, 'custom', 'H:i:s'),
      '@default_time' => $default_time_formatted,
    ]);

    // 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_settings']['#attached']['library'][] = 'scheduler/default-time';
      $form['scheduler_settings']['#attached']['drupalSettings']['schedulerDefaultTime'] = $default_time_formatted;
    }
  }
  else {
    $descriptions['format'] = t('Enter a date and time.');
Loading