class FieldRedirectionResult in Field Redirection 8.2
Defines a value object for a field redirection result.
Hierarchy
- class \Drupal\field_redirection\FieldRedirectionResult
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_redirectionView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FieldRedirectionResult:: |
protected | property | URL to redirect to. | |
FieldRedirectionResult:: |
protected | property | TRUE if redirect should occur. | |
FieldRedirectionResult:: |
public | function | Gets the result as a redirect response. | |
FieldRedirectionResult:: |
public static | function | Factory method for a field redirection result that should not redirect. | |
FieldRedirectionResult:: |
public static | function | Factory method to create from a URL object. | |
FieldRedirectionResult:: |
protected | function | Gets redirect URL. | |
FieldRedirectionResult:: |
public | function | Returns TRUE if redirect should occur. | |
FieldRedirectionResult:: |
protected | function | Constructs a new FieldRedirectionResult. |