51f88dd674e1930c76661c033a87357e7f391d16
[yaffs-website] / web / core / modules / path / src / Controller / PathController.php
1 <?php
2
3 namespace Drupal\path\Controller;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Controller\ControllerBase;
7 use Drupal\Core\Path\AliasStorageInterface;
8 use Drupal\Core\Path\AliasManagerInterface;
9 use Drupal\Core\Url;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 use Symfony\Component\HttpFoundation\Request;
12
13 /**
14  * Controller routines for path routes.
15  */
16 class PathController extends ControllerBase {
17
18   /**
19    * The path alias storage.
20    *
21    * @var \Drupal\Core\Path\AliasStorageInterface
22    */
23   protected $aliasStorage;
24
25   /**
26    * The path alias manager.
27    *
28    * @var \Drupal\Core\Path\AliasManagerInterface
29    */
30   protected $aliasManager;
31
32   /**
33    * Constructs a new PathController.
34    *
35    * @param \Drupal\Core\Path\AliasStorageInterface $alias_storage
36    *   The path alias storage.
37    * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
38    *   The path alias manager.
39    */
40   public function __construct(AliasStorageInterface $alias_storage, AliasManagerInterface $alias_manager) {
41     $this->aliasStorage = $alias_storage;
42     $this->aliasManager = $alias_manager;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public static function create(ContainerInterface $container) {
49     return new static(
50       $container->get('path.alias_storage'),
51       $container->get('path.alias_manager')
52     );
53   }
54
55   /**
56    * Displays the path administration overview page.
57    *
58    * @param \Symfony\Component\HttpFoundation\Request $request
59    *   The request object.
60    *
61    * @return array
62    *   A render array as expected by drupal_render().
63    */
64   public function adminOverview(Request $request) {
65     $keys = $request->query->get('search');
66     // Add the filter form above the overview table.
67     $build['path_admin_filter_form'] = $this->formBuilder()->getForm('Drupal\path\Form\PathFilterForm', $keys);
68     // Enable language column if language.module is enabled or if we have any
69     // alias with a language.
70     $multilanguage = ($this->moduleHandler()->moduleExists('language') || $this->aliasStorage->languageAliasExists());
71
72     $header = [];
73     $header[] = ['data' => $this->t('Alias'), 'field' => 'alias', 'sort' => 'asc'];
74     $header[] = ['data' => $this->t('System'), 'field' => 'source'];
75     if ($multilanguage) {
76       $header[] = ['data' => $this->t('Language'), 'field' => 'langcode'];
77     }
78     $header[] = $this->t('Operations');
79
80     $rows = [];
81     $destination = $this->getDestinationArray();
82     foreach ($this->aliasStorage->getAliasesForAdminListing($header, $keys) as $data) {
83       $row = [];
84       // @todo Should Path module store leading slashes? See
85       //   https://www.drupal.org/node/2430593.
86       $row['data']['alias'] = $this->l(Unicode::truncate($data->alias, 50, FALSE, TRUE), Url::fromUserInput($data->source, [
87         'attributes' => ['title' => $data->alias],
88       ]));
89       $row['data']['source'] = $this->l(Unicode::truncate($data->source, 50, FALSE, TRUE), Url::fromUserInput($data->source, [
90         'alias' => TRUE,
91         'attributes' => ['title' => $data->source],
92       ]));
93       if ($multilanguage) {
94         $row['data']['language_name'] = $this->languageManager()->getLanguageName($data->langcode);
95       }
96
97       $operations = [];
98       $operations['edit'] = [
99         'title' => $this->t('Edit'),
100         'url' => Url::fromRoute('path.admin_edit', ['pid' => $data->pid], ['query' => $destination]),
101       ];
102       $operations['delete'] = [
103         'title' => $this->t('Delete'),
104         'url' => Url::fromRoute('path.delete', ['pid' => $data->pid], ['query' => $destination]),
105       ];
106       $row['data']['operations'] = [
107         'data' => [
108           '#type' => 'operations',
109           '#links' => $operations,
110         ],
111       ];
112
113       // If the system path maps to a different URL alias, highlight this table
114       // row to let the user know of old aliases.
115       if ($data->alias != $this->aliasManager->getAliasByPath($data->source, $data->langcode)) {
116         $row['class'] = ['warning'];
117       }
118
119       $rows[] = $row;
120     }
121
122     $build['path_table'] = [
123       '#type' => 'table',
124       '#header' => $header,
125       '#rows' => $rows,
126       '#empty' => $this->t('No URL aliases available. <a href=":link">Add URL alias</a>.', [':link' => $this->url('path.admin_add')]),
127     ];
128     $build['path_pager'] = ['#type' => 'pager'];
129
130     return $build;
131   }
132
133 }