3 namespace Drupal\pathauto\Form;
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\pathauto\AliasTypeBatchUpdateInterface;
9 use Drupal\pathauto\AliasTypeManager;
10 use Drupal\pathauto\PathautoGeneratorInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Configure file system settings for this site.
16 class PathautoBulkUpdateForm extends FormBase {
19 * The alias type manager.
21 * @var \Drupal\pathauto\AliasTypeManager
23 protected $aliasTypeManager;
26 * Constructs a PathautoBulkUpdateForm object.
28 * @param \Drupal\pathauto\AliasTypeManager $alias_type_manager
29 * The alias type manager.
31 public function __construct(AliasTypeManager $alias_type_manager) {
32 $this->aliasTypeManager = $alias_type_manager;
38 public static function create(ContainerInterface $container) {
40 $container->get('plugin.manager.alias_type')
47 public function getFormId() {
48 return 'pathauto_bulk_update_form';
54 public function buildForm(array $form, FormStateInterface $form_state) {
58 $form['#update_callbacks'] = array();
60 $form['update'] = array(
61 '#type' => 'checkboxes',
62 '#title' => $this->t('Select the types of paths for which to generate URL aliases'),
63 '#options' => array(),
64 '#default_value' => array(),
67 $definitions = $this->aliasTypeManager->getVisibleDefinitions();
69 foreach ($definitions as $id => $definition) {
70 $alias_type = $this->aliasTypeManager->createInstance($id);
71 if ($alias_type instanceof AliasTypeBatchUpdateInterface) {
72 $form['update']['#options'][$id] = $alias_type->getLabel();
76 $form['action'] = array(
78 '#title' => $this->t('Select which URL aliases to generate'),
79 '#options' => ['create' => $this->t('Generate a URL alias for un-aliased paths only')],
80 '#default_value' => 'create',
83 $config = $this->config('pathauto.settings');
85 if ($config->get('update_action') == PathautoGeneratorInterface::UPDATE_ACTION_NO_NEW) {
86 // Existing aliases should not be updated.
87 $form['warning'] = array(
88 '#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()]),
92 $form['action']['#options']['update'] = $this->t('Update the URL alias for paths having an old URL alias');
93 $form['action']['#options']['all'] = $this->t('Regenerate URL aliases for all paths');
96 $form['actions']['#type'] = 'actions';
97 $form['actions']['submit'] = array(
99 '#value' => $this->t('Update'),
108 public function submitForm(array &$form, FormStateInterface $form_state) {
110 'title' => $this->t('Bulk updating URL aliases'),
111 'operations' => array(
112 array('Drupal\pathauto\Form\PathautoBulkUpdateForm::batchStart', array()),
114 'finished' => 'Drupal\pathauto\Form\PathautoBulkUpdateForm::batchFinished',
117 $action = $form_state->getValue('action');
119 foreach ($form_state->getValue('update') as $id) {
121 $batch['operations'][] = array('Drupal\pathauto\Form\PathautoBulkUpdateForm::batchProcess', [$id, $action]);
129 * Batch callback; initialize the number of updated aliases.
131 public static function batchStart(&$context) {
132 $context['results']['updates'] = 0;
136 * Common batch processing callback for all operations.
138 * Required to load our include the proper batch file.
140 public static function batchProcess($id, $action, &$context) {
141 /** @var \Drupal\pathauto\AliasTypeBatchUpdateInterface $alias_type */
142 $alias_type = \Drupal::service('plugin.manager.alias_type')->createInstance($id);
143 $alias_type->batchUpdate($action, $context);
147 * Batch finished callback.
149 public static function batchFinished($success, $results, $operations) {
151 if ($results['updates']) {
152 \Drupal::service('messenger')->addMessage(\Drupal::translation()
153 ->formatPlural($results['updates'], 'Generated 1 URL alias.', 'Generated @count URL aliases.'));
156 \Drupal::service('messenger')
157 ->addMessage(t('No new URL aliases to generate.'));
161 $error_operation = reset($operations);
162 \Drupal::service('messenger')
163 ->addMessage(t('An error occurred while processing @operation with arguments : @args'), [
164 '@operation' => $error_operation[0],
165 '@args' => print_r($error_operation[0]),