View source
<?php
namespace Drupal\autosave_form\Storage;
use Drupal\Component\Serialization\SerializationInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\ContentEntityFormInterface;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
class AutosaveEntityFormDatabaseStorage implements AutosaveEntityFormStorageInterface {
protected $connection;
protected $serializer;
public function __construct(Connection $connection, SerializationInterface $serializer) {
$this->connection = $connection;
$this->serializer = $serializer;
}
public function storeEntityAndFormState($form_id, $form_session_id, $entity_type_id, $entity_id, $langcode, $uid, $timestamp, EntityInterface $entity, FormStateInterface $form_state) {
$serialized_entity = $this
->serializeEntity($entity, $form_state);
$serialized_form_state = $this->serializer
->encode([
'storage' => $form_state
->getStorage(),
'input' => $form_state
->getUserInput(),
]);
if (!$entity
->isNew()) {
$this->connection
->insert(static::AUTOSAVE_ENTITY_FORM_TABLE)
->fields([
'form_id',
'form_session_id',
'entity_type_id',
'entity_id',
'langcode',
'uid',
'timestamp',
'entity',
'form_state',
])
->values([
$form_id,
$form_session_id,
$entity_type_id,
$entity_id,
$langcode,
$uid,
$timestamp,
$serialized_entity,
$serialized_form_state,
])
->execute();
}
}
public function getEntityAndFormState($form_id, $entity_type_id, $entity_id, $langcode, $uid, $form_session_id = NULL, $autosaved_timestamp = NULL) {
$result = NULL;
$query = $this->connection
->select(static::AUTOSAVE_ENTITY_FORM_TABLE, 'cefa')
->fields('cefa', [
'entity',
'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) {
$result['entity'] = $this->serializer
->decode($data['entity']);
$form_state_data = $this->serializer
->decode($data['form_state']);
$result['form_state'] = new FormState();
$result['form_state']
->setStorage($form_state_data['storage']);
$result['form_state']
->setUserInput($form_state_data['input']);
$result['form_state']
->set('autosave_form_state_timestamp', $data['timestamp']);
}
return $result;
}
public function getEntity($form_id, $entity_type_id, $entity_id, $langcode, $uid, $form_session_id = NULL, $autosaved_timestamp = NULL) {
$entity = NULL;
$query = $this->connection
->select(static::AUTOSAVE_ENTITY_FORM_TABLE, 'cefa')
->fields('cefa', [
'entity',
])
->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) {
$entity = $this->serializer
->decode($data['entity']);
}
return $entity;
}
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) {
$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;
}
public function hasAutosavedStateForFormState(FormStateInterface $form_state, $uid) {
$result = FALSE;
if (($form_object = $form_state
->getFormObject()) && $form_object instanceof EntityFormInterface && ($entity = $form_object
->getEntity()) && !$entity
->isNew()) {
$result = $this
->hasAutosavedState($form_object
->getFormId(), $entity
->getEntityTypeId(), $entity
->id(), $entity
->language()
->getId(), $uid, $form_state
->get('autosave_form_session_id'));
}
return $result;
}
public function hasAutosavedState($form_id, $entity_type_id, $entity_id, $langcode, $uid, $form_session_id = NULL) {
$query = $this->connection
->select(static::AUTOSAVE_ENTITY_FORM_TABLE, 'cefa')
->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);
$count = $query
->countQuery()
->execute()
->fetchField();
return (bool) $count;
}
public function getLastAutosavedStateTimestamp($form_id, $entity_type_id, $entity_id, $langcode, $uid) {
$timestamp = $this->connection
->select(static::AUTOSAVE_ENTITY_FORM_TABLE, 'cefa')
->fields('cefa', [
'timestamp',
])
->orderBy('timestamp', 'DESC')
->condition('form_id', $form_id)
->condition('entity_type_id', $entity_type_id)
->condition('entity_id', $entity_id)
->condition('langcode', $langcode)
->condition('uid', $uid)
->execute()
->fetchField();
return is_bool($timestamp) ? NULL : $timestamp;
}
public function purgeAutosavedEntityState($entity_type_id, $entity_id, $form_session_id = NULL, $form_id = NULL, $langcode = NULL, $uid = NULL) {
$query = $this->connection
->delete(static::AUTOSAVE_ENTITY_FORM_TABLE);
if (isset($form_session_id)) {
$query
->condition('form_session_id', $form_session_id);
}
if (isset($form_id)) {
$query
->condition('form_id', $form_id);
}
$query
->condition('entity_type_id', $entity_type_id)
->condition('entity_id', $entity_id);
if (isset($langcode)) {
$query
->condition('langcode', $langcode);
}
if (isset($uid)) {
$query
->condition('uid', $uid);
}
$query
->execute();
}
public function purgeAutosavedEntitiesStates($entity_type_id = NULL, $langcode = NULL, $uid = NULL) {
$query = $this->connection
->delete(static::AUTOSAVE_ENTITY_FORM_TABLE);
if (isset($entity_type_id)) {
$query
->condition('entity_type_id', $entity_type_id);
}
if (isset($langcode)) {
$query
->condition('langcode', $langcode);
}
if (isset($uid)) {
$query
->condition('uid', $uid);
}
$query
->execute();
}
protected function serializeEntity(EntityInterface $entity, FormStateInterface $form_state) {
if (($form_object = $form_state
->getFormObject()) && $form_object instanceof ContentEntityFormInterface && method_exists($form_object, 'isEntityDeepSerializationRequired') && $form_object
->isEntityDeepSerializationRequired()) {
if (method_exists($entity, 'setDeepSerialization')) {
$entity
->setDeepSerialization(TRUE);
}
}
return $this->serializer
->encode($entity);
}
}