You are here

public function EditUpdateForm::buildForm in Scheduled Publish 8.3

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

src/Form/EditUpdateForm.php, line 30
Contains \Drupal\scheduled_publish\Form\EditUpdateForm.

Class

EditUpdateForm

Namespace

Drupal\scheduled_publish\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $entity = NULL, $field_delta = NULL) {
  if (!isset($entity) || !isset($field_delta)) {
    $form['message'] = [
      '#theme_wrappers' => [
        'container',
      ],
      '#markup' => $this
        ->t('A valid entity and field delta must be provided.'),
    ];
    return $form;
  }
  $fields = $this
    ->getScheduledFields($entity
    ->getEntityTypeId(), $entity
    ->bundle());
  $field = reset($fields);
  $states = $entity
    ->get($field)
    ->getValue();
  if (!isset($states[$field_delta])) {
    $form['message'] = [
      '#theme_wrappers' => [
        'container',
      ],
      '#markup' => $this
        ->t('This status update does not exist.'),
    ];
    return $form;
  }

  // Save data into form_state.
  $form_state
    ->set([
    'scheduled_publish',
    'entity',
  ], $entity);
  $form_state
    ->set([
    'scheduled_publish',
    'field',
  ], $field);
  $form_state
    ->set([
    'scheduled_publish',
    'field_delta',
  ], $field_delta);
  $entity_info = $entity
    ->label() . ' (' . $entity
    ->id() . ')';
  $form['#title'] = $this
    ->t('Edit status update for the "@node" node', [
    '@node' => $entity_info,
  ]);
  $form['message'] = [
    '#theme_wrappers' => [
      'container',
    ],
    '#markup' => $this
      ->t('If this state change invalidates any existing transitions those will be deleted.'),
  ];
  $prev_state = $states[$field_delta - 1] ?? FALSE;
  if ($prev_state) {
    $orig_status = $entity->moderation_state->value;
    $entity->moderation_state->value = $prev_state['moderation_state'];
  }
  $m_options = $this
    ->getModerationOptions($entity);
  if ($prev_state) {
    $entity->moderation_state->value = $orig_status;
  }
  $form['moderation_state'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Moderation state change'),
    '#options' => $m_options,
    '#default_value' => isset($m_options[$states[$field_delta]['moderation_state']]) ? $states[$field_delta]['moderation_state'] : NULL,
    '#required' => TRUE,
  ];
  $form['value'] = [
    '#type' => 'datetime',
    '#title' => $this
      ->t('Scheduled date'),
    '#description' => $this
      ->t('Date & time of the scheduled state change'),
    '#date_increment' => 1,
    '#date_timezone' => date_default_timezone_get(),
    '#default_value' => new DrupalDateTime($states[$field_delta]['value'], ScheduledPublish::STORAGE_TIMEZONE),
    '#required' => TRUE,
  ];
  $form['actions'] = [
    '#type' => 'container',
    '#weight' => 10,
  ];
  $form['actions']['save'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
    '#attributes' => [
      'class' => [
        'button',
        'button--primary',
      ],
    ],
  ];
  $query = \Drupal::service('request_stack')
    ->getCurrentRequest()->query;
  $url = Url::fromRoute('view.scheduled_publish.page_1');
  if ($query
    ->has('destination')) {
    $options = UrlHelper::parse($query
      ->get('destination'));
    try {
      $url = Url::fromUserInput('/' . ltrim($options['path'], '/'), $options);
    } catch (\InvalidArgumentException $e) {

      // Suppress the exception.
    }
  }
  if ($url) {
    $form['actions']['cancel'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Cancel'),
      '#attributes' => [
        'class' => [
          'button',
        ],
      ],
      '#url' => $url,
    ];
  }
  return $form;
}