Version 1
[yaffs-website] / web / modules / contrib / redirect / modules / redirect_404 / src / Form / RedirectFix404Form.php
1 <?php
2
3 namespace Drupal\redirect_404\Form;
4
5 use Drupal\Core\Datetime\DateFormatterInterface;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Drupal\Core\Form\FormBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Language\LanguageInterface;
10 use Drupal\Core\Language\LanguageManagerInterface;
11 use Drupal\Core\Url;
12 use Drupal\redirect_404\SqlRedirectNotFoundStorage;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14
15 /**
16  * Provides a form that lists all 404 error paths and no redirect assigned yet.
17  *
18  * This is a fallback for the provided default view.
19  */
20 class RedirectFix404Form extends FormBase {
21
22   /**
23    * The language manager.
24    *
25    * @var \Drupal\Core\Language\LanguageManagerInterface
26    */
27   protected $languageManager;
28
29   /**
30    * The redirect storage.
31    *
32    * @var \Drupal\redirect_404\SqlRedirectNotFoundStorage
33    */
34   protected $redirectStorage;
35
36   /**
37    * The date formatter service.
38    *
39    * @var \Drupal\Core\Datetime\DateFormatterInterface
40    */
41   protected $dateFormatter;
42
43   /**
44    * The entity manager.
45    *
46    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
47    */
48   protected $entityTypeManager;
49
50   /**
51    * Constructs a RedirectFix404Form.
52    *
53    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
54    *   The language manager.
55    * @param \Drupal\redirect_404\SqlRedirectNotFoundStorage $redirect_storage
56    *   The redirect storage.
57    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
58    *   The date Formatter service.
59    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
60    *   The entity manager.
61    */
62   public function __construct(LanguageManagerInterface $language_manager, SqlRedirectNotFoundStorage $redirect_storage, DateFormatterInterface $date_formatter, EntityTypeManagerInterface $entity_type_manager) {
63     $this->languageManager = $language_manager;
64     $this->redirectStorage = $redirect_storage;
65     $this->dateFormatter = $date_formatter;
66     $this->entityTypeManager = $entity_type_manager;
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public static function create(ContainerInterface $container) {
73     return new static(
74       $container->get('language_manager'),
75       $container->get('redirect.not_found_storage'),
76       $container->get('date.formatter'),
77       $container->get('entity_type.manager')
78     );
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function getFormId() {
85     return 'redirect_fix_404_form';
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function buildForm(array $form, FormStateInterface $form_state) {
92     $destination = $this->getDestinationArray();
93
94     $search = $this->getRequest()->get('search');
95     $form['#attributes'] = ['class' => ['search-form']];
96
97     $form['basic'] = [
98       '#type' => 'fieldset',
99       '#title' => $this->t('Filter 404s'),
100       '#attributes' => ['class' => ['container-inline']],
101     ];
102     $form['basic']['filter'] = [
103       '#type' => 'textfield',
104       '#title' => '',
105       '#default_value' => $search,
106       '#maxlength' => 128,
107       '#size' => 25,
108     ];
109     $form['basic']['submit'] = [
110       '#type' => 'submit',
111       '#value' => $this->t('Filter'),
112       '#action' => 'filter',
113     ];
114     if ($search) {
115       $form['basic']['reset'] = [
116         '#type' => 'submit',
117         '#value' => $this->t('Reset'),
118         '#action' => 'reset',
119       ];
120     }
121
122     $languages = $this->languageManager->getLanguages(LanguageInterface::STATE_ALL);
123     $multilingual = $this->languageManager->isMultilingual();
124
125     $header = [
126       ['data' => $this->t('Path'), 'field' => 'source'],
127       ['data' => $this->t('Count'), 'field' => 'count', 'sort' => 'desc'],
128       ['data' => $this->t('Last accessed'), 'field' => 'timestamp'],
129     ];
130     if ($multilingual) {
131       $header[] = ['data' => $this->t('Language'), 'field' => 'language'];
132     }
133     $header[] = ['data' => $this->t('Operations')];
134
135     $rows = [];
136     $results = $this->redirectStorage->listRequests($header, $search);
137     foreach ($results as $result) {
138       $path = ltrim($result->path, '/');
139
140       $row = [];
141       $row['source'] = $path;
142       $row['count'] = $result->count;
143       $row['timestamp'] = $this->dateFormatter->format($result->timestamp, 'short');
144       if ($multilingual) {
145         if (isset($languages[$result->langcode])) {
146           $row['language'] = $languages[$result->langcode]->getName();
147         }
148         else {
149           $row['language'] = $this->t('Undefined @langcode', ['@langcode' => $result->langcode]);
150         }
151       }
152
153       $operations = [];
154       if ($this->entityTypeManager->getAccessControlHandler('redirect')->createAccess()) {
155         $operations['add'] = [
156           'title' => $this->t('Add redirect'),
157           'url' => Url::fromRoute('redirect.add', [], ['query' => ['source' => $path, 'language' => $result->langcode] + $destination]),
158         ];
159       }
160       $row['operations'] = [
161         'data' => [
162           '#type' => 'operations',
163           '#links' => $operations,
164         ],
165       ];
166
167       $rows[] = $row;
168     }
169
170     $form['redirect_404_table']  = [
171       '#theme' => 'table',
172       '#header' => $header,
173       '#rows' => $rows,
174       '#empty' => $this->t('There are no 404 errors to fix.'),
175     ];
176     $form['redirect_404_pager'] = ['#type' => 'pager'];
177     return $form;
178   }
179
180   /**
181    * {@inheritdoc}
182    */
183   public function submitForm(array &$form, FormStateInterface $form_state) {
184
185     if ($form_state->getTriggeringElement()['#action'] == 'filter') {
186       $form_state->setRedirect('redirect_404.fix_404', [], ['query' => ['search' => trim($form_state->getValue('filter'))]]);
187     }
188     else {
189       $form_state->setRedirect('redirect_404.fix_404');
190     }
191   }
192
193 }