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
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\redirect_404\Form\RedirectFix404Form
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\FormView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
RedirectFix404Form:: |
protected | property | The date formatter service. | |
RedirectFix404Form:: |
protected | property | The entity manager. | |
RedirectFix404Form:: |
protected | property | The language manager. | |
RedirectFix404Form:: |
protected | property | The redirect storage. | |
RedirectFix404Form:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
RedirectFix404Form:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
RedirectFix404Form:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
RedirectFix404Form:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
RedirectFix404Form:: |
public | function | Constructs a RedirectFix404Form. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |