protected function FieldRedirectionResultBuilder::shouldDeny in Field Redirection 8.2
Determine whether we should deny redirecting.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field items.
\Symfony\Component\HttpFoundation\Request $request: The request.
\Drupal\Core\Session\AccountInterface $account: The account.
array $settings: The field settings.
Return value
bool TRUE if we should deny redirecting.
1 call to FieldRedirectionResultBuilder::shouldDeny()
- FieldRedirectionResultBuilder::buildResult in src/
FieldRedirectionResultBuilder.php - Builds a redirection result for a given set of values.
File
- src/
FieldRedirectionResultBuilder.php, line 84
Class
- FieldRedirectionResultBuilder
- Defines a service for evaluating the intended action for a field redirection.
Namespace
Drupal\field_redirectionCode
protected function shouldDeny(FieldItemListInterface $items, Request $request, AccountInterface $account, array $settings = []) {
// Return early if account has bypass permission.
if ($account
->hasPermission('bypass redirection')) {
return TRUE;
}
$current_url = Url::fromRoute('<current>');
$current_path = $current_url
->toString();
// Optionally control the list of pages this works on.
if (!empty($settings['page_restrictions']) && !empty($settings['pages'])) {
// Remove '1' from this value so it can be XOR'd later on.
$page_restrictions = $settings['page_restrictions'] - 1;
// Do raw token replacements.
$pages = $this->token
->replace($settings['pages'], [], [
'clear' => TRUE,
]);
// Normalise all paths to lower case.
$pages = mb_strtolower($pages);
$page_match = $this->pathMatcher
->matchPath($current_path, $pages);
$requestUri = $request
->getRequestUri();
if ($current_path != $requestUri) {
$page_match = $page_match || $this->pathMatcher
->matchPath($requestUri, $pages);
}
// Stop processing if the page restrictions have matched.
if (!($page_restrictions xor $page_match)) {
return TRUE;
}
}
// Don't do anything if the current page is running the normal cron script;
// this also supports Elysia Cron.
if (mb_strpos($current_path, '/cron') === 0) {
return TRUE;
}
// Don't do anything if the cron script is being executed from the admin
// status page.
if ($current_path === '/admin/reports/status/run-cron') {
return TRUE;
}
// Don't do anything if site is in maintenance mode.
if (defined('MAINTENANCE_MODE') || $this->state
->get('system.maintenance_mode')) {
return TRUE;
}
// Only redirect based on the first value of a field. Ignore other values.
if (!$items
->isEmpty()) {
/** @var \Drupal\link\Plugin\Field\FieldType\LinkItem $item */
$item = $items
->first();
}
elseif (isset($settings['404_if_empty']) && $settings['404_if_empty']) {
throw new NotFoundHttpException();
}
else {
return TRUE;
}
$redirect_url = $item
->getUrl();
// We need to check if the redirect URL is the same as:
//
// 1. The current (possibly an alias) path (relative).
// 2. The current path's internal path (relative). Url->toString()
// always returns an alias, so this is covered by point 1 above.
// 3. The current path's internal path (absolute).
// 4. The current path, which is also the home page.
//
// If any of these cases are true, then do not redirect.
//
// Current path (relative) and current internal path (relative).
if ($current_path == $redirect_url
->toString()) {
return TRUE;
}
// Current path (absolute).
$current_url
->setAbsolute(TRUE);
if ($current_url
->toString() === $redirect_url
->toString()) {
return TRUE;
}
// Current path is the home page.
if (!$redirect_url
->isExternal() && $redirect_url
->getRouteName() == '<front>' && $this->pathMatcher
->isFrontPage()) {
return TRUE;
}
return FALSE;
}