You are here

public function ContentLockController::createLockCall in Content locking (anti-concurrent editing) 8

Same name and namespace in other branches
  1. 8.2 src/Controller/ContentLockController.php \Drupal\content_lock\Controller\ContentLockController::createLockCall()

Custom callback for the create lock route.

Parameters

\Symfony\Component\HttpFoundation\Request $request:

\Drupal\Core\Entity\ContentEntityInterface $entity:

Return value

\Symfony\Component\HttpFoundation\JsonResponse

See also

\Drupal\content_lock\Routing\ContentLockRoutes::routes()

File

src/Controller/ContentLockController.php, line 59

Class

ContentLockController
Class ContentLockController.

Namespace

Drupal\content_lock\Controller

Code

public function createLockCall(Request $request, ContentEntityInterface $entity, $langcode, $form_op) {
  $response = new AjaxResponse();

  // Not lockable entity or entity creation.
  if (!$this->lockService
    ->isLockable($entity, $form_op) || is_null($entity
    ->id())) {
    $lockable = FALSE;
    $lock = FALSE;
  }
  else {
    $lockable = TRUE;
    $destination = $request->query
      ->get('destination') ?: $entity
      ->toUrl('edit-form')
      ->toString();
    $lock = $lockable ? $this->lockService
      ->locking($entity
      ->id(), $langcode, $form_op, $this
      ->currentUser()
      ->id(), $entity
      ->getEntityTypeId(), FALSE, $destination) : FALSE;

    // Render status messages from locking service.
    $response
      ->addCommand(new PrependCommand('', [
      '#type' => 'status_messages',
    ]));
    if ($lock) {
      $language = $this
        ->languageManager()
        ->getLanguage($langcode);
      $url = $entity
        ->toUrl('canonical', [
        'language' => $language,
      ]);
      $unlock_button = $this->lockService
        ->unlockButton($entity
        ->getEntityTypeId(), $entity
        ->id(), $langcode, $form_op, $url
        ->toString());
      $response
        ->addCommand(new AppendCommand('.content-lock-actions.form-actions', $unlock_button));
    }
  }
  $response
    ->addCommand(new LockFormCommand($lockable, $lock));
  return $response;
}