You are here

public function PathProcessorSearchApiPage::processOutbound in Search API Pages 8

Processes the outbound path.

Parameters

string $path: The path to process, with a leading slash.

array $options: (optional) An associative array of additional options, with the following elements:

  • 'query': An array of query key/value-pairs (without any URL-encoding) to append to the URL.
  • 'fragment': A fragment identifier (named anchor) to append to the URL. Do not include the leading '#' character.
  • 'absolute': Defaults to FALSE. Whether to force the output to be an absolute link (beginning with http:). Useful for links that will be displayed outside the site, such as in an RSS feed.
  • 'language': An optional language object used to look up the alias for the URL. If $options['language'] is omitted, it defaults to the current language for the language type LanguageInterface::TYPE_URL.
  • 'https': Whether this URL should point to a secure location. If not defined, the current scheme is used, so the user stays on HTTP or HTTPS respectively. TRUE enforces HTTPS and FALSE enforces HTTP.
  • 'base_url': Only used internally by a path processor, for example, to modify the base URL when a language dependent URL requires so.
  • 'prefix': Only used internally, to modify the path when a language dependent URL requires so.
  • 'route': The route object for the given path. It will be set by \Drupal\Core\Routing\UrlGenerator::generateFromRoute().

\Symfony\Component\HttpFoundation\Request $request: The HttpRequest object representing the current request.

\Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata: (optional) Object to collect path processors' bubbleable metadata.

Return value

string The processed path.

Overrides OutboundPathProcessorInterface::processOutbound

File

src/PathProcessor/PathProcessorSearchApiPage.php, line 105

Class

PathProcessorSearchApiPage
Class PathProcessorSearchApiPage.

Namespace

Drupal\search_api_page\PathProcessor

Code

public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
  if ($request === NULL) {
    return $path;
  }
  if (strpos($request
    ->get('_route'), 'search_api_page.') !== 0) {
    return $path;
  }

  // Skip processing of no 'Search API Page' routes.
  $url_object = \Drupal::service('path.validator')
    ->getUrlIfValid($path);
  if ($url_object && strpos($url_object
    ->getRouteName(), 'search_api_page.') !== 0) {
    return $path;
  }
  if (!isset($options['language']) || empty($options['language'])) {
    return $path;
  }
  $search_api_page_id = $request
    ->get('search_api_page_name');
  $config_name = 'search_api_page.search_api_page.' . $search_api_page_id;
  $original_language = $this->languageManager
    ->getConfigOverrideLanguage();
  $this->languageManager
    ->setConfigOverrideLanguage($options['language']);
  $path = \Drupal::config($config_name)
    ->get('path');
  $this->languageManager
    ->setConfigOverrideLanguage($original_language);

  // Preserve keys when switching between languages.
  if ($request
    ->get('keys')) {
    $path .= '/' . $request
      ->get('keys');
  }
  if (strpos($path, '/') !== 0) {
    return '/' . $path;
  }
  return $path;
}