You are here

public function PageRedirect::performAction in Rabbit Hole 8

Same name and namespace in other branches
  1. 2.x src/Plugin/RabbitHoleBehaviorPlugin/PageRedirect.php \Drupal\rabbit_hole\Plugin\RabbitHoleBehaviorPlugin\PageRedirect::performAction()

Perform the rabbit hole action.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity the action is being performed on.

Overrides RabbitHoleBehaviorPluginBase::performAction

File

src/Plugin/RabbitHoleBehaviorPlugin/PageRedirect.php, line 124

Class

PageRedirect
Redirects to another page.

Namespace

Drupal\rabbit_hole\Plugin\RabbitHoleBehaviorPlugin

Code

public function performAction(EntityInterface $entity, Response $current_response = NULL) {
  $target = $this
    ->getActionTarget($entity);
  $response_code = $this
    ->getActionResponseCode($entity);

  // The fallback action is executed if redirect target is either empty or
  // has invalid value.
  if (empty($target)) {
    return $this
      ->getFallbackAction($entity);
  }
  switch ($response_code) {
    case self::REDIRECT_MOVED_PERMANENTLY:
    case self::REDIRECT_FOUND:
    case self::REDIRECT_SEE_OTHER:
    case self::REDIRECT_TEMPORARY_REDIRECT:
      if ($current_response === NULL) {
        $redirect_response = new TrustedRedirectResponse($target, $response_code);
        $redirect_response
          ->addCacheableDependency($this->cacheMetadata);
        return $redirect_response;
      }
      else {

        // If a response already exists we don't need to do anything with it.
        return $current_response;
      }

    // TODO: I don't think this is the correct way to handle a 304 response.
    case self::REDIRECT_NOT_MODIFIED:
      if ($current_response === NULL) {
        $not_modified_response = new Response();
        $not_modified_response
          ->setStatusCode(self::REDIRECT_NOT_MODIFIED);
        $not_modified_response->headers
          ->set('Location', $target);
        return $not_modified_response;
      }
      else {

        // If a response already exists we don't need to do anything with it.
        return $current_response;
      }

    // TODO: I have no idea if this is actually the correct way to handle a
    // 305 response in Symfony/D8. Documentation on it seems a bit sparse.
    case self::REDIRECT_USE_PROXY:
      if ($current_response === NULL) {
        $use_proxy_response = new Response();
        $use_proxy_response
          ->setStatusCode(self::REDIRECT_USE_PROXY);
        $use_proxy_response->headers
          ->set('Location', $target);
        return $use_proxy_response;
      }
      else {

        // If a response already exists we don't need to do anything with it.
        return $current_response;
      }
    default:
      throw new InvalidRedirectResponseException();
  }
}