You are here

public function SearchApiPageRoutes::routes in Search API Pages 8

Returns an array of route objects.

Return value

\Symfony\Component\Routing\Route[] An array of route objects.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 string reference to 'SearchApiPageRoutes::routes'
search_api_page.routing.yml in ./search_api_page.routing.yml
search_api_page.routing.yml

File

src/Routing/SearchApiPageRoutes.php, line 63

Class

SearchApiPageRoutes
Defines a route subscriber to register a url for serving search pages.

Namespace

Drupal\search_api_page\Routing

Code

public function routes() {
  $routes = [];
  $is_multilingual = $this->languageManager
    ->isMultilingual();

  /* @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 ($this->languageManager
      ->getLanguages() as $language) {

      // Check if we are multilingual or not.
      $path = $default_path;
      if ($is_multilingual) {
        $pathOverride = $this->languageManager
          ->getLanguageConfigOverride($language
          ->getId(), 'search_api_page.search_api_page.' . $search_api_page
          ->id())
          ->get('path');
        if (!empty($pathOverride)) {
          $path = $pathOverride;
        }
      }
      $defaultArgs = [
        '_controller' => SearchApiPageController::class . '::page',
        '_title_callback' => SearchApiPageController::class . '::title',
        'search_api_page_name' => $search_api_page
          ->id(),
      ];

      // Use clean urls or not.
      if ($search_api_page
        ->getCleanUrl()) {
        $path .= '/{keys}';
        $defaultArgs['keys'] = '';
      }
      $routeName = 'search_api_page.' . $language
        ->getId() . '.' . $search_api_page
        ->id();
      $routeRequirements = [
        '_permission' => 'view search api pages',
        'keys' => '.*',
      ];
      $routes[$routeName] = new Route($path, $defaultArgs, $routeRequirements);
    }
  }
  return $routes;
}