Version 1
[yaffs-website] / web / core / modules / search / src / Routing / SearchPageRoutes.php
1 <?php
2
3 namespace Drupal\search\Routing;
4
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\search\SearchPageRepositoryInterface;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8 use Symfony\Component\Routing\Route;
9
10 /**
11  * Provides dynamic routes for search.
12  */
13 class SearchPageRoutes implements ContainerInjectionInterface {
14
15   /**
16    * The search page repository.
17    *
18    * @var \Drupal\search\SearchPageRepositoryInterface
19    */
20   protected $searchPageRepository;
21
22   /**
23    * Constructs a new search route subscriber.
24    *
25    * @param \Drupal\search\SearchPageRepositoryInterface $search_page_repository
26    *   The search page repository.
27    */
28   public function __construct(SearchPageRepositoryInterface $search_page_repository) {
29     $this->searchPageRepository = $search_page_repository;
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public static function create(ContainerInterface $container) {
36     return new static(
37       $container->get('search.search_page_repository')
38     );
39   }
40
41   /**
42    * Returns an array of route objects.
43    *
44    * @return \Symfony\Component\Routing\Route[]
45    *   An array of route objects.
46    */
47   public function routes() {
48     $routes = [];
49     // @todo Decide if /search should continue to redirect to /search/$default,
50     //   or just perform the appropriate search.
51     if ($default_page = $this->searchPageRepository->getDefaultSearchPage()) {
52       $routes['search.view'] = new Route(
53         '/search',
54         [
55           '_controller' => 'Drupal\search\Controller\SearchController::redirectSearchPage',
56           '_title' => 'Search',
57           'entity' => $default_page,
58         ],
59         [
60           '_entity_access' => 'entity.view',
61           '_permission' => 'search content',
62         ],
63         [
64           'parameters' => [
65             'entity' => [
66               'type' => 'entity:search_page',
67             ],
68           ],
69         ]
70       );
71     }
72     $active_pages = $this->searchPageRepository->getActiveSearchPages();
73     foreach ($active_pages as $entity_id => $entity) {
74       $routes["search.view_$entity_id"] = new Route(
75         '/search/' . $entity->getPath(),
76         [
77           '_controller' => 'Drupal\search\Controller\SearchController::view',
78           '_title' => 'Search',
79           'entity' => $entity_id,
80         ],
81         [
82           '_entity_access' => 'entity.view',
83           '_permission' => 'search content',
84         ],
85         [
86           'parameters' => [
87             'entity' => [
88               'type' => 'entity:search_page',
89             ],
90           ],
91         ]
92       );
93
94       $routes["search.help_$entity_id"] = new Route(
95         '/search/' . $entity->getPath() . '/help',
96         [
97           '_controller' => 'Drupal\search\Controller\SearchController::searchHelp',
98           '_title' => 'Search help',
99           'entity' => $entity_id,
100         ],
101         [
102           '_entity_access' => 'entity.view',
103           '_permission' => 'search content',
104         ],
105         [
106           'parameters' => [
107             'entity' => [
108               'type' => 'entity:search_page',
109             ],
110           ],
111         ]
112       );
113     }
114     return $routes;
115   }
116
117 }