You are here

public function AutosaveEntityFormHandler::isAutosaveSubmitValid in Autosave Form 8

Checks if the autosave submit is allowed.

Parameters

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

Return value

boolean TRUE if the autosave submission should be executed, FALSE otherwise.

Overrides AutosaveFormInterface::isAutosaveSubmitValid

File

src/Form/AutosaveEntityFormHandler.php, line 229

Class

AutosaveEntityFormHandler
Base class for autosave form handlers.

Namespace

Drupal\autosave_form\Form

Code

public function isAutosaveSubmitValid(FormStateInterface $form_state) {
  list($form_id, $entity) = $this
    ->getFormIDandEntity($form_state);

  // Check that the entity is still valid.
  if ($entity instanceof EntityChangedInterface) {
    $changed_time = $entity
      ->getChangedTime();
    $input = $form_state
      ->getUserInput();
    $changed_form_value = isset($input['changed']) ? $input['changed'] : NULL;
    $entity
      ->setChangedTime($changed_form_value ?: $changed_time);
    if (!$this->conflictEnabled && ($unchanged = $this->entityStorage
      ->loadUnchanged($entity
      ->id())) && $unchanged
      ->getChangedTimeAcrossTranslations() > $entity
      ->getChangedTimeAcrossTranslations()) {
      $form_state
        ->setTemporaryValue('autosave_form_last_autosave_timestamp', 'entity_saved_meanwhile');
      return FALSE;
    }
    else {
      $entity
        ->setChangedTime($changed_time);
    }
  }
  return TRUE;
}