Skip to content
Snippets Groups Projects
Commit 68077947 authored by jurgenhaas's avatar jurgenhaas
Browse files

Update patch for content_lock

parent a58e3a12
No related branches found
No related tags found
1 merge request!205Merging develop into main
Pipeline #1226961 passed with warnings
diff --git a/content_lock.module b/content_lock.module
index 0bae073d352baebf7cb934e1243c0ad87ab61703..e11bf199b891b1019566a06104c0529054d11103 100644
--- a/content_lock.module
+++ b/content_lock.module
@@ -95,16 +95,17 @@ function content_lock_form_alter(&$form, FormStateInterface $form_state, $form_i
if ($lock_service->isJsLock($entity_type)) {
$form['#attached']['library'][] = 'content_lock/drupal.content_lock.lock_form';
+ $args = [
+ 'entity' => $entity->id(),
+ 'langcode' => $entity->language()->getId(),
+ 'form_op' => $form_op,
+ ];
$form['#attached']['drupalSettings']['content_lock'] = [
Html::cleanCssIdentifier($form_id) => [
- 'lockUrl' => Url::fromRoute('content_lock.create_lock.' . $entity_type,
- [
- 'entity' => $entity->id(),
- 'langcode' => $entity->language()->getId(),
- 'form_op' => $form_op,
- ],
- ['query' => ['destination' => Drupal::request()->getRequestUri()]]
- )->toString(),
+ 'lockUrl' => Url::fromRoute('content_lock.create_lock.' . $entity_type, $args, [
+ 'query' => ['destination' => Drupal::request()->getRequestUri()],
+ ])->toString(),
+ 'releaseUrl' => Url::fromRoute('content_lock.release_lock.' . $entity_type, $args)->toString(),
],
];
diff --git a/js/content_lock_form.js b/js/content_lock_form.js
index 2661f311e9c689cfc7bc303edd43b96c53eec6ca..58827cb7302130a213cabd1f234501570dc99363 100644
--- a/js/content_lock_form.js
+++ b/js/content_lock_form.js
@@ -73,5 +73,16 @@
});
}
};
+
+ var onBeforeUnLoadEvent = false;
+
+ window.onunload = window.onbeforeunload= function() {
+ if (!onBeforeUnLoadEvent) {
+ onBeforeUnLoadEvent = true;
+ if (typeof navigator.sendBeacon === 'function') {
+ navigator.sendBeacon(settings.releaseUrl);
+ }
+ }
+ };
};
})(jQuery, Drupal, once);
diff --git a/src/ContentLock/ContentLock.php b/src/ContentLock/ContentLock.php
index 5d6f693fc4526e0c93f8a81a7c1063ec39e69872..dea173950eda995e41d599f03d39ca02be475ca4 100644
--- a/src/ContentLock/ContentLock.php
+++ b/src/ContentLock/ContentLock.php
@@ -344,6 +344,7 @@ class ContentLock implements ContentLockInterface {
*/
public function locking($entity_id, $langcode, $form_op, $uid, $entity_type = 'node', $quiet = FALSE, $destination = NULL) {
$translation_lock = $this->isTranslationLockEnabled($entity_type);
+ $js_lock = $this->isJsLock($entity_type);
if (!$translation_lock) {
$langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
}
@@ -371,12 +372,20 @@ class ContentLock implements ContentLockInterface {
if ($this->verbose() && !$quiet) {
if ($translation_lock) {
- $this->messenger->addStatus($this->t('This content translation is now locked against simultaneous editing. This content translation will remain locked if you navigate away from this page without saving or unlocking it.'));
+ $message = 'This content translation is now locked against simultaneous editing.';
+ if (!$js_lock) {
+ $message .= ' This content translation will remain locked if you navigate away from this page without saving or unlocking it.';
+ }
}
else {
- $this->messenger->addStatus($this->t('This content is now locked against simultaneous editing. This content will remain locked if you navigate away from this page without saving or unlocking it.'));
+ $message = 'This content is now locked against simultaneous editing.';
+ if (!$js_lock) {
+ $message .= ' This content will remain locked if you navigate away from this page without saving or unlocking it.';
+ }
}
+ $this->messenger->addStatus($this->t($message));
}
+
// Post locking hook.
$this->moduleHandler->invokeAll('content_lock_locked', [
$entity_id,
@@ -424,11 +433,18 @@ class ContentLock implements ContentLockInterface {
// Locked by current user.
if ($this->verbose() && !$quiet) {
if ($translation_lock) {
- $this->messenger->addStatus($this->t('This content translation is now locked by you against simultaneous editing. This content translation will remain locked if you navigate away from this page without saving or unlocking it.'));
+ $message = 'This content translation is now locked by you against simultaneous editing.';
+ if (!$js_lock) {
+ $message .= ' This content translation will remain locked if you navigate away from this page without saving or unlocking it.';
+ }
}
else {
- $this->messenger->addStatus($this->t('This content is now locked by you against simultaneous editing. This content will remain locked if you navigate away from this page without saving or unlocking it.'));
+ $message = 'This content is now locked by you against simultaneous editing.';
+ if (!$js_lock) {
+ $message .= ' This content will remain locked if you navigate away from this page without saving or unlocking it.';
+ }
}
+ $this->messenger->addStatus($this->t($message));
}
// Send success flag.
diff --git a/src/Controller/ContentLockController.php b/src/Controller/ContentLockController.php
index b0270fb2242492a1f0c62c8070173f9ed7e99298..5d214cac26d4fda3edbfe33366d536a484d43379 100644
--- a/src/Controller/ContentLockController.php
+++ b/src/Controller/ContentLockController.php
@@ -5,7 +5,6 @@ namespace Drupal\content_lock\Controller;
use Drupal\content_lock\Ajax\LockFormCommand;
use Drupal\content_lock\ContentLock\ContentLockInterface;
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;
@@ -78,19 +77,31 @@ class ContentLockController extends ControllerBase {
// 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;
}
+ /**
+ * Custom callback for the release lock route.
+ *
+ * @param \Symfony\Component\HttpFoundation\Request $request
+ * The current request.
+ * @param \Drupal\Core\Entity\ContentEntityInterface $entity
+ * The locked entity.
+ * @param string $langcode
+ * The langcode.
+ * @param string $form_op
+ * The form op.
+ *
+ * @see \Drupal\content_lock\Routing\ContentLockRoutes::routes()
+ */
+ public function releaseCall(Request $request, ContentEntityInterface $entity, $langcode, $form_op) {
+ $this->lockService->release($entity->id(), $entity->language()->getId(), $form_op, $this->currentUser()->id(), $entity->getEntityTypeId());
+ return [];
+ }
+
/**
* Custom access checker for the create lock requirements route.
*
diff --git a/src/Form/ContentLockSettingsForm.php b/src/Form/ContentLockSettingsForm.php
index c5baea93f91a5321599cb13ee62bab0ac755a659..4f8e372b80d7ff213c2ab2a95c52a294949f2b52 100644
--- a/src/Form/ContentLockSettingsForm.php
+++ b/src/Form/ContentLockSettingsForm.php
@@ -184,9 +184,9 @@ class ContentLockSettingsForm extends ConfigFormBase {
$form['entities'][$definition->id()]['settings']['js_lock'] = [
'#type' => 'checkbox',
- '#title' => $this->t('Lock form using JS.'),
- '#default_value' => in_array($definition->id(), $config->get('types_js_lock') ?: []),
- '#description' => $this->t('Activating this options activates the lock when the user is on the form. This helps if modules interacting with form without a user interacting with the form, like the prefetch_cache module.'),
+ '#title' => $this->t('Lock and release form using JS.'),
+ '#default_value' => in_array($definition->id(), $config->get('types_js_lock')?: []),
+ '#description' => $this->t('Activating this options activates the lock when the user is on the form. This helps if modules interacting with form without a user interacting with the form, like the prefetch_cache module. The form is automatically released when navigating away from the page.'),
];
if (!empty($definition->getHandlerClasses()['form'])) {
diff --git a/src/Routing/ContentLockRoutes.php b/src/Routing/ContentLockRoutes.php
index a0004621619ce7f11f31bd642cf1402847fb94df..289bdff22c16b99a05ad310b3ac34dae69978b08 100644
--- a/src/Routing/ContentLockRoutes.php
+++ b/src/Routing/ContentLockRoutes.php
@@ -76,6 +76,22 @@ class ContentLockRoutes implements ContainerInjectionInterface {
],
]
);
+ $routes['content_lock.release_lock.' . $definition->id()] = new Route(
+ '/admin/lock/release/' . $definition->id() . '/{entity}/{langcode}/{form_op}',
+ [
+ '_controller' => '\Drupal\content_lock\Controller\ContentLockController::releaseCall',
+ ],
+ [
+ '_custom_access' => '\Drupal\content_lock\Controller\ContentLockController::access',
+ ],
+ [
+ 'parameters' => [
+ 'entity' => [
+ 'type' => 'entity:' . $definition->id(),
+ ],
+ ],
+ ]
+ );
}
}
return $routes;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment