You are here

function smart_date_form_node_form_alter in Smart Date 3.0.x

Same name and namespace in other branches
  1. 3.1.x smart_date.module \smart_date_form_node_form_alter()
  2. 3.2.x smart_date.module \smart_date_form_node_form_alter()
  3. 3.3.x smart_date.module \smart_date_form_node_form_alter()
  4. 3.4.x smart_date.module \smart_date_form_node_form_alter()

Implements hook_form_BASE_FORM_ID_alter().

Prepopulate the smart date field with the date passed from query parameter.

File

./smart_date.module, line 86
Field hooks for a field that stores a start and end date as timestamps.

Code

function smart_date_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Event start date from query parameter.
  $start = \Drupal::request()->query
    ->get('start');

  // Field name of the start date from query parameter.
  $start_field = \Drupal::request()->query
    ->get('start_field');
  if (empty($start) || empty($start_field)) {

    // Incomplete data, nothing to do.
    return;
  }
  if (!isset($form[$start_field]['#attributes']['class']) || !is_array($form[$start_field]['#attributes']['class']) || !in_array('field--type-smartdate', $form[$start_field]['#attributes']['class'])) {

    // Not a Smart Date field.
    return;
  }
  $node = $form_state
    ->getFormObject()
    ->getEntity();
  if (!$node
    ->isNew()) {

    // Only populate new nodes.
    return;
  }

  // This is a new node, so properly process Smart Date default times.
  $field_definition = $node
    ->get($start_field)
    ->first()
    ->getFieldDefinition();
  $defaults = $field_definition
    ->getDefaultValueLiteral();
  if (is_array($defaults)) {
    $defaults = array_pop($defaults);
  }
  if (strpos($start, ' ')) {

    // The datetime string shouldn't contain a space, so if there assume it
    // should be a +.
    $start = str_replace(' ', '+', $start);
  }
  $start_time = new DrupalDateTime($start);
  $end_time = NULL;
  $start_str_len = strlen($start);
  if ($start_str_len == 10) {

    // If only date supplied, populate time based on default setting.
    switch ($defaults['default_date_type']) {

      // Set start time based on what would be used as default.
      case 'next_hour':
        $current_time = new DrupalDateTime();
        $next_hour = $current_time
          ->format('H');
        if ($current_time
          ->format('i') != 0) {
          $next_hour++;
        }
        $start_time
          ->setTime($next_hour, 0);
        break;
      case 'now':
        $current_time = new DrupalDateTime();
        $start_time
          ->setTime($current_time
          ->format('H'), $current_time
          ->format('i'));
        break;
      case 'relative':
        $relative_time = DrupalDateTime::createFromTimestamp(strtotime($defaults['default_date']));
        $start_time
          ->setTime($relative_time
          ->format('H'), $relative_time
          ->format('i'));
        break;
      default:

        // No default set, so assume it should be all day.
        $end_time = clone $start_time;
        $end_time
          ->setTime(23, 59);
    }
  }
  $form[$start_field]['widget'][0]['value']['#default_value'] = $start_time;
  if (!$end_time) {

    // Offset the end time based on default duration.
    $end_time = clone $start_time;
    $end_time
      ->modify('+' . $defaults['default_duration'] . ' minutes');
  }
  $form[$start_field]['widget'][0]['end_value']['#default_value'] = $end_time;
}