3 namespace Drupal\redirect_404\Form;
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;
12 use Drupal\redirect_404\SqlRedirectNotFoundStorage;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Provides a form that lists all 404 error paths and no redirect assigned yet.
18 * This is a fallback for the provided default view.
20 class RedirectFix404Form extends FormBase {
23 * The language manager.
25 * @var \Drupal\Core\Language\LanguageManagerInterface
27 protected $languageManager;
30 * The redirect storage.
32 * @var \Drupal\redirect_404\SqlRedirectNotFoundStorage
34 protected $redirectStorage;
37 * The date formatter service.
39 * @var \Drupal\Core\Datetime\DateFormatterInterface
41 protected $dateFormatter;
46 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
48 protected $entityTypeManager;
51 * Constructs a RedirectFix404Form.
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
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;
72 public static function create(ContainerInterface $container) {
74 $container->get('language_manager'),
75 $container->get('redirect.not_found_storage'),
76 $container->get('date.formatter'),
77 $container->get('entity_type.manager')
84 public function getFormId() {
85 return 'redirect_fix_404_form';
91 public function buildForm(array $form, FormStateInterface $form_state) {
92 $destination = $this->getDestinationArray();
94 $search = $this->getRequest()->get('search');
95 $form['#attributes'] = ['class' => ['search-form']];
98 '#type' => 'fieldset',
99 '#title' => $this->t('Filter 404s'),
100 '#attributes' => ['class' => ['container-inline']],
102 $form['basic']['filter'] = [
103 '#type' => 'textfield',
105 '#default_value' => $search,
109 $form['basic']['submit'] = [
111 '#value' => $this->t('Filter'),
112 '#action' => 'filter',
115 $form['basic']['reset'] = [
117 '#value' => $this->t('Reset'),
118 '#action' => 'reset',
122 $languages = $this->languageManager->getLanguages(LanguageInterface::STATE_ALL);
123 $multilingual = $this->languageManager->isMultilingual();
126 ['data' => $this->t('Path'), 'field' => 'source'],
127 ['data' => $this->t('Count'), 'field' => 'count', 'sort' => 'desc'],
128 ['data' => $this->t('Last accessed'), 'field' => 'timestamp'],
131 $header[] = ['data' => $this->t('Language'), 'field' => 'language'];
133 $header[] = ['data' => $this->t('Operations')];
136 $results = $this->redirectStorage->listRequests($header, $search);
137 foreach ($results as $result) {
138 $path = ltrim($result->path, '/');
141 $row['source'] = $path;
142 $row['count'] = $result->count;
143 $row['timestamp'] = $this->dateFormatter->format($result->timestamp, 'short');
145 if (isset($languages[$result->langcode])) {
146 $row['language'] = $languages[$result->langcode]->getName();
149 $row['language'] = $this->t('Undefined @langcode', ['@langcode' => $result->langcode]);
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]),
160 $row['operations'] = [
162 '#type' => 'operations',
163 '#links' => $operations,
170 $form['redirect_404_table'] = [
172 '#header' => $header,
174 '#empty' => $this->t('There are no 404 errors to fix.'),
176 $form['redirect_404_pager'] = ['#type' => 'pager'];
183 public function submitForm(array &$form, FormStateInterface $form_state) {
185 if ($form_state->getTriggeringElement()['#action'] == 'filter') {
186 $form_state->setRedirect('redirect_404.fix_404', [], ['query' => ['search' => trim($form_state->getValue('filter'))]]);
189 $form_state->setRedirect('redirect_404.fix_404');