You are here

class FieldRedirectionResult in Field Redirection 8.2

Defines a value object for a field redirection result.

Hierarchy

Expanded class hierarchy of FieldRedirectionResult

See also

\Drupal\field_redirection\FieldRedirectionResultBuilder

2 files declare their use of FieldRedirectionResult
FieldRedirectionResultBuilderLinkTest.php in tests/src/Kernel/FieldRedirectionResultBuilderLinkTest.php
FieldRedirectionResultUnitTest.php in tests/src/Unit/FieldRedirectionResultUnitTest.php

File

src/FieldRedirectionResult.php, line 13

Namespace

Drupal\field_redirection
View source
class FieldRedirectionResult {

  /**
   * TRUE if redirect should occur.
   *
   * @var bool
   */
  protected $shouldRedirect = TRUE;

  /**
   * URL to redirect to.
   *
   * @var \Drupal\Core\Url
   */
  protected $redirectUrl;

  /**
   * Constructs a new FieldRedirectionResult.
   *
   * Note this function is protected by design, use one of the static methods
   * such as ::fromUrl and ::deny.
   */
  protected function __construct() {
  }

  /**
   * Returns TRUE if redirect should occur.
   *
   * @return bool
   *   TRUE if redirect should occur.
   */
  public function shouldRedirect() {
    return $this->shouldRedirect;
  }

  /**
   * Gets the result as a redirect response.
   *
   * @param int $status_code
   *   Status code.
   * @param array $headers
   *   Additional headers.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   Redirect response.
   *
   * @throws \LogicException
   *   When the result should not redirect.
   */
  public function asRedirectResponse($status_code = 302, array $headers = []) {
    return new RedirectResponse($this
      ->getRedirectUrl()
      ->toString(), $status_code, $headers);
  }

  /**
   * Gets redirect URL.
   *
   * @return \Drupal\Core\Url
   *   The url object.
   */
  protected function getRedirectUrl() {
    if (!$this
      ->shouldRedirect()) {
      throw new \LogicException("There is no redirect URL for a redirect result that doesn't redirect");
    }
    return $this->redirectUrl;
  }

  /**
   * Factory method for a field redirection result that should not redirect.
   *
   * @return \Drupal\field_redirection\FieldRedirectionResult
   *   New instance.
   */
  public static function deny() {
    $instance = new static();
    $instance->shouldRedirect = FALSE;
    return $instance;
  }

  /**
   * Factory method to create from a URL object.
   *
   * @param \Drupal\Core\Url $url
   *   The URL object.
   *
   * @return \Drupal\field_redirection\FieldRedirectionResult
   *   New instance.
   */
  public static function fromUrl(Url $url) {
    $instance = new static();
    $instance->redirectUrl = $url;
    return $instance;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldRedirectionResult::$redirectUrl protected property URL to redirect to.
FieldRedirectionResult::$shouldRedirect protected property TRUE if redirect should occur.
FieldRedirectionResult::asRedirectResponse public function Gets the result as a redirect response.
FieldRedirectionResult::deny public static function Factory method for a field redirection result that should not redirect.
FieldRedirectionResult::fromUrl public static function Factory method to create from a URL object.
FieldRedirectionResult::getRedirectUrl protected function Gets redirect URL.
FieldRedirectionResult::shouldRedirect public function Returns TRUE if redirect should occur.
FieldRedirectionResult::__construct protected function Constructs a new FieldRedirectionResult.