ContentLockController.php in Content locking (anti-concurrent editing) 8.2
File
src/Controller/ContentLockController.php
View source
<?php
namespace Drupal\content_lock\Controller;
use Drupal\content_lock\Ajax\LockFormCommand;
use Drupal\content_lock\ContentLock\ContentLock;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AppendCommand;
use Drupal\Core\Ajax\PrependCommand;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class ContentLockController extends ControllerBase {
protected $lockService;
public function __construct(ContentLock $lock_service) {
$this->lockService = $lock_service;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('content_lock'));
}
public function createLockCall(Request $request, ContentEntityInterface $entity, $langcode, $form_op) {
$response = new AjaxResponse();
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;
$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;
}
public function access(ContentEntityInterface $entity, AccountInterface $account) {
return $entity
->access('update', $account, TRUE);
}
}