Skip to content
Snippets Groups Projects
Commit 420668e1 authored by Andrey Tymchuk's avatar Andrey Tymchuk Committed by A.Tymchuk
Browse files

Issue #3393497 by hartsak, WalkingDexter: Possibility to set a message after redirect

parent c6f07b38
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,7 @@ name: 'Content Translation Redirect'
type: module
description: 'Allows users to redirect from a non-existent content translation to the default content translation.'
package: Multilingual
core_version_requirement: ^9 || ^10
core_version_requirement: ^9.1 || ^10
configure: entity.content_translation_redirect.collection
dependencies:
......
......@@ -4,6 +4,6 @@ services:
arguments: ['@entity_type.manager', '@cache_tags.invalidator']
content_translation_redirect.request_subscriber:
class: Drupal\content_translation_redirect\EventSubscriber\ContentTranslationRedirectRequestSubscriber
arguments: ['@current_route_match', '@language_manager', '@entity_type.manager', '@content_translation_redirect.manager']
arguments: ['@current_route_match', '@language_manager', '@entity_type.manager', '@content_translation_redirect.manager', '@event_dispatcher']
tags:
- { name: event_subscriber }
<?php
namespace Drupal\content_translation_redirect;
/**
* Defines events for Content Translation Redirect.
*/
final class ContentTranslationRedirectEvents {
/**
* The name of the event fired when performing the redirect.
*
* @Event
*
* @see \Drupal\content_translation_redirect\Event\ContentTranslationRedirectEvent
* @see \Drupal\content_translation_redirect\EventSubscriber\ContentTranslationRedirectRequestSubscriber::onRequest()
*/
public const REDIRECT = 'content_translation_redirect';
}
<?php
namespace Drupal\content_translation_redirect\Event;
use Drupal\Component\EventDispatcher\Event;
use Drupal\content_translation_redirect\ContentTranslationRedirectInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Routing\LocalRedirectResponse;
use Drupal\Core\Url;
/**
* Defines the content translation redirect event.
*/
class ContentTranslationRedirectEvent extends Event {
/**
* The redirect response.
*
* @var \Drupal\Core\Routing\LocalRedirectResponse
*/
protected $response;
/**
* The entity for which the redirect is being performed.
*
* @var \Drupal\Core\Entity\ContentEntityInterface
*/
protected $entity;
/**
* The current content language.
*
* @var \Drupal\Core\Language\LanguageInterface
*/
protected $language;
/**
* The redirect entity.
*
* @var \Drupal\content_translation_redirect\ContentTranslationRedirectInterface
*/
protected $redirect;
/**
* The redirect Url object.
*
* @var \Drupal\Core\Url
*/
protected $url;
/**
* ContentTranslationRedirectEvent constructor.
*
* @param \Drupal\Core\Routing\LocalRedirectResponse $response
* The redirect response.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity for which the redirect is being performed.
* @param \Drupal\Core\Language\LanguageInterface $language
* The current content language.
* @param \Drupal\content_translation_redirect\ContentTranslationRedirectInterface $redirect
* The redirect entity.
* @param \Drupal\Core\Url $url
* The redirect Url object.
*/
public function __construct(LocalRedirectResponse $response, ContentEntityInterface $entity, LanguageInterface $language, ContentTranslationRedirectInterface $redirect, Url $url) {
$this->response = $response;
$this->entity = $entity;
$this->language = $language;
$this->redirect = $redirect;
$this->url = $url;
}
/**
* Gets the redirect response.
*
* @return \Drupal\Core\Routing\LocalRedirectResponse
* The redirect response.
*/
public function getResponse(): LocalRedirectResponse {
return $this->response;
}
/**
* Gets the entity for which the redirect is being performed.
*
* @return \Drupal\Core\Entity\ContentEntityInterface
* The entity object.
*/
public function getEntity(): ContentEntityInterface {
return $this->entity;
}
/**
* Gets the current content language.
*
* @return \Drupal\Core\Language\LanguageInterface
* The current content language.
*/
public function getLanguage(): LanguageInterface {
return $this->language;
}
/**
* Gets the redirect entity.
*
* @return \Drupal\content_translation_redirect\ContentTranslationRedirectInterface
* The redirect entity object.
*/
public function getRedirect(): ContentTranslationRedirectInterface {
return $this->redirect;
}
/**
* Gets the redirect Url object.
*
* @return \Drupal\Core\Url
* The redirect Url object.
*/
public function getUrl(): Url {
return $this->url;
}
}
......@@ -2,7 +2,9 @@
namespace Drupal\content_translation_redirect\EventSubscriber;
use Drupal\content_translation_redirect\ContentTranslationRedirectEvents;
use Drupal\content_translation_redirect\ContentTranslationRedirectManagerInterface;
use Drupal\content_translation_redirect\Event\ContentTranslationRedirectEvent;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\ContentEntityInterface;
......@@ -15,6 +17,7 @@ use Drupal\Core\Url;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
/**
* Redirect subscriber for controller requests.
......@@ -49,6 +52,13 @@ class ContentTranslationRedirectRequestSubscriber implements EventSubscriberInte
*/
protected $manager;
/**
* The event dispatcher.
*
* @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* ContentTranslationRedirectRequestSubscriber constructor.
*
......@@ -60,12 +70,15 @@ class ContentTranslationRedirectRequestSubscriber implements EventSubscriberInte
* The entity type manager.
* @param \Drupal\content_translation_redirect\ContentTranslationRedirectManagerInterface $manager
* The content translation redirect manager.
* @param \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher.
*/
public function __construct(RouteMatchInterface $route_match, LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_type_manager, ContentTranslationRedirectManagerInterface $manager) {
public function __construct(RouteMatchInterface $route_match, LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_type_manager, ContentTranslationRedirectManagerInterface $manager, EventDispatcherInterface $event_dispatcher) {
$this->routeMatch = $route_match;
$this->languageManager = $language_manager;
$this->storage = $entity_type_manager->getStorage('content_translation_redirect');
$this->manager = $manager;
$this->eventDispatcher = $event_dispatcher;
}
/**
......@@ -132,6 +145,10 @@ class ContentTranslationRedirectRequestSubscriber implements EventSubscriberInte
$response->addCacheableDependency($cacheable_metadata);
$response->addCacheableDependency($redirect);
$response->addCacheableDependency($entity);
$redirect_event = new ContentTranslationRedirectEvent($response, $entity, $language, $redirect, $url);
$this->eventDispatcher->dispatch($redirect_event, ContentTranslationRedirectEvents::REDIRECT);
$event->setResponse($response);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment