class RedirectSource in Acquia Content Hub 8.2
Fixes the redirect source field to handle query parameters.
@package Drupal\acquia_contenthub\EventSubscriber\PreEntitySave
Hierarchy
- class \Drupal\acquia_contenthub\EventSubscriber\PreEntitySave\RedirectSource implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of RedirectSource
1 string reference to 'RedirectSource'
1 service uses RedirectSource
File
- src/
EventSubscriber/ PreEntitySave/ RedirectSource.php, line 15
Namespace
Drupal\acquia_contenthub\EventSubscriber\PreEntitySaveView source
class RedirectSource implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[AcquiaContentHubEvents::PRE_ENTITY_SAVE][] = [
'onPreEntitySave',
80,
];
return $events;
}
/**
* Adds the query parameter.
*
* @param \Drupal\acquia_contenthub\Event\PreEntitySaveEvent $event
* The pre entity save event.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function onPreEntitySave(PreEntitySaveEvent $event) {
$entity = $event
->getEntity();
// If it is not a redirect then exit.
if ($entity
->getEntityTypeId() !== 'redirect') {
return;
}
$data = json_decode(base64_decode($event
->getCdf()
->getMetadata()['data']), TRUE);
$redirect_source = $data['redirect_source'];
$this
->setQuery($redirect_source, $entity);
}
/**
* Set query parameter for redirect.
*
* @param array $redirect_source
* The redirect source.
* @param \Drupal\redirect\Entity\Redirect $redirect
* The redirect entity.
*/
protected function setQuery(array $redirect_source, Redirect $redirect) {
$langcode = $redirect
->getEntityType()
->getKey('langcode');
$language = $redirect
->get($langcode)->value;
$query = $redirect_source['value'][$language]['query'] ?? [];
$path = $redirect
->getSource()['path'];
if (!empty($query)) {
$redirect
->setSource($path, $query);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RedirectSource:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
RedirectSource:: |
public | function | Adds the query parameter. | |
RedirectSource:: |
protected | function | Set query parameter for redirect. |