PathProcessorSearchApiPage.php in Search API Pages 8
File
src/PathProcessor/PathProcessorSearchApiPage.php
View source
<?php
namespace Drupal\search_api_page\PathProcessor;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Request;
class PathProcessorSearchApiPage implements InboundPathProcessorInterface, OutboundPathProcessorInterface {
protected $entityTypeManager;
protected $languageManager;
protected $config;
public function __construct(EntityTypeManagerInterface $entityTypeManager, LanguageManagerInterface $languageManager, ConfigFactoryInterface $config) {
$this->entityTypeManager = $entityTypeManager;
$this->languageManager = $languageManager;
$this->config = $config;
}
public function processInbound($path, Request $request) {
foreach ($this
->getSearchApiPagePathsUsingCleanUrls() as $search_api_clean_url_path) {
$regex = '~^' . $search_api_clean_url_path . '~';
if (preg_match($regex, $path)) {
$keys = str_replace($search_api_clean_url_path, '', $path);
return $search_api_clean_url_path . rawurlencode($keys);
}
}
return $path;
}
protected function getSearchApiPagePathsUsingCleanUrls() {
$paths = [];
$is_multilingual = $this->languageManager
->isMultilingual();
$all_languages = $this->languageManager
->getLanguages();
foreach ($this->entityTypeManager
->getStorage('search_api_page')
->loadMultiple() as $search_api_page) {
$default_path = $search_api_page
->getPath();
foreach ($all_languages as $language) {
$path = '';
if ($is_multilingual) {
$path = $this->languageManager
->getLanguageConfigOverride($language
->getId(), 'search_api_page.search_api_page.' . $search_api_page
->id())
->get('path');
}
if (empty($path)) {
$path = $default_path;
}
if ($search_api_page
->getCleanUrl()) {
$path .= '/';
$paths[] = '/' . $path;
}
}
}
return $paths;
}
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;
}
$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);
if ($request
->get('keys')) {
$path .= '/' . $request
->get('keys');
}
if (strpos($path, '/') !== 0) {
return '/' . $path;
}
return $path;
}
}