You are here

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'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses RedirectSource
redirect_source.pre_entity_save in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\EventSubscriber\PreEntitySave\RedirectSource

File

src/EventSubscriber/PreEntitySave/RedirectSource.php, line 15

Namespace

Drupal\acquia_contenthub\EventSubscriber\PreEntitySave
View 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

Namesort descending Modifiers Type Description Overrides
RedirectSource::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
RedirectSource::onPreEntitySave public function Adds the query parameter.
RedirectSource::setQuery protected function Set query parameter for redirect.