You are here

function fullcalendar_view_form_node_form_alter in Fullcalendar View 8

Same name and namespace in other branches
  1. 8.3 fullcalendar_view.module \fullcalendar_view_form_node_form_alter()
  2. 8.2 fullcalendar_view.module \fullcalendar_view_form_node_form_alter()
  3. 6.x fullcalendar_view.module \fullcalendar_view_form_node_form_alter()
  4. 5.x fullcalendar_view.module \fullcalendar_view_form_node_form_alter()

Implements hook_form_BASE_FORM_ID_alter().

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

File

./fullcalendar_view.module, line 30
Full Canlendar Views module help and theme functions.

Code

function fullcalendar_view_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)) {
    $node = $form_state
      ->getFormObject()
      ->getEntity();

    // Only handle new node with the start field.
    if ($node
      ->isNew() && isset($form[$start_field]) && isset($form[$start_field]['widget'][0])) {

      // Only handle datetime field.
      if ($form[$start_field]['widget'][0]['value']['#type'] === 'datetime') {

        // Prepopulate the start date field as the event date.
        $form[$start_field]['widget'][0]['value']['#default_value'] = new DrupalDateTime($start);
      }
    }
  }
}