Skip to content
Snippets Groups Projects
Commit 69986883 authored by Andrei Mateescu's avatar Andrei Mateescu Committed by Italo Mairo
Browse files

Issue #3301512 by amateescu, itamair, kiseleva.t, s_leu, alecsmrekar, plach:...

Issue #3301512 by amateescu, itamair, kiseleva.t, s_leu, alecsmrekar, plach: Extend the ability to skip geocoding when processing a large number of entity updates, like migrations or workspace publishing
parent cbe57a0f
No related branches found
No related tags found
1 merge request!60Add a way to skip geocoding using runtime parameters.
Pipeline #363764 passed
...@@ -163,9 +163,14 @@ function geocoder_field_entity_presave(EntityInterface $entity) { ...@@ -163,9 +163,14 @@ function geocoder_field_entity_presave(EntityInterface $entity) {
return; return;
} }
// Skip any action if requested.
$geocoder_config = \Drupal::configFactory()->get('geocoder.settings'); $geocoder_config = \Drupal::configFactory()->get('geocoder.settings');
// Check geocoder_presave_disabled setting and do nothing if enabled. $config_disabled = $geocoder_config->get('geocoder_presave_disabled');
if ($geocoder_config->get('geocoder_presave_disabled')) { // Check if geocoder presave is disabled at runtime by a request attribute.
// i.e. via WorkspacePublishingSubscriber
// (@see https://www.drupal.org/i/3301512)
$runtime_disabled = \Drupal::request()->attributes->get('geocoder_presave_disabled', FALSE);
if ($config_disabled || $runtime_disabled) {
return; return;
} }
......
...@@ -9,3 +9,9 @@ services: ...@@ -9,3 +9,9 @@ services:
class: Drupal\geocoder_field\PreprocessorPluginManager class: Drupal\geocoder_field\PreprocessorPluginManager
parent: default_plugin_manager parent: default_plugin_manager
arguments: ["@country_manager"] arguments: ["@country_manager"]
geocoder_field.workspace_publishing_subscriber:
class: Drupal\geocoder_field\EventSubscriber\WorkspacePublishingSubscriber
arguments: ['@request_stack']
tags:
- { name: event_subscriber }
<?php
declare(strict_types=1);
namespace Drupal\geocoder_field\EventSubscriber;
use Drupal\workspaces\Event\WorkspacePostPublishEvent;
use Drupal\workspaces\Event\WorkspacePrePublishEvent;
use Drupal\workspaces\Event\WorkspacePublishEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Event subscriber to respond to workspace publishing events.
*
* The geocoding operations from geocoder_field_entity_presave() can be very
* expensive when updating many entities at once. Workspace publishing doesn't
* change any field data, it only re-saves the latest workspace-specific
* revision and sets it as the default one, so there is no need to update
* geocoding data.
*
* @see geocoder_field_entity_presave()
*/
class WorkspacePublishingSubscriber implements EventSubscriberInterface {
public function __construct(
protected RequestStack $requestStack,
) {}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
if (!class_exists(WorkspacePublishEvent::class)) {
return [];
}
return [
WorkspacePrePublishEvent::class => ['onPrePublish'],
WorkspacePostPublishEvent::class => ['onPostPublish'],
];
}
/**
* Adds a custom request attribute to prevent geocoding updates.
*/
public function onPrePublish(): void {
if ($request = $this->requestStack->getCurrentRequest()) {
$request->attributes->set('geocoder_presave_disabled', TRUE);
}
}
/**
* Removes the custom request attribute.
*/
public function onPostPublish(): void {
if ($request = $this->requestStack->getCurrentRequest()) {
$request->attributes->remove('geocoder_presave_disabled');
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment