Skip to content

Instantly share code, notes, and snippets.

@chx
Created September 17, 2022 20:14
Show Gist options
  • Save chx/202a356fce758bdb32ce0e7e7388fbd8 to your computer and use it in GitHub Desktop.
Save chx/202a356fce758bdb32ce0e7e7388fbd8 to your computer and use it in GitHub Desktop.
Pause redirect module
name: Pause redirect
type: module
description: Pause redirects
package: redirect
core: 8.x
core_version_requirement: ^8 || ^9
dependencies:
- redirect
pause_redirect.settings:
path: '/admin/config/search/redirect/pause'
defaults:
_form: 'Drupal\pause_redirect\Form\PauseRedirectSettingsForm'
_title: 'Pause Redirect settings'
requirements:
_permission: 'administer redirects'
services:
pause_redirect.event_subscriber:
class: Drupal\pause_redirect\EventSubscriber\PauseRedirectRequestSubscriber
decorates: redirect.request_subscriber
arguments: ['@pause_redirect.event_subscriber.inner', '@messenger']
<?php
namespace Drupal\pause_redirect\EventSubscriber;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\pause_redirect\Form\PauseRedirectSettingsForm;
use Drupal\redirect\EventSubscriber\RedirectRequestSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Pause redirect event subscriber.
*/
class PauseRedirectRequestSubscriber implements EventSubscriberInterface {
/**
* Constructs event subscriber.
*
* @param \Drupal\redirect\EventSubscriber\RedirectRequestSubscriber $original
* The original redirect request subscriber.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger.
*/
public function __construct(protected RedirectRequestSubscriber $original, protected MessengerInterface $messenger) {
}
public function onKernelRequestCheckRedirect(RequestEvent $original_event): void {
$cloned_event = clone $original_event;
$this->original->onKernelRequestCheckRedirect($cloned_event);
if ($cloned_event->hasResponse()) {
$response = $cloned_event->getResponse();
if ($response instanceof RedirectResponse && PauseRedirectSettingsForm::isPaused($original_event->getRequest())) {
$this->messenger->addWarning(t('This page redirects to <a href="@link">@link</a>.', ['@link' => $response->getTargetUrl()]));
}
else {
$original_event->setResponse($response);
}
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
$events[KernelEvents::REQUEST][] = ['onKernelRequestCheckRedirect', 33];
return $events;
}
}
<?php
namespace Drupal\pause_redirect\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Configure Redirect settings for this site.
*/
class PauseRedirectSettingsForm extends FormBase {
const PAUSE_REDIRECT = 'pause_redirect';
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'pause_redirect_settings';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form[self::PAUSE_REDIRECT] = [
'#type' => 'checkbox',
'#title' => $this->t('Pause redirects'),
'#default_value' => static::isPaused($this->getRequest()),
];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
'#button_type' => 'primary',
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->messenger()->addStatus($this->t('Saved.'));
$this->getRequest()->getSession()->set(self::PAUSE_REDIRECT, (bool) $form_state->getValue(self::PAUSE_REDIRECT));
}
/**
* @param \Symfony\Component\HttpFoundation\Request $request
* The request holding the session.
*
* @return bool
* Whether redirects are paused.
*/
public static function isPaused(Request $request): bool {
return $request->getSession()->get(self::PAUSE_REDIRECT, FALSE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment