You are here

class RedirectFix404Form in Redirect 8

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

This is a fallback for the provided default view.

Hierarchy

Expanded class hierarchy of RedirectFix404Form

1 string reference to 'RedirectFix404Form'
redirect_404.routing.yml in modules/redirect_404/redirect_404.routing.yml
modules/redirect_404/redirect_404.routing.yml

File

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

Namespace

Drupal\redirect_404\Form
View source
class RedirectFix404Form extends FormBase {

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * The redirect storage.
   *
   * @var \Drupal\redirect_404\SqlRedirectNotFoundStorage
   */
  protected $redirectStorage;

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a RedirectFix404Form.
   *
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param \Drupal\redirect_404\SqlRedirectNotFoundStorage $redirect_storage
   *   The redirect storage.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date Formatter service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity manager.
   */
  public function __construct(LanguageManagerInterface $language_manager, SqlRedirectNotFoundStorage $redirect_storage, DateFormatterInterface $date_formatter, EntityTypeManagerInterface $entity_type_manager) {
    $this->languageManager = $language_manager;
    $this->redirectStorage = $redirect_storage;
    $this->dateFormatter = $date_formatter;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('language_manager'), $container
      ->get('redirect.not_found_storage'), $container
      ->get('date.formatter'), $container
      ->get('entity_type.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'redirect_fix_404_form';
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    if ($form_state
      ->getTriggeringElement()['#action'] == 'filter') {
      $form_state
        ->setRedirect('redirect_404.fix_404', [], [
        'query' => [
          'search' => trim($form_state
            ->getValue('filter')),
        ],
      ]);
    }
    else {
      $form_state
        ->setRedirect('redirect_404.fix_404');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
RedirectFix404Form::$dateFormatter protected property The date formatter service.
RedirectFix404Form::$entityTypeManager protected property The entity manager.
RedirectFix404Form::$languageManager protected property The language manager.
RedirectFix404Form::$redirectStorage protected property The redirect storage.
RedirectFix404Form::buildForm public function Form constructor. Overrides FormInterface::buildForm
RedirectFix404Form::create public static function Instantiates a new instance of this class. Overrides FormBase::create
RedirectFix404Form::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
RedirectFix404Form::submitForm public function Form submission handler. Overrides FormInterface::submitForm
RedirectFix404Form::__construct public function Constructs a RedirectFix404Form.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.