You are here

public function AutosaveEntityFormDatabaseStorage::getFormState in Autosave Form 8

Retrieves the form state.

The timestamp of the autosaved state will be set into the form state storage under the key 'autosave_form_state_timestamp'.

Parameters

$form_id: The form id.

string $entity_type_id: The entity type id of the entity.

mixed $entity_id: The id of the entity.

string $langcode: The language code of the original entity.

$uid: The user id.

$form_session_id: (optional) The form session id.

$timestamp: (optional) The timestamp for which to load the form state.

Return value

\Drupal\Core\Form\FormStateInterface The form state.

Overrides AutosaveEntityFormStorageInterface::getFormState

File

src/Storage/AutosaveEntityFormDatabaseStorage.php, line 158

Class

AutosaveEntityFormDatabaseStorage
A database backend for autosave of entity forms.

Namespace

Drupal\autosave_form\Storage

Code

public function getFormState($form_id, $entity_type_id, $entity_id, $langcode, $uid, $form_session_id = NULL, $autosaved_timestamp = NULL) {
  $form_state = NULL;
  $query = $this->connection
    ->select(static::AUTOSAVE_ENTITY_FORM_TABLE, 'cefa')
    ->fields('cefa', [
    'form_state',
    'timestamp',
  ])
    ->orderBy('timestamp', 'DESC')
    ->condition('form_id', $form_id);
  if (isset($form_session_id)) {
    $query
      ->condition('form_session_id', $form_session_id);
  }
  $query
    ->condition('entity_type_id', $entity_type_id)
    ->condition('entity_id', $entity_id)
    ->condition('langcode', $langcode)
    ->condition('uid', $uid);
  if (isset($autosaved_timestamp)) {
    $query
      ->condition('timestamp', $autosaved_timestamp);
  }
  $data = $query
    ->execute()
    ->fetchAssoc();
  if ($data) {

    // Prepare the form state.
    $form_state_data = $this->serializer
      ->decode($data['form_state']);
    $form_state = new FormState();
    $form_state
      ->setStorage($form_state_data['storage']);
    $form_state
      ->setUserInput($form_state_data['input']);
    $form_state
      ->set('autosave_form_state_timestamp', $data['timestamp']);
  }
  return $form_state;
}