You are here

public function LanguageCookieConditionHardcodedBlacklistedPaths::evaluate in Language Cookie 8

Evaluates the condition and returns TRUE or FALSE accordingly.

Return value

bool TRUE if the condition has been met, FALSE otherwise.

Overrides LanguageCookieConditionBase::evaluate

File

src/Plugin/LanguageCookieCondition/LanguageCookieConditionHardcodedBlacklistedPaths.php, line 108

Class

LanguageCookieConditionHardcodedBlacklistedPaths
Class for the Hardcoded blacklisted paths condition plugin.

Namespace

Drupal\language_cookie\Plugin\LanguageCookieCondition

Code

public function evaluate() {
  $hardcoded_blacklist = [
    '/cdn/farfuture',
    '/httprl_async_function_callback',
    '/' . PublicStream::basePath() . '*',
  ];

  // Do not set a cookie on the Language Selection Page.
  // See https://www.drupal.org/project/language_selection_page.
  $language_selection_page_config = $this->configFactory
    ->get('language_selection_page.negotiation');
  if ($language_selection_page_path = $language_selection_page_config
    ->get('path')) {
    $hardcoded_blacklist[] = '/' . $language_selection_page_path;
  }
  foreach ($hardcoded_blacklist as $blacklisted_path) {
    $request = $this->requestStack
      ->getCurrentRequest();

    // Compare the lowercase path alias (if any) and internal path.
    $path = $this->currentPath
      ->getPath($request);

    // Do not trim a trailing slash if that is the complete path.
    $path = $path === '/' ? $path : rtrim($path, '/');
    $path_alias = mb_strtolower($this->aliasManager
      ->getAliasByPath($path));
    $is_on_blacklisted_path = $this->pathMatcher
      ->matchPath($path_alias, $blacklisted_path) || $path != $path_alias && $this->pathMatcher
      ->matchPath($path, $blacklisted_path);
    if ($is_on_blacklisted_path) {
      return $this
        ->block();
    }
  }
  return $this
    ->pass();
}