You are here

public function AutosaveFormAlterTrait::formAlter in Autosave Form 8

Performs the needed alterations to the form.

Parameters

array $form: The form to be altered to provide the autosave form support.

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

1 call to AutosaveFormAlterTrait::formAlter()
AutosaveEntityFormHandler::formAlter in src/Form/AutosaveEntityFormHandler.php
Performs the needed alterations to the entity form.

File

src/Form/AutosaveFormAlterTrait.php, line 26

Class

AutosaveFormAlterTrait
Provides a trait for common autosave form alterations.

Namespace

Drupal\autosave_form\Form

Code

public function formAlter(array &$form, FormStateInterface $form_state) {
  if (!$this
    ->isAutosaveEnabled($form_state)) {
    return;
  }
  $form['#attributes']['class'][] = 'autosave-form';
  $form['#attached']['library'][] = 'autosave_form/drupal.autosave_form';
  $form['#attached']['drupalSettings']['autosaveForm']['interval'] = $this->configFactory
    ->get('autosave_form.settings')
    ->get('interval');
  $form['#attached']['drupalSettings']['autosaveForm']['onlyOnFormChange'] = $this->configFactory
    ->get('autosave_form.settings')
    ->get('only_on_form_change');
  $form['#attached']['drupalSettings']['autosaveForm']['notification'] = $this->configFactory
    ->get('autosave_form.settings')
    ->get('notification');
  $input = $form_state
    ->getUserInput();
  $show_restore_discard = !$form_state
    ->isRebuilding() ?: !empty($input['autosave_restore_discard']);
  if ($show_restore_discard && !$form_state
    ->get('autosave_form_state_timestamp') && !$form_state
    ->get('autosave_form_rejected') && ($autosave_form_state_timestamp = $this
    ->getLastAutosavedTimestamp($form_state, $this->currentUser
    ->id()))) {
    $form[AutosaveFormInterface::AUTOSAVE_RESTORE_ELEMENT_NAME] = [
      '#type' => 'submit',
      '#name' => AutosaveFormInterface::AUTOSAVE_RESTORE_ELEMENT_NAME,
      '#value' => $this
        ->t('Autosave restore'),
      '#limit_validation_errors' => [],
      '#attributes' => [
        'class' => [
          'autosave-form-restore',
          'visually-hidden',
        ],
      ],
      '#submit' => [
        [
          $this,
          'autosaveFormRestoreSubmit',
        ],
      ],
      '#autosave_form_state_timestamp' => $autosave_form_state_timestamp,
    ];
    $form[AutosaveFormInterface::AUTOSAVE_REJECT_ELEMENT_NAME] = [
      '#type' => 'submit',
      '#name' => 'autosave_form_reject',
      '#value' => $this
        ->t('Autosave reject'),
      '#limit_validation_errors' => [],
      '#attributes' => [
        'class' => [
          'autosave-form-reject',
          'visually-hidden',
        ],
      ],
      '#submit' => [
        [
          $this,
          'autosaveFormRejectSubmit',
        ],
      ],
      '#ajax' => [
        'callback' => [
          $this,
          'autosaveFormRejectAjax',
        ],
      ],
    ];
    $form['autosave_restore_discard'] = [
      '#type' => 'hidden',
      '#default_value' => 'autosave_restore_discard',
      '#attributes' => [
        'class' => [
          'autosave-form-restore-discard',
        ],
      ],
    ];

    // Add the message to be shown on the form. Our JS library will check if
    // the message exist and only then offer the options for restore and
    // reject, otherwise will start the auto save process.
    $date = $this->dateFormatter
      ->format($autosave_form_state_timestamp, 'custom', 'M d, Y H:i');
    $message = $this
      ->t('A version of this page you were editing at @date was saved as a draft. Do you want to resume editing or discard it?', [
      '@date' => $date,
    ]);
    $form['#attached']['drupalSettings']['autosaveForm']['message'] = (string) $message;
  }
  $autosave_form_session_id = $this
    ->getAutosaveFormSessionID($form_state);
  if (!$autosave_form_session_id) {
    $autosave_form_session_id = !empty($input['autosave_form_session_id']) ? $input['autosave_form_session_id'] : $form['#build_id'];
    $this
      ->setAutosaveFormSessionID($form_state, $autosave_form_session_id);
  }
  $form['autosave_form_session_id'] = [
    '#type' => 'hidden',
    '#value' => $autosave_form_session_id,
    '#name' => 'autosave_form_session_id',
    // Form processing and validation requires this value, so ensure the
    // submitted form value appears literally, regardless of custom #tree
    // and #parents being set elsewhere.
    '#parents' => [
      'autosave_form_session_id',
    ],
  ];
  $form[AutosaveFormInterface::AUTOSAVE_ELEMENT_NAME] = [
    '#type' => 'submit',
    '#name' => AutosaveFormInterface::AUTOSAVE_ELEMENT_NAME,
    '#value' => $this
      ->t('Autosave save'),
    '#attributes' => [
      'class' => [
        'autosave-form-save',
        'visually-hidden',
      ],
    ],
    '#submit' => [
      [
        $this,
        'autosaveFormSubmit',
      ],
    ],
    '#ajax' => [
      'callback' => [
        $this,
        'autosaveFormAjax',
      ],
      // Do not refocus to prevent losing focus of the element the user might
      // be currently editing when the autosave submission is triggered.
      'disable-refocus' => TRUE,
      'progress' => FALSE,
    ],
    '#autosave_form' => TRUE,
    // Retrieve the "autosave_form_session_id" also from the form state as on
    // autosave restore the one from the restored state will be present in
    // the form state storage and we want to continue using that session for
    // the further autosave states after the restoration.
    '#autosave_form_session_id' => $autosave_form_session_id,
  ];
  $form['autosave_form_last_autosave_timestamp'] = [
    '#type' => 'hidden',
    '#name' => 'autosave_form_last_autosave_timestamp',
    '#value' => $form_state
      ->get('autosave_form_last_autosave_timestamp') ?: '',
  ];
}