Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / pathauto / src / Form / PathautoSettingsForm.php
1 <?php
2
3 namespace Drupal\pathauto\Form;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Entity\EntityFieldManagerInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Entity\FieldableEntityInterface;
10 use Drupal\Core\Form\ConfigFormBase;
11 use Drupal\Core\Url;
12 use Drupal\pathauto\AliasTypeManager;
13 use Drupal\pathauto\PathautoGeneratorInterface;
14 use Drupal\Core\Form\FormStateInterface;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
16
17 /**
18  * Configure file system settings for this site.
19  */
20 class PathautoSettingsForm extends ConfigFormBase {
21
22   /**
23    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
24    */
25   protected $entityTypeManager;
26
27   /**
28    * @var \Drupal\Core\Entity\EntityFieldManagerInterface
29    */
30   protected $entityFieldManager;
31
32   /**
33    * @var \Drupal\pathauto\AliasTypeManager
34    */
35   protected $aliasTypeManager;
36
37   /**
38    * {@inheritdoc}
39    */
40   public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, AliasTypeManager $alias_type_manager) {
41     parent::__construct($config_factory);
42     $this->entityTypeManager = $entity_type_manager;
43     $this->entityFieldManager = $entity_field_manager;
44     $this->aliasTypeManager = $alias_type_manager;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public static function create(ContainerInterface $container) {
51     return new static(
52       $container->get('config.factory'),
53       $container->get('entity_type.manager'),
54       $container->get('entity_field.manager'),
55       $container->get('plugin.manager.alias_type')
56     );
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function getFormId() {
63     return 'pathauto_settings_form';
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   protected function getEditableConfigNames() {
70     return ['pathauto.settings'];
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function buildForm(array $form, FormStateInterface $form_state) {
77     $config = $this->config('pathauto.settings');
78
79     $form['enabled_entity_types'] = [
80       '#type' => 'details',
81       '#open' => TRUE,
82       '#title' => $this->t('Enabled entity types'),
83       '#description' => $this->t('Enable to add a path field and allow to define alias patterns for the given type. Disabled types already define a path field themselves or currently have a pattern.'),
84       '#tree' => TRUE,
85     ];
86
87     // Get all applicable entity types.
88     foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
89       // Disable a checkbox if it already exists and if the entity type has
90       // patterns currently defined or if it isn't defined by us.
91       $patterns_count = \Drupal::entityQuery('pathauto_pattern')
92         ->condition('type', 'canonical_entities:' . $entity_type_id)
93         ->count()
94         ->execute();
95
96       if (is_subclass_of($entity_type->getClass(), FieldableEntityInterface::class) && $entity_type->hasLinkTemplate('canonical')) {
97         $field_definitions = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id);
98         $form['enabled_entity_types'][$entity_type_id] = [
99           '#type' => 'checkbox',
100           '#title' => $entity_type->getLabel(),
101           '#default_value' => isset($field_definitions['path']) || in_array($entity_type_id, $config->get('enabled_entity_types')),
102           '#disabled' => isset($field_definitions['path']) && ($field_definitions['path']->getProvider() != 'pathauto' || $patterns_count),
103         ];
104       }
105     }
106
107     $form['verbose'] = array(
108       '#type' => 'checkbox',
109       '#title' => $this->t('Verbose'),
110       '#default_value' => $config->get('verbose'),
111       '#description' => $this->t('Display alias changes (except during bulk updates).'),
112     );
113
114     $form['separator'] = array(
115       '#type' => 'textfield',
116       '#title' => $this->t('Separator'),
117       '#size' => 1,
118       '#maxlength' => 1,
119       '#default_value' => $config->get('separator'),
120       '#description' => $this->t('Character used to separate words in titles. This will replace any spaces and punctuation characters. Using a space or + character can cause unexpected results.'),
121     );
122
123     $form['case'] = array(
124       '#type' => 'checkbox',
125       '#title' => $this->t('Character case'),
126       '#default_value' => $config->get('case'),
127       '#description' => $this->t('Convert token values to lowercase.'),
128     );
129
130     $max_length = \Drupal::service('pathauto.alias_storage_helper')->getAliasSchemaMaxlength();
131
132     $help_link = '';
133     if (\Drupal::moduleHandler()->moduleExists('help')) {
134       $help_link = ' ' . $this->t('See <a href=":pathauto-help">Pathauto help</a> for details.', [':pathauto-help' => Url::fromRoute('help.page', ['name' => 'pathauto'])->toString()]);
135     }
136
137     $form['max_length'] = array(
138       '#type' => 'number',
139       '#title' => $this->t('Maximum alias length'),
140       '#size' => 3,
141       '#maxlength' => 3,
142       '#default_value' => $config->get('max_length'),
143       '#min' => 1,
144       '#max' => $max_length,
145       '#description' => $this->t('Maximum length of aliases to generate. 100 is the recommended length. @max is the maximum possible length.', array('@max' => $max_length)) . $help_link,
146     );
147
148     $form['max_component_length'] = array(
149       '#type' => 'number',
150       '#title' => $this->t('Maximum component length'),
151       '#size' => 3,
152       '#maxlength' => 3,
153       '#default_value' => $config->get('max_component_length'),
154       '#min' => 1,
155       '#max' => $max_length,
156       '#description' => $this->t('Maximum text length of any component in the alias (e.g., [title]). 100 is the recommended length. @max is the maximum possible length.', ['@max' => $max_length]) . $help_link,
157     );
158
159     $description = $this->t('What should Pathauto do when updating an existing content item which already has an alias?');
160     if (\Drupal::moduleHandler()->moduleExists('redirect')) {
161       $description .= ' ' . $this->t('The <a href=":url">Redirect module settings</a> affect whether a redirect is created when an alias is deleted.', array(':url' => Url::fromRoute('redirect.settings')->toString()));
162     }
163     else {
164       $description .= ' ' . $this->t('Considering installing the <a href=":url">Redirect module</a> to get redirects when your aliases change.', array(':url' => 'http://drupal.org/project/redirect'));
165     }
166
167     $form['update_action'] = array(
168       '#type' => 'radios',
169       '#title' => $this->t('Update action'),
170       '#default_value' => $config->get('update_action'),
171       '#options' => array(
172         PathautoGeneratorInterface::UPDATE_ACTION_NO_NEW => $this->t('Do nothing. Leave the old alias intact.'),
173         PathautoGeneratorInterface::UPDATE_ACTION_LEAVE => $this->t('Create a new alias. Leave the existing alias functioning.'),
174         PathautoGeneratorInterface::UPDATE_ACTION_DELETE => $this->t('Create a new alias. Delete the old alias.'),
175       ),
176       '#description' => $description,
177     );
178
179     $form['transliterate'] = array(
180       '#type' => 'checkbox',
181       '#title' => $this->t('Transliterate prior to creating alias'),
182       '#default_value' => $config->get('transliterate'),
183       '#description' => $this->t('When a pattern includes certain characters (such as those with accents) should Pathauto attempt to transliterate them into the US-ASCII alphabet?'),
184     );
185
186     $form['reduce_ascii'] = array(
187       '#type' => 'checkbox',
188       '#title' => $this->t('Reduce strings to letters and numbers'),
189       '#default_value' => $config->get('reduce_ascii'),
190       '#description' => $this->t('Filters the new alias to only letters and numbers found in the ASCII-96 set.'),
191     );
192
193     $form['ignore_words'] = array(
194       '#type' => 'textarea',
195       '#title' => $this->t('Strings to Remove'),
196       '#default_value' => $config->get('ignore_words'),
197       '#description' => $this->t('Words to strip out of the URL alias, separated by commas. Do not use this to remove punctuation.'),
198     );
199
200     $form['safe_tokens'] = array(
201       '#type' => 'textarea',
202       '#title' => $this->t('Safe tokens'),
203       '#default_value' => implode(', ', $config->get('safe_tokens')),
204       '#description' => $this->t('List of tokens that are safe to use in alias patterns and do not need to be cleaned. For example urls, aliases, machine names. Separated with a comma.'),
205     );
206
207     $form['punctuation'] = array(
208       '#type' => 'details',
209       '#title' => $this->t('Punctuation'),
210       '#open' => FALSE,
211       '#tree' => TRUE,
212     );
213
214     $punctuation = \Drupal::service('pathauto.alias_cleaner')->getPunctuationCharacters();
215
216     foreach ($punctuation as $name => $details) {
217       // Use the value from config if it exists.
218       if ($config->get('punctuation.' . $name) !== NULL) {
219         $details['default'] = $config->get('punctuation.' . $name);
220       }
221       else {
222         // Otherwise use the correct default.
223         $details['default'] = $details['value'] == $config->get('separator') ? PathautoGeneratorInterface::PUNCTUATION_REPLACE : PathautoGeneratorInterface::PUNCTUATION_REMOVE;
224       }
225       $form['punctuation'][$name] = array(
226         '#type' => 'select',
227         '#title' => $details['name'] . ' (<code>' . Html::escape($details['value']) . '</code>)',
228         '#default_value' => $details['default'],
229         '#options' => array(
230           PathautoGeneratorInterface::PUNCTUATION_REMOVE => $this->t('Remove'),
231           PathautoGeneratorInterface::PUNCTUATION_REPLACE => $this->t('Replace by separator'),
232           PathautoGeneratorInterface::PUNCTUATION_DO_NOTHING => $this->t('No action (do not replace)'),
233         ),
234       );
235     }
236
237     return parent::buildForm($form, $form_state);
238   }
239
240   /**
241    * {@inheritdoc}
242    */
243   public function submitForm(array &$form, FormStateInterface $form_state) {
244
245     $config = $this->config('pathauto.settings');
246
247     $form_state->cleanValues();
248
249     foreach ($form_state->getValues() as $key => $value) {
250       if ($key == 'enabled_entity_types') {
251         $enabled_entity_types = [];
252         foreach ($value as $entity_type_id => $enabled) {
253           $field_definitions = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id);
254           // Verify that the entity type is enabled and that it is not defined
255           // or defined by us before adding it to the configuration, so that
256           // we do not store an entity type that cannot be enabled or disabled.
257           if ($enabled && (!isset($field_definitions['path']) || ($field_definitions['path']->getProvider() === 'pathauto'))) {
258             $enabled_entity_types[] = $entity_type_id;
259           }
260         }
261         $value = $enabled_entity_types;
262       }
263       elseif ($key == 'safe_tokens') {
264         $value = array_filter(array_map('trim', explode(',', $value)));
265       }
266       $config->set($key, $value);
267     }
268     $config->save();
269
270     parent::submitForm($form, $form_state);
271   }
272
273 }