You are here

function content_lock_form_submit in Content locking (anti-concurrent editing) 8

Same name and namespace in other branches
  1. 8.2 content_lock.module \content_lock_form_submit()

Submit handler for content_lock.

1 string reference to 'content_lock_form_submit'
content_lock_form_alter in ./content_lock.module
Implements hook_form_FORM_ID_alter().

File

./content_lock.module, line 146
Content lock - Main functions of the module.

Code

function content_lock_form_submit($form, FormStateInterface $form_state) {

  // Signals editing is finished; remove the lock.
  $user = \Drupal::currentUser();

  /** @var \Drupal\content_lock\ContentLock\ContentLock $lock_service */
  $lock_service = \Drupal::service('content_lock');

  /** @var \Drupal\core\Entity\ContentEntityInterface $entity */
  $entity = $form_state
    ->getFormObject()
    ->getEntity();

  // If the user submitting owns the lock, release it.
  $lock_service
    ->release($entity
    ->id(), $entity
    ->language()
    ->getId(), $form_state
    ->getFormObject()
    ->getOperation(), $user
    ->id(), $entity
    ->getEntityTypeId());

  // We need to redirect to the canonical page after saving it. If not, we
  // stay on the edit form and we re-lock the entity.
  if (!$form_state
    ->getRedirect() || $form_state
    ->getRedirect() && $entity
    ->hasLinkTemplate('edit-form') && $entity
    ->toUrl('edit-form')
    ->toString() == $form_state
    ->getRedirect()
    ->toString()) {
    $form_state
      ->setRedirectUrl($entity
      ->toUrl());
  }
}