You are here

class PathProcessorSearchApiPage in Search API Pages 8

Class PathProcessorSearchApiPage.

Hierarchy

Expanded class hierarchy of PathProcessorSearchApiPage

1 string reference to 'PathProcessorSearchApiPage'
search_api_page.services.yml in ./search_api_page.services.yml
search_api_page.services.yml
1 service uses PathProcessorSearchApiPage
path_processor.search_api_page in ./search_api_page.services.yml
Drupal\search_api_page\PathProcessor\PathProcessorSearchApiPage

File

src/PathProcessor/PathProcessorSearchApiPage.php, line 17

Namespace

Drupal\search_api_page\PathProcessor
View source
class PathProcessorSearchApiPage implements InboundPathProcessorInterface, OutboundPathProcessorInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $config;

  /**
   * PathProcessorSearchApiPage constructor.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager, LanguageManagerInterface $languageManager, ConfigFactoryInterface $config) {
    $this->entityTypeManager = $entityTypeManager;
    $this->languageManager = $languageManager;
    $this->config = $config;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * Get Search API page path for clean urls.
   */
  protected function getSearchApiPagePathsUsingCleanUrls() {
    $paths = [];
    $is_multilingual = $this->languageManager
      ->isMultilingual();
    $all_languages = $this->languageManager
      ->getLanguages();

    /* @var $search_api_page \Drupal\search_api_page\SearchApiPageInterface */
    foreach ($this->entityTypeManager
      ->getStorage('search_api_page')
      ->loadMultiple() as $search_api_page) {

      // Default path.
      $default_path = $search_api_page
        ->getPath();

      // Loop over all languages so we can get the translated path (if any).
      foreach ($all_languages as $language) {
        $path = '';

        // Check if we are multilingual or not.
        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;
        }

        // Use clean urls or not.
        if ($search_api_page
          ->getCleanUrl()) {
          $path .= '/';
          $paths[] = '/' . $path;
        }
      }
    }
    return $paths;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PathProcessorSearchApiPage::$config protected property The configuration factory.
PathProcessorSearchApiPage::$entityTypeManager protected property The entity type manager.
PathProcessorSearchApiPage::$languageManager protected property The language manager.
PathProcessorSearchApiPage::getSearchApiPagePathsUsingCleanUrls protected function Get Search API page path for clean urls.
PathProcessorSearchApiPage::processInbound public function Processes the inbound path. Overrides InboundPathProcessorInterface::processInbound
PathProcessorSearchApiPage::processOutbound public function Processes the outbound path. Overrides OutboundPathProcessorInterface::processOutbound
PathProcessorSearchApiPage::__construct public function PathProcessorSearchApiPage constructor.