function content_lock_timeout_entity_prepare_form in Content locking (anti-concurrent editing) 8
Same name and namespace in other branches
- 8.2 modules/content_lock_timeout/content_lock_timeout.module \content_lock_timeout_entity_prepare_form()
Implements hook_entity_prepare_form().
File
- modules/
content_lock_timeout/ content_lock_timeout.module, line 53 - Allowed time-based automatic unlocking of nodes.
Code
function content_lock_timeout_entity_prepare_form(EntityInterface $entity, $operation, FormStateInterface $form_state) {
// We support entity type Node, Term and Block Content.
if ($entity instanceof NodeInterface || $entity instanceof TermInterface || $entity instanceof BlockContentInterface) {
$user = \Drupal::currentUser();
$config = \Drupal::config('content_lock_timeout.settings');
if (!$config
->get('content_lock_timeout_on_edit')) {
return;
}
$timeout_minutes = $config
->get('content_lock_timeout_minutes');
$last_valid_time = Drupal::time()
->getCurrentTime() - 60 * $timeout_minutes;
/** @var \Drupal\content_lock\ContentLock\ContentLock $lock_service */
$lock_service = \Drupal::service('content_lock');
// This is a new, unsaved entity (which thus can't be locked).
// This is a stale lock.
// There already is a lock on this entity.
// A different user owns the lock.
// There is already a lock on this entity.
if (!empty($entity
->id()) && is_object($lock = $lock_service
->fetchLock($entity
->id(), $entity
->language()
->getId(), $operation, $entity
->getEntityTypeId())) && $lock->uid != $user
->id() && $lock->timestamp < $last_valid_time && $user
->hasPermission('break content lock') && $user
->id() > 0) {
$lock_service
->release($entity
->id(), $entity
->language()
->getId(), $operation, $lock->uid, $entity
->getEntityTypeId());
if ($lock_service
->verbose()) {
$username = User::load($lock->uid)
->getDisplayName();
$date = \Drupal::service('date.formatter')
->formatInterval(REQUEST_TIME - $lock->timestamp);
$stale_time = \Drupal::service('date.formatter')
->formatInterval($last_valid_time - $lock->timestamp);
\Drupal::messenger()
->addStatus(t('Breaking existing lock by @name so that you may edit this node. (This lock was set @date ago and was stale since @stale_time.)', [
'@name' => $username,
'@date' => $date,
'@stale_time' => $stale_time,
]));
}
}
}
}