3 namespace Drupal\pathauto\Form;
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\pathauto\AliasTypeManager;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
11 * Alias mass delete form.
13 class PathautoAdminDelete extends FormBase {
16 * The alias type manager.
18 * @var \Drupal\pathauto\AliasTypeManager
20 protected $aliasTypeManager;
23 * Constructs a PathautoAdminDelete object.
25 * @param \Drupal\pathauto\AliasTypeManager $alias_type_manager
26 * The alias type manager.
28 public function __construct(AliasTypeManager $alias_type_manager) {
29 $this->aliasTypeManager = $alias_type_manager;
35 public static function create(ContainerInterface $container) {
37 $container->get('plugin.manager.alias_type')
44 public function getFormId() {
45 return 'pathauto_admin_delete';
51 public function buildForm(array $form, FormStateInterface $form_state) {
53 '#type' => 'fieldset',
54 '#title' => $this->t('Choose aliases to delete'),
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]),
68 // Next, iterate over all visible alias types.
69 $definitions = $this->aliasTypeManager->getVisibleDefinitions();
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]),
84 '#type' => 'fieldset',
85 '#title' => $this->t('Delete options'),
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.'),
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'] = [
101 '#value' => $this->t('Delete aliases now!'),
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'])) {
116 'title' => $this->t('Bulk deleting URL aliases'),
117 'operations' => [['Drupal\pathauto\Form\PathautoAdminDelete::batchStart', [$delete_all]]],
118 'finished' => 'Drupal\pathauto\Form\PathautoAdminDelete::batchFinished',
122 foreach (array_keys($form_state->getValue(['delete', 'plugins'])) as $id) {
123 $batch['operations'][] = ['Drupal\pathauto\Form\PathautoAdminDelete::batchProcess', [$id]];
127 foreach (array_keys(array_filter($form_state->getValue(['delete', 'plugins']))) as $id) {
128 $batch['operations'][] = ['Drupal\pathauto\Form\PathautoAdminDelete::batchProcess', [$id]];
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.'));
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()]));
149 * Batch callback; record if aliases of all types must be deleted.
151 public static function batchStart($delete_all, &$context) {
152 $context['results']['delete_all'] = $delete_all;
153 $context['results']['deletions'] = [];
157 * Common batch processing callback for all operations.
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);
166 * Batch finished callback.
168 public static function batchFinished($success, $results, $operations) {
170 if ($results['delete_all']) {
171 \Drupal::service('messenger')
172 ->addMessage(t('All of your automatically generated path aliases have been deleted.'));
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.', [
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]),