comparison core/modules/path/src/Controller/PathController.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
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
63 * \Drupal\Core\Render\RendererInterface::render().
64 */
65 public function adminOverview(Request $request) {
66 $keys = $request->query->get('search');
67 // Add the filter form above the overview table.
68 $build['path_admin_filter_form'] = $this->formBuilder()->getForm('Drupal\path\Form\PathFilterForm', $keys);
69 // Enable language column if language.module is enabled or if we have any
70 // alias with a language.
71 $multilanguage = ($this->moduleHandler()->moduleExists('language') || $this->aliasStorage->languageAliasExists());
72
73 $header = [];
74 $header[] = ['data' => $this->t('Alias'), 'field' => 'alias', 'sort' => 'asc'];
75 $header[] = ['data' => $this->t('System'), 'field' => 'source'];
76 if ($multilanguage) {
77 $header[] = ['data' => $this->t('Language'), 'field' => 'langcode'];
78 }
79 $header[] = $this->t('Operations');
80
81 $rows = [];
82 $destination = $this->getDestinationArray();
83 foreach ($this->aliasStorage->getAliasesForAdminListing($header, $keys) as $data) {
84 $row = [];
85 // @todo Should Path module store leading slashes? See
86 // https://www.drupal.org/node/2430593.
87 $row['data']['alias'] = $this->l(Unicode::truncate($data->alias, 50, FALSE, TRUE), Url::fromUserInput($data->source, [
88 'attributes' => ['title' => $data->alias],
89 ]));
90 $row['data']['source'] = $this->l(Unicode::truncate($data->source, 50, FALSE, TRUE), Url::fromUserInput($data->source, [
91 'alias' => TRUE,
92 'attributes' => ['title' => $data->source],
93 ]));
94 if ($multilanguage) {
95 $row['data']['language_name'] = $this->languageManager()->getLanguageName($data->langcode);
96 }
97
98 $operations = [];
99 $operations['edit'] = [
100 'title' => $this->t('Edit'),
101 'url' => Url::fromRoute('path.admin_edit', ['pid' => $data->pid], ['query' => $destination]),
102 ];
103 $operations['delete'] = [
104 'title' => $this->t('Delete'),
105 'url' => Url::fromRoute('path.delete', ['pid' => $data->pid], ['query' => $destination]),
106 ];
107 $row['data']['operations'] = [
108 'data' => [
109 '#type' => 'operations',
110 '#links' => $operations,
111 ],
112 ];
113
114 // If the system path maps to a different URL alias, highlight this table
115 // row to let the user know of old aliases.
116 if ($data->alias != $this->aliasManager->getAliasByPath($data->source, $data->langcode)) {
117 $row['class'] = ['warning'];
118 }
119
120 $rows[] = $row;
121 }
122
123 $build['path_table'] = [
124 '#type' => 'table',
125 '#header' => $header,
126 '#rows' => $rows,
127 '#empty' => $this->t('No URL aliases available. <a href=":link">Add URL alias</a>.', [':link' => $this->url('path.admin_add')]),
128 ];
129 $build['path_pager'] = ['#type' => 'pager'];
130
131 return $build;
132 }
133
134 }