Programmatically fill the "Publish On" date field value provided by the Scheduler Module I'm using the Scheduler Module to schedule the publishing date of the content in my Drupal 8 site. I would like to programmatically fill the value of the Publish On date field provided by the scheduler module. 

In Drupal 7, I used the following code 

function hook_form_alter (&amp;$form, $form_state) {
     $date_format = (_scheduler_use_date_popup()) ? SCHEDULER_DATE_FORMAT : variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT);

      $form['scheduler_settings']['publish_on']['#default_value'] = format_date($querydate, 'custom', $date_format);
}


This code doesn't work in Drupal 8, since the _scheduler_use_date_popup() function and the SCHEDULER_DATE_FORMAT constant are not available. 

I'm using the following code in Drupal 8 which doesn't work:

function hook_form_id_alter(&amp;$form, FormStateInterface $form_state) {
    $form['scheduler_settings']['publish_on']['#default_value'] = \Drupal::service('date.formatter')-&gt;format($contest_start_date, 'custom', 'Y-m-d H:i:s'); 
}
You should be able to provide a default value using a hook_field_widget_form_alter() to modify the #value_callback of the TimestampDatetimeNoDefaultWidget that scheduler uses to a custom one, and this is how you can pass custom logic for a default value. Here's some sample code that could go in your custom module, you could add/modify conditions and logic as needed, but this should at least get things moving in the right direction:

&lt;?php

/**
 * @file
 * Publish On Default module file.
 *
 * This module provides a default value for the "Publish On" value
 * for nodes.
 */

use Drupal\Core\Datetime\Element\Datetime;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\scheduler\Plugin\Field\FieldWidget\TimestampDatetimeNoDefaultWidget;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_field_widget_form_alter().
 */
function mymodule_field_widget_datetime_timestamp_no_default_form_alter(&amp;$element, FormStateInterface $form_state, $context) {
  // Only alter the callback for the "Publish On" instance of the `datetime_timestamp_no_default` widget.
  if (isset($element['#title'])) {
    if ($element['#title']-&gt;__toString() == "Publish on") {
      $element['value']['#value_callback'] = 'mymodule_publish_on_default_value_callback';
    }
  }
}

/**
 * Callback function to add default time to the publish on widget.
 */
function mymodule_publish_on_default_value_callback(&amp;$element, $input, $form_state) {
  // If no default value, set the default to the current date/time.
  if (empty($element['#default_value'])) {
    $date_format = $element['#date_date_format'];
    $time_format = $element['#date_time_format'];
    $timezone = !empty($element['#date_timezone']) ? $element['#date_timezone'] : NULL;
    $date_time_format = trim($date_format . ' ' . $time_format);
    $date_time_input = date('Y-m-d H:i:s');
    $date = DrupalDateTime::createFromFormat($date_time_format, $date_time_input, $timezone);
    $element['#default_value'] = $date;
    $input = FALSE;
    return Datetime::valueCallback($element, $input, $form_state);
  }
  // Fallback to the original valueCallback() method.
  else {
    return TimestampDatetimeNoDefaultWidget::valueCallback($element, $input, $form_state);
  }
}