You are here

class AutosaveEntityFormHandler in Autosave Form 8

Base class for autosave form handlers.

Hierarchy

Expanded class hierarchy of AutosaveEntityFormHandler

1 file declares its use of AutosaveEntityFormHandler
autosave_form.module in ./autosave_form.module
This module holds autosave form functionality.

File

src/Form/AutosaveEntityFormHandler.php, line 29

Namespace

Drupal\autosave_form\Form
View source
class AutosaveEntityFormHandler implements AutosaveEntityFormHandlerInterface, EntityHandlerInterface {
  use DependencySerializationTrait;
  use AutosaveFormAlterTrait {
    autosaveFormAjax as traitAutosaveFormAjax;
    formAlter as traitFormAlter;
  }

  /**
   * The type of the entity for whose form the autosave form is used.
   *
   * @var string
   */
  protected $entityTypeId;

  /**
   * Information about the entity type.
   *
   * @var \Drupal\Core\Entity\EntityTypeInterface
   */
  protected $entityType;

  /**
   * The entity storage.
   *
   * Will be used to load the unchanged entity.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $entityStorage;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The autosave form storage.
   *
   * @var \Drupal\autosave_form\Storage\AutosaveEntityFormStorageInterface
   */
  protected $autosaveEntityFormStorage;

  /**
   * The time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The factory for expirable key value stores used by form cache.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface
   */
  protected $keyValueExpirableFactory;

  /**
   * Defines whether the conflict module is enabled or not.
   *
   * @var bool
   */
  protected $conflictEnabled;

