You are here

function autosave_form_entity_update in Autosave Form 8

Implements hook_entity_update().

File

./autosave_form.module, line 100
This module holds autosave form functionality.

Code

function autosave_form_entity_update(EntityInterface $entity) {
  static $conflict_enabled;
  static $autosave_entity_form_storage;
  if (!isset($autosave_entity_form_storage)) {

    /** @var \Drupal\autosave_form\Storage\AutosaveEntityFormStorageInterface $autosave_entity_form_storage */
    $autosave_entity_form_storage = \Drupal::service('autosave_form.entity_form_storage');
  }
  if ($conflict_enabled === FALSE || is_null($conflict_enabled) && !($conflict_enabled = \Drupal::moduleHandler()
    ->moduleExists('conflict'))) {

    // If conflict management is not available the autosaved entity states have
    // to be removed when the entity is saved.
    $autosave_entity_form_storage
      ->purgeAutosavedEntityState($entity
      ->getEntityTypeId(), $entity
      ->id());
  }
  else {
    $entity_type = $entity
      ->getEntityType();
    if ($entity_type
      ->hasHandlerClass('autosave_form') && ($class = $entity_type
      ->getHandlerClass('autosave_form'))) {

      // If conflict is enabled and the entity is saved then delete only the
      // current autosave session of the current user.
      if ($autosave_session_id = $class::getAutosaveSessionID($entity)) {
        $autosave_entity_form_storage
          ->purgeAutosavedEntityState($entity
          ->getEntityTypeId(), $entity
          ->id(), $autosave_session_id);
      }
    }
  }
}