You are here

public function RedirectFix404Form::buildForm in Redirect 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

modules/redirect_404/src/Form/RedirectFix404Form.php, line 91

Class

RedirectFix404Form
Provides a form that lists all 404 error paths and no redirect assigned yet.

Namespace

Drupal\redirect_404\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $destination = $this
    ->getDestinationArray();
  $search = $this
    ->getRequest()
    ->get('search');
  $form['#attributes'] = [
    'class' => [
      'search-form',
    ],
  ];
  $form['basic'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Filter 404s'),
    '#attributes' => [
      'class' => [
        'container-inline',
      ],
    ],
  ];
  $form['basic']['filter'] = [
    '#type' => 'textfield',
    '#title' => '',
    '#default_value' => $search,
    '#maxlength' => 128,
    '#size' => 25,
  ];
  $form['basic']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Filter'),
    '#action' => 'filter',
  ];
  if ($search) {
    $form['basic']['reset'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Reset'),
      '#action' => 'reset',
    ];
  }
  $languages = $this->languageManager
    ->getLanguages(LanguageInterface::STATE_ALL);
  $multilingual = $this->languageManager
    ->isMultilingual();
  $header = [
    [
      'data' => $this
        ->t('Path'),
      'field' => 'source',
    ],
    [
      'data' => $this
        ->t('Count'),
      'field' => 'count',
      'sort' => 'desc',
    ],
    [
      'data' => $this
        ->t('Last accessed'),
      'field' => 'timestamp',
    ],
  ];
  if ($multilingual) {
    $header[] = [
      'data' => $this
        ->t('Language'),
      'field' => 'language',
    ];
  }
  $header[] = [
    'data' => $this
      ->t('Operations'),
  ];
  $rows = [];
  $results = $this->redirectStorage
    ->listRequests($header, $search);
  foreach ($results as $result) {
    $path = ltrim($result->path, '/');
    $row = [];
    $row['source'] = $path;
    $row['count'] = $result->count;
    $row['timestamp'] = $this->dateFormatter
      ->format($result->timestamp, 'short');
    if ($multilingual) {
      if (isset($languages[$result->langcode])) {
        $row['language'] = $languages[$result->langcode]
          ->getName();
      }
      else {
        $row['language'] = $this
          ->t('Undefined @langcode', [
          '@langcode' => $result->langcode,
        ]);
      }
    }
    $operations = [];
    if ($this->entityTypeManager
      ->getAccessControlHandler('redirect')
      ->createAccess()) {
      $operations['add'] = [
        'title' => $this
          ->t('Add redirect'),
        'url' => Url::fromRoute('redirect.add', [], [
          'query' => [
            'source' => $path,
            'language' => $result->langcode,
          ] + $destination,
        ]),
      ];
    }
    $row['operations'] = [
      'data' => [
        '#type' => 'operations',
        '#links' => $operations,
      ],
    ];
    $rows[] = $row;
  }
  $form['redirect_404_table'] = [
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => $this
      ->t('There are no 404 errors to fix.'),
  ];
  $form['redirect_404_pager'] = [
    '#type' => 'pager',
  ];
  return $form;
}