You are here

public function Redirect::execute in Entity Legal 4.0.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/EntityLegal/Redirect.php \Drupal\entity_legal\Plugin\EntityLegal\Redirect::execute()
  2. 3.0.x src/Plugin/EntityLegal/Redirect.php \Drupal\entity_legal\Plugin\EntityLegal\Redirect::execute()

Execute callback for Entity Legal method plugin.

Parameters

array $context: Contextual information for plugin to execute on.

Overrides EntityLegalPluginInterface::execute

File

src/Plugin/EntityLegal/Redirect.php, line 95

Class

Redirect
Method class for redirecting existing users to accept a legal document.

Namespace

Drupal\entity_legal\Plugin\EntityLegal

Code

public function execute(array &$context = []) {

  /** @var \Drupal\entity_legal\EntityLegalDocumentInterface $document */
  foreach ($this->documents as $document) {

    /** @var \Symfony\Component\HttpKernel\Event\GetResponseEvent $event */
    $event = $context['event'];
    $request = $event
      ->getRequest();

    // The acceptance of a legal document is applicable only to humans.
    if ($request
      ->getRequestFormat() !== 'html') {
      return FALSE;
    }

    // Don't redirect on POST requests.
    if (!$request
      ->isMethodSafe()) {
      return FALSE;
    }
    if (!($route_name = $this->routeMatch
      ->getRouteName())) {

      // Unrouted?
      return FALSE;
    }
    if ($this
      ->isExcludedRoute($route_name, $document)) {
      return FALSE;
    }

    // Do not redirect password reset.
    if ($this
      ->isPasswordReset($event
      ->getRequest())) {
      return FALSE;
    }
    if ($messages = $this
      ->messenger()
      ->all()) {

      // Save any messages set for the destination page.
      // @see \Drupal\entity_legal\Form\EntityLegalDocumentAcceptanceForm::submitForm()
      $this->tempStore
        ->set('postponed_messages', $messages);
      $this
        ->messenger()
        ->deleteAll();
    }
    $this
      ->messenger()
      ->addWarning($this
      ->t('You must accept this agreement before continuing.'));
    $entity_url = $document
      ->toUrl()
      ->setOption('query', $this
      ->getDestinationArray())
      ->setAbsolute(TRUE)
      ->toString();
    $event
      ->setResponse(new TrustedRedirectResponse($entity_url));

    // Remove destination cause the RedirectResponseSubscriber redirects and
    // in some cases it brings redirect loops.
    $request->query
      ->remove('destination');
    $request->request
      ->remove('destination');
  }
}