Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / pathauto / src / Form / PathautoAdminDelete.php
1 <?php
2
3 namespace Drupal\pathauto\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\pathauto\AliasTypeManager;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9
10 /**
11  * Alias mass delete form.
12  */
13 class PathautoAdminDelete extends FormBase {
14
15   /**
16    * The alias type manager.
17    *
18    * @var \Drupal\pathauto\AliasTypeManager
19    */
20   protected $aliasTypeManager;
21
22   /**
23    * Constructs a PathautoAdminDelete object.
24    *
25    * @param \Drupal\pathauto\AliasTypeManager $alias_type_manager
26    *   The alias type manager.
27    */
28   public function __construct(AliasTypeManager $alias_type_manager) {
29     $this->aliasTypeManager = $alias_type_manager;
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public static function create(ContainerInterface $container) {
36     return new static(
37       $container->get('plugin.manager.alias_type')
38     );
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function getFormId() {
45     return 'pathauto_admin_delete';
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function buildForm(array $form, FormStateInterface $form_state) {
52     $form['delete'] = [
53       '#type' => 'fieldset',
54       '#title' => $this->t('Choose aliases to delete'),
55       '#tree' => TRUE,
56     ];
57
58     // First we do the "all" case.
59     $storage_helper = \Drupal::service('pathauto.alias_storage_helper');
60     $total_count = $storage_helper->countAll();
61     $form['delete']['all_aliases'] = [
62       '#type' => 'checkbox',
63       '#title' => $this->t('All aliases'),
64       '#default_value' => FALSE,
65       '#description' => $this->t('Delete all aliases. Number of aliases which will be deleted: %count.', ['%count' => $total_count]),
66     ];
67
68     // Next, iterate over all visible alias types.
69     $definitions = $this->aliasTypeManager->getVisibleDefinitions();
70
71     foreach ($definitions as $id => $definition) {
72       /** @var \Drupal\pathauto\AliasTypeInterface $alias_type */
73       $alias_type = $this->aliasTypeManager->createInstance($id);
74       $count = $storage_helper->countBySourcePrefix($alias_type->getSourcePrefix());
75       $form['delete']['plugins'][$id] = [
76         '#type' => 'checkbox',
77         '#title' => (string) $definition['label'],
78         '#default_value' => FALSE,
79         '#description' => $this->t('Delete aliases for all @label. Number of aliases which will be deleted: %count.', ['@label' => (string) $definition['label'], '%count' => $count]),
80       ];
81     }
82
83     $form['options'] = [
84       '#type' => 'fieldset',
85       '#title' => $this->t('Delete options'),
86       '#tree' => TRUE,
87     ];
88
89     // Provide checkbox for not deleting custom aliases.
90     $form['options']['keep_custom_aliases'] = [
91       '#type' => 'checkbox',
92       '#title' => $this->t('Only delete automatically generated aliases'),
93       '#default_value' => TRUE,
94       '#description' => $this->t('When checked, aliases which have been manually set are not affected by this mass-deletion.'),
95     ];
96
97     // Warn them and give a button that shows we mean business.
98     $form['warning'] = ['#value' => '<p>' . $this->t('<strong>Note:</strong> there is no confirmation. Be sure of your action before clicking the "Delete aliases now!" button.<br />You may want to make a backup of the database and/or the url_alias table prior to using this feature.') . '</p>'];
99     $form['buttons']['submit'] = [
100       '#type' => 'submit',
101       '#value' => $this->t('Delete aliases now!'),
102     ];
103
104     return $form;
105   }
106
107   /**
108    * {@inheritdoc}
109    */
110   public function submitForm(array &$form, FormStateInterface $form_state) {
111     $delete_all = $form_state->getValue(['delete', 'all_aliases']);
112     // Keeping custom aliases forces us to go the slow way to correctly check
113     // the automatic/manual flag.
114     if ($form_state->getValue(['options', 'keep_custom_aliases'])) {
115       $batch = [
116         'title' => $this->t('Bulk deleting URL aliases'),
117         'operations' => [['Drupal\pathauto\Form\PathautoAdminDelete::batchStart', [$delete_all]]],
118         'finished' => 'Drupal\pathauto\Form\PathautoAdminDelete::batchFinished',
119       ];
120
121       if ($delete_all) {
122         foreach (array_keys($form_state->getValue(['delete', 'plugins'])) as $id) {
123           $batch['operations'][] = ['Drupal\pathauto\Form\PathautoAdminDelete::batchProcess', [$id]];
124         }
125       }
126       else {
127         foreach (array_keys(array_filter($form_state->getValue(['delete', 'plugins']))) as $id) {
128           $batch['operations'][] = ['Drupal\pathauto\Form\PathautoAdminDelete::batchProcess', [$id]];
129         }
130       }
131
132       batch_set($batch);
133     }
134     else if ($delete_all) {
135       \Drupal::service('pathauto.alias_storage_helper')->deleteAll();
136       $this->messenger()->addMessage($this->t('All of your path aliases have been deleted.'));
137     }
138     else {
139       $storage_helper = \Drupal::service('pathauto.alias_storage_helper');
140       foreach (array_keys(array_filter($form_state->getValue(['delete', 'plugins']))) as $id) {
141         $alias_type = $this->aliasTypeManager->createInstance($id);
142         $storage_helper->deleteBySourcePrefix((string) $alias_type->getSourcePrefix());
143         $this->messenger()->addMessage($this->t('All of your %label path aliases have been deleted.', ['%label' => $alias_type->getLabel()]));
144       }
145     }
146   }
147
148   /**
149    * Batch callback; record if aliases of all types must be deleted.
150    */
151   public static function batchStart($delete_all, &$context) {
152     $context['results']['delete_all'] = $delete_all;
153     $context['results']['deletions'] = [];
154   }
155
156   /**
157    * Common batch processing callback for all operations.
158    */
159   public static function batchProcess($id, &$context) {
160     /** @var \Drupal\pathauto\AliasTypeBatchUpdateInterface $alias_type */
161     $alias_type = \Drupal::service('plugin.manager.alias_type')->createInstance($id);
162     $alias_type->batchDelete($context);
163   }
164
165   /**
166    * Batch finished callback.
167    */
168   public static function batchFinished($success, $results, $operations) {
169     if ($success) {
170       if ($results['delete_all']) {
171         \Drupal::service('messenger')
172           ->addMessage(t('All of your automatically generated path aliases have been deleted.'));
173       }
174       else if (isset($results['deletions'])) {
175         foreach (array_values($results['deletions']) as $label) {
176           \Drupal::service('messenger')
177             ->addMessage(t('All of your automatically generated %label path aliases have been deleted.', [
178               '%label' => $label,
179             ]));
180         }
181       }
182     }
183     else {
184       $error_operation = reset($operations);
185       \Drupal::service('messenger')
186         ->addMessage(t('An error occurred while processing @operation with arguments : @args', [
187           '@operation' => $error_operation[0],
188           '@args' => print_r($error_operation[0]),
189         ]));
190     }
191   }
192
193 }