  /**
   * Initializes an instance of the autosave form controller.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity type manager.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\autosave_form\Storage\AutosaveEntityFormStorageInterface $autosave_entity_form_storage
   *   The autosave form storage service.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface $key_value_expirable_factory
   *   The key value expirable factory, used to create key value expirable
   *   stores for the form cache and form state cache.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
   */
  public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user, AutosaveEntityFormStorageInterface $autosave_entity_form_storage, TimeInterface $time, DateFormatterInterface $date_formatter, ConfigFactoryInterface $config_factory, KeyValueExpirableFactoryInterface $key_value_expirable_factory, ModuleHandlerInterface $module_handler) {
    $this->entityTypeId = $entity_type
      ->id();
    $this->entityType = $entity_type;
    $this->entityStorage = $entity_type_manager
      ->getStorage($entity_type
      ->id());
    $this->currentUser = $current_user;
    $this->autosaveEntityFormStorage = $autosave_entity_form_storage;
    $this->time = $time;
    $this->dateFormatter = $date_formatter;
    $this->configFactory = $config_factory;
    $this->keyValueExpirableFactory = $key_value_expirable_factory;
    $this->conflictEnabled = $module_handler
      ->moduleExists('conflict');
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static($entity_type, $container
      ->get('entity_type.manager'), $container
      ->get('current_user'), $container
      ->get('autosave_form.entity_form_storage'), $container
      ->get('datetime.time'), $container
      ->get('date.formatter'), $container
      ->get('config.factory'), $container
      ->get('keyvalue.expirable'), $container
      ->get('module_handler'));
  }

  /**
   * Performs the needed alterations to the entity form.
   *
   * @param array $form
   *   The entity form to be altered to provide the autosave functionality.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public function formAlter(array &$form, FormStateInterface $form_state) {
    $this
      ->traitFormAlter($form, $form_state);

    // Flag the entity with the current autosave session ID.
    $form['#entity_builders'][] = [
      static::class,
      'entityFormEntityBuild',
    ];
  }

  /**
   * Ajax callback for autosaving content entity forms.
   */
  public function autosaveFormAjax($form, FormStateInterface $form_state) {
    $response = $this
      ->traitAutosaveFormAjax($form, $form_state);
    $timestamp = $form_state
      ->getTemporaryValue('autosave_form_last_autosave_timestamp');
    if ($timestamp == 'entity_saved_meanwhile') {
      $input = $form_state
        ->getUserInput();

      // Autosave has run already or this is the first run.
      if (is_numeric($input['autosave_form_last_autosave_timestamp']) || empty($input['autosave_form_last_autosave_timestamp'])) {

        // Alert the user about turning off autosave and deleting all the
        // autosaved states.
        $message = $this->configFactory
          ->get('autosave_form.messages')
          ->get('entity_saved_in_background_alert_message');
        $options = [
          'width' => 'auto',
          'closeOnEscape' => FALSE,
        ];
        $response
          ->addCommand(new OpenAutosaveDisabledDialog($this
          ->t('Autosave has been disabled'), $message, $options));

        // Reset the last autosaved timestamp on the page.
        $response
          ->addCommand(new InvokeCommand('input[name="autosave_form_last_autosave_timestamp"]', 'attr', [
          'value',
          (string) $this
            ->t('Autosave is turned off.'),
        ]));

        // Stop the autosave submissions.
        $attachments = [];
        $attachments['drupalSettings']['autosaveForm']['autosaveFormRunning'] = FALSE;
        $response
          ->addAttachments($attachments);
      }
    }
    return $response;
  }

  /**
   * Entity builder method.
   *
   * Flags the entity with the autosave form session ID.
   *
   * @param string $entity_type
   *   The type of the entity.
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity whose form is being built.
   */
  public static function entityFormEntityBuild($entity_type, EntityInterface $entity, array $form, FormStateInterface $form_state) {
    if ($autosave_form_session_id = static::getAutosaveFormSessionID($form_state)) {
      $entity->{static::AUTOSAVE_SESSION_ID} = $autosave_form_session_id;
    }
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function getLastAutosavedFormState(FormStateInterface $form_state, $autosave_form_session_id, $uid) {
    list($form_id, $entity) = $this
      ->getFormIDandEntity($form_state);
    return $this->autosaveEntityFormStorage
      ->getFormState($form_id, $entity
      ->getEntityTypeId(), $entity
      ->id(), $entity
      ->language()
      ->getId(), $uid, $autosave_form_session_id);
  }

  /**
   * {@inheritdoc}
   */
  public function storeState(FormStateInterface $form_state, $autosave_form_session_id, $autosave_timestamp, $uid) {
    list($form_id, $entity) = $this
      ->getFormIDandEntity($form_state);
    $this->autosaveEntityFormStorage
      ->storeEntityAndFormState($form_id, $autosave_form_session_id, $entity
      ->getEntityTypeId(), $entity
      ->id(), $entity
      ->language()
      ->getId(), $uid, $autosave_timestamp, $entity, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function getLastAutosavedTimestamp(FormStateInterface $form_state, $uid) {
    list($form_id, $entity) = $this
      ->getFormIDandEntity($form_state);
    return $entity
      ->isNew() ? NULL : $this->autosaveEntityFormStorage
      ->getLastAutosavedStateTimestamp($form_id, $entity
      ->getEntityTypeId(), $entity
      ->id(), $entity
      ->language()
      ->getId(), $uid);
  }

  /**
   * {@inheritdoc}
   */
  public function purgeCurrentAutosavedState(FormStateInterface $form_state, $uid) {
    list($form_id, $entity) = $this
      ->getFormIDandEntity($form_state);
    $this->autosaveEntityFormStorage
      ->purgeAutosavedEntityState($entity
      ->getEntityTypeId(), $entity
      ->id(), $this
      ->getAutosaveFormSessionID($form_state), $form_id, $entity
      ->language()
      ->getId(), $uid);
  }

  /**
   * {@inheritdoc}
   */
  public function purgeAllAutosavedStates(FormStateInterface $form_state, $uid) {
    list($form_id, $entity) = $this
      ->getFormIDandEntity($form_state);
    $this->autosaveEntityFormStorage
      ->purgeAutosavedEntityState($entity
      ->getEntityTypeId(), $entity
      ->id(), NULL, $form_id, $entity
      ->language()
      ->getId(), $uid);
  }

  /**
   * {@inheritdoc}
   */
  public function isAutosaveEnabled(FormStateInterface $form_state) {
    list($form_id, $entity) = $this
      ->getFormIDandEntity($form_state);
    $allowed = !$entity
      ->isNew() && !$this->currentUser
      ->isAnonymous();
    return $allowed;
  }

  /**
   * {@inheritdoc}
   */
  public static function getAutosaveSessionID(EntityInterface $entity) {
    if (isset($entity->{static::AUTOSAVE_SESSION_ID})) {
      return $entity->{static::AUTOSAVE_SESSION_ID};
    }
    else {
      return NULL;
    }
  }

  /**
   * Retrieves the form ID and the form entity object from the form state.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return array
   *   The first value is the form ID and the second the form entity object.
   */
  protected function getFormIDandEntity(FormStateInterface $form_state) {

    /** @var EntityFormInterface $form_object */
    $form_object = $form_state
      ->getFormObject();
    $form_id = $form_object
      ->getFormId();
    $entity = $form_object
      ->getEntity();
    return [
      $form_id,
      $entity,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AutosaveButtonClickedTrait::isAutosaveTriggered protected function Checks if the submission is triggered by autosave save.
AutosaveButtonClickedTrait::isRejectTriggered protected function Checks if autosave restore has been triggered.
AutosaveButtonClickedTrait::isRestoreTriggered protected function Checks if autosave restore has been triggered.
AutosaveEntityFormHandler::$autosaveEntityFormStorage protected property The autosave form storage.
AutosaveEntityFormHandler::$configFactory protected property The configuration factory.
AutosaveEntityFormHandler::$conflictEnabled protected property Defines whether the conflict module is enabled or not.
AutosaveEntityFormHandler::$currentUser protected property The current user.
AutosaveEntityFormHandler::$dateFormatter protected property The date formatter service.
AutosaveEntityFormHandler::$entityStorage protected property The entity storage.
AutosaveEntityFormHandler::$entityType protected property Information about the entity type.
AutosaveEntityFormHandler::$entityTypeId protected property The type of the entity for whose form the autosave form is used.
AutosaveEntityFormHandler::$keyValueExpirableFactory protected property The factory for expirable key value stores used by form cache.
AutosaveEntityFormHandler::$time protected property The time service.
AutosaveEntityFormHandler::autosaveFormAjax public function Ajax callback for autosaving content entity forms.
AutosaveEntityFormHandler::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface::createInstance
AutosaveEntityFormHandler::entityFormEntityBuild public static function Entity builder method.
AutosaveEntityFormHandler::formAlter public function Performs the needed alterations to the entity form. Overrides AutosaveFormInterface::formAlter
AutosaveEntityFormHandler::getAutosaveSessionID public static function Returns the autosave session ID of the entity. Overrides AutosaveEntityFormHandlerInterface::getAutosaveSessionID
AutosaveEntityFormHandler::getFormIDandEntity protected function Retrieves the form ID and the form entity object from the form state.
AutosaveEntityFormHandler::getLastAutosavedFormState public function Retrieves the last autosaved form state if any present. Overrides AutosaveFormInterface::getLastAutosavedFormState
AutosaveEntityFormHandler::getLastAutosavedTimestamp public function Retrieves the last autosaved timestamp. Overrides AutosaveFormInterface::getLastAutosavedTimestamp
AutosaveEntityFormHandler::isAutosaveEnabled public function Determines if autosave is enabled for the current form. Overrides AutosaveFormInterface::isAutosaveEnabled
AutosaveEntityFormHandler::isAutosaveSubmitValid public function Checks if the autosave submit is allowed. Overrides AutosaveFormInterface::isAutosaveSubmitValid
AutosaveEntityFormHandler::purgeAllAutosavedStates public function Purges all autosaved state sessions for the current form and user. Overrides AutosaveFormInterface::purgeAllAutosavedStates
AutosaveEntityFormHandler::purgeCurrentAutosavedState public function Purges the current autosave state session for the form state and user. Overrides AutosaveFormInterface::purgeCurrentAutosavedState
AutosaveEntityFormHandler::storeState public function Stores the state of the form in the autosave storage. Overrides AutosaveFormInterface::storeState
AutosaveEntityFormHandler::__construct public function Initializes an instance of the autosave form controller.
AutosaveEntityFormHandlerInterface::AUTOSAVE_SESSION_ID constant The object property to use to flag the entity with the autosave session ID.
AutosaveFormAlterTrait::autosaveFormAjax public function Ajax callback for autosaving forms. Aliased as: traitAutosaveFormAjax
AutosaveFormAlterTrait::autosaveFormRejectAjax public function Ajax callback for rejecting autosaved states.
AutosaveFormAlterTrait::autosaveFormRejectSubmit public function Form submission handler for rejecting autosaved states.
AutosaveFormAlterTrait::autosaveFormRestoreSubmit public function Form submission handler for restoring autosaved state.
AutosaveFormAlterTrait::autosaveFormSubmit public function Form submission handler for autosaving forms.
AutosaveFormAlterTrait::formAlter public function Performs the needed alterations to the form. Aliased as: traitFormAlter
AutosaveFormAlterTrait::getAutosaveFormSessionID protected static function Retrieves the autosave form session ID.
AutosaveFormAlterTrait::getRequestMethod protected function Returns the HTTP method used by the request that is building the form.
AutosaveFormAlterTrait::setAutosaveFormSessionID protected function Sets the autosave form session ID into the form state.
AutosaveFormInterface::AUTOSAVE_ELEMENT_NAME constant
AutosaveFormInterface::AUTOSAVE_REJECT_ELEMENT_NAME constant
AutosaveFormInterface::AUTOSAVE_RESTORE_ELEMENT_NAME constant
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.