class PathautoBulkUpdateForm in Pathauto 8
Configure file system settings for this site.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\pathauto\Form\PathautoBulkUpdateForm
Expanded class hierarchy of PathautoBulkUpdateForm
1 file declares its use of PathautoBulkUpdateForm
- PathautoCommands.php in src/
Commands/ PathautoCommands.php
1 string reference to 'PathautoBulkUpdateForm'
File
- src/
Form/ PathautoBulkUpdateForm.php, line 16
Namespace
Drupal\pathauto\FormView source
class PathautoBulkUpdateForm extends FormBase {
/**
* Generate URL aliases for un-aliased paths only.
*/
const ACTION_CREATE = 'create';
/**
* Update URL aliases for paths that have an existing alias.
*/
const ACTION_UPDATE = 'update';
/**
* Regenerate URL aliases for all paths.
*/
const ACTION_ALL = 'all';
/**
* The alias type manager.
*
* @var \Drupal\pathauto\AliasTypeManager
*/
protected $aliasTypeManager;
/**
* Constructs a PathautoBulkUpdateForm object.
*
* @param \Drupal\pathauto\AliasTypeManager $alias_type_manager
* The alias type manager.
*/
public function __construct(AliasTypeManager $alias_type_manager) {
$this->aliasTypeManager = $alias_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.alias_type'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'pathauto_bulk_update_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = [];
$form['#update_callbacks'] = [];
$form['update'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Select the types of paths for which to generate URL aliases'),
'#options' => [],
'#default_value' => [],
];
$definitions = $this->aliasTypeManager
->getVisibleDefinitions();
foreach ($definitions as $id => $definition) {
$alias_type = $this->aliasTypeManager
->createInstance($id);
if ($alias_type instanceof AliasTypeBatchUpdateInterface) {
$form['update']['#options'][$id] = $alias_type
->getLabel();
}
}
$form['action'] = [
'#type' => 'radios',
'#title' => $this
->t('Select which URL aliases to generate'),
'#options' => [
static::ACTION_CREATE => $this
->t('Generate a URL alias for un-aliased paths only'),
],
'#default_value' => static::ACTION_CREATE,
];
$config = $this
->config('pathauto.settings');
if ($config
->get('update_action') == PathautoGeneratorInterface::UPDATE_ACTION_NO_NEW) {
// Existing aliases should not be updated.
$form['warning'] = [
'#markup' => $this
->t('<a href=":url">Pathauto settings</a> are set to ignore paths which already have a URL alias. You can only create URL aliases for paths having none.', [
':url' => Url::fromRoute('pathauto.settings.form')
->toString(),
]),
];
}
else {
$form['action']['#options'][static::ACTION_UPDATE] = $this
->t('Update the URL alias for paths having an old URL alias');
$form['action']['#options'][static::ACTION_ALL] = $this
->t('Regenerate URL aliases for all paths');
}
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Update'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$batch = [
'title' => $this
->t('Bulk updating URL aliases'),
'operations' => [
[
'Drupal\\pathauto\\Form\\PathautoBulkUpdateForm::batchStart',
[],
],
],
'finished' => 'Drupal\\pathauto\\Form\\PathautoBulkUpdateForm::batchFinished',
];
$action = $form_state
->getValue('action');
foreach ($form_state
->getValue('update') as $id) {
if (!empty($id)) {
$batch['operations'][] = [
'Drupal\\pathauto\\Form\\PathautoBulkUpdateForm::batchProcess',
[
$id,
$action,
],
];
}
}
batch_set($batch);
}
/**
* Batch callback; initialize the number of updated aliases.
*/
public static function batchStart(&$context) {
$context['results']['updates'] = 0;
}
/**
* Common batch processing callback for all operations.
*
* Required to load our include the proper batch file.
*/
public static function batchProcess($id, $action, &$context) {
/** @var \Drupal\pathauto\AliasTypeBatchUpdateInterface $alias_type */
$alias_type = \Drupal::service('plugin.manager.alias_type')
->createInstance($id);
$alias_type
->batchUpdate($action, $context);
}
/**
* Batch finished callback.
*/
public static function batchFinished($success, $results, $operations) {
if ($success) {
if ($results['updates']) {
\Drupal::service('messenger')
->addMessage(\Drupal::translation()
->formatPlural($results['updates'], 'Generated 1 URL alias.', 'Generated @count URL aliases.'));
}
else {
\Drupal::service('messenger')
->addMessage(t('No new URL aliases to generate.'));
}
}
else {
$error_operation = reset($operations);
\Drupal::service('messenger')
->addMessage(t('An error occurred while processing @operation with arguments : @args'), [
'@operation' => $error_operation[0],
'@args' => print_r($error_operation[0]),
]);
}
}
}
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. | |
PathautoBulkUpdateForm:: |
protected | property | The alias type manager. | |
PathautoBulkUpdateForm:: |
constant | Regenerate URL aliases for all paths. | ||
PathautoBulkUpdateForm:: |
constant | Generate URL aliases for un-aliased paths only. | ||
PathautoBulkUpdateForm:: |
constant | Update URL aliases for paths that have an existing alias. | ||
PathautoBulkUpdateForm:: |
public static | function | Batch finished callback. | |
PathautoBulkUpdateForm:: |
public static | function | Common batch processing callback for all operations. | |
PathautoBulkUpdateForm:: |
public static | function | Batch callback; initialize the number of updated aliases. | |
PathautoBulkUpdateForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
PathautoBulkUpdateForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
PathautoBulkUpdateForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
PathautoBulkUpdateForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
PathautoBulkUpdateForm:: |
public | function | Constructs a PathautoBulkUpdateForm object. | |
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. | |
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. |