You are here

public function SmartDateOverrideForm::buildForm in Smart Date 3.4.x

Same name and namespace in other branches
  1. 8.2 modules/smart_date_recur/src/Form/SmartDateOverrideForm.php \Drupal\smart_date_recur\Form\SmartDateOverrideForm::buildForm()
  2. 3.x modules/smart_date_recur/src/Form/SmartDateOverrideForm.php \Drupal\smart_date_recur\Form\SmartDateOverrideForm::buildForm()
  3. 3.0.x modules/smart_date_recur/src/Form/SmartDateOverrideForm.php \Drupal\smart_date_recur\Form\SmartDateOverrideForm::buildForm()
  4. 3.1.x modules/smart_date_recur/src/Form/SmartDateOverrideForm.php \Drupal\smart_date_recur\Form\SmartDateOverrideForm::buildForm()
  5. 3.2.x modules/smart_date_recur/src/Form/SmartDateOverrideForm.php \Drupal\smart_date_recur\Form\SmartDateOverrideForm::buildForm()
  6. 3.3.x modules/smart_date_recur/src/Form/SmartDateOverrideForm.php \Drupal\smart_date_recur\Form\SmartDateOverrideForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

modules/smart_date_recur/src/Form/SmartDateOverrideForm.php, line 35

Class

SmartDateOverrideForm
Form controller for Smart Date Recur instace override edit forms.

Namespace

Drupal\smart_date_recur\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $rrule = NULL, $index = NULL, $ajax = FALSE) {

  // TODO: Show some kind of entity summary? Title at least?
  $instances = $rrule
    ->getRuleInstances();
  if ($ajax) {
    $form['#prefix'] = '<div id="manage-instances">';
    $form['#suffix'] = '</div>';
  }

  // Get field config.
  $field_config = FieldConfig::loadByName($rrule
    ->get('entity_type')
    ->getString(), $rrule
    ->get('bundle')
    ->getString(), $rrule
    ->get('field_name')
    ->getString());
  $defaults = $field_config
    ->getDefaultValueLiteral()[0];
  $values['start'] = DrupalDateTime::createFromTimestamp($instances[$index]['value']);
  $values['end'] = DrupalDateTime::createFromTimestamp($instances[$index]['end_value']);
  $values['duration'] = ($instances[$index]['end_value'] - $instances[$index]['value']) / 60;
  $element = [
    'value' => [
      '#type' => 'datetime',
    ],
  ];
  SmartDateWidgetBase::createWidget($element, $values, $defaults);

  // Add the wrapper class for the Smart Date field so JS and CSS apply.
  $element['#attributes']['class'][] = 'smartdate--widget';
  $form['override'] = $element;
  $form['#attached']['library'][] = 'smart_date/smart_date';

  // Pass in values to identify the override.
  $form['rrule'] = [
    '#type' => 'hidden',
    '#value' => $rrule
      ->id(),
  ];
  $form['rrule_index'] = [
    '#type' => 'hidden',
    '#value' => $index,
  ];
  if (!empty($instances[$index]['oid'])) {
    $form['oid'] = [
      '#type' => 'hidden',
      '#value' => $instances[$index]['oid'],
    ];
  }
  if ($ajax) {
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Save'),
      '#button_type' => 'primary',
      '#ajax' => [
        'callback' => '::ajaxSubmit',
      ],
    ];
    $cancelurl = new Url('smart_date_recur.instances', [
      'rrule' => (int) $rrule
        ->id(),
      'modal' => TRUE,
    ]);
    $form['ajaxcancel'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Cancel'),
      '#attributes' => [
        'class' => [
          'button',
          'use-ajax',
        ],
      ],
      '#url' => $cancelurl,
      '#cache' => [
        'contexts' => [
          'url.query_args:destination',
        ],
      ],
    ];
  }
  else {
    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Save'),
      '#button_type' => 'primary',
    ];
  }
  return $form;
}