Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / pathauto / src / Form / PathautoBulkUpdateForm.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\Core\Url;
8 use Drupal\pathauto\AliasTypeBatchUpdateInterface;
9 use Drupal\pathauto\AliasTypeManager;
10 use Drupal\pathauto\PathautoGeneratorInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Configure file system settings for this site.
15  */
16 class PathautoBulkUpdateForm extends FormBase {
17
18   /**
19    * The alias type manager.
20    *
21    * @var \Drupal\pathauto\AliasTypeManager
22    */
23   protected $aliasTypeManager;
24
25   /**
26    * Constructs a PathautoBulkUpdateForm object.
27    *
28    * @param \Drupal\pathauto\AliasTypeManager $alias_type_manager
29    *   The alias type manager.
30    */
31   public function __construct(AliasTypeManager $alias_type_manager) {
32     $this->aliasTypeManager = $alias_type_manager;
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public static function create(ContainerInterface $container) {
39     return new static(
40       $container->get('plugin.manager.alias_type')
41     );
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function getFormId() {
48     return 'pathauto_bulk_update_form';
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function buildForm(array $form, FormStateInterface $form_state) {
55
56     $form = array();
57
58     $form['#update_callbacks'] = array();
59
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(),
65     );
66
67     $definitions = $this->aliasTypeManager->getVisibleDefinitions();
68
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();
73       }
74     }
75
76     $form['action'] = array(
77       '#type' => 'radios',
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',
81     );
82
83     $config = $this->config('pathauto.settings');
84
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()]),
89       );
90     }
91     else {
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');
94     }
95
96     $form['actions']['#type'] = 'actions';
97     $form['actions']['submit'] = array(
98       '#type' => 'submit',
99       '#value' => $this->t('Update'),
100     );
101
102     return $form;
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   public function submitForm(array &$form, FormStateInterface $form_state) {
109     $batch = array(
110       'title' => $this->t('Bulk updating URL aliases'),
111       'operations' => array(
112         array('Drupal\pathauto\Form\PathautoBulkUpdateForm::batchStart', array()),
113       ),
114       'finished' => 'Drupal\pathauto\Form\PathautoBulkUpdateForm::batchFinished',
115     );
116
117     $action = $form_state->getValue('action');
118
119     foreach ($form_state->getValue('update') as $id) {
120       if (!empty($id)) {
121         $batch['operations'][] = array('Drupal\pathauto\Form\PathautoBulkUpdateForm::batchProcess', [$id, $action]);
122       }
123     }
124
125     batch_set($batch);
126   }
127
128   /**
129    * Batch callback; initialize the number of updated aliases.
130    */
131   public static function batchStart(&$context) {
132     $context['results']['updates'] = 0;
133   }
134
135   /**
136    * Common batch processing callback for all operations.
137    *
138    * Required to load our include the proper batch file.
139    */
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);
144   }
145
146   /**
147    * Batch finished callback.
148    */
149   public static function batchFinished($success, $results, $operations) {
150     if ($success) {
151       if ($results['updates']) {
152         \Drupal::service('messenger')->addMessage(\Drupal::translation()
153           ->formatPlural($results['updates'], 'Generated 1 URL alias.', 'Generated @count URL aliases.'));
154       }
155       else {
156         \Drupal::service('messenger')
157           ->addMessage(t('No new URL aliases to generate.'));
158       }
159     }
160     else {
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]),
166         ]);
167     }
168   }
169
170 }