3 namespace Drupal\pathauto\Form;
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;
12 use Drupal\pathauto\AliasTypeManager;
13 use Drupal\pathauto\PathautoGeneratorInterface;
14 use Drupal\Core\Form\FormStateInterface;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
18 * Configure file system settings for this site.
20 class PathautoSettingsForm extends ConfigFormBase {
23 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
25 protected $entityTypeManager;
28 * @var \Drupal\Core\Entity\EntityFieldManagerInterface
30 protected $entityFieldManager;
33 * @var \Drupal\pathauto\AliasTypeManager
35 protected $aliasTypeManager;
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;
50 public static function create(ContainerInterface $container) {
52 $container->get('config.factory'),
53 $container->get('entity_type.manager'),
54 $container->get('entity_field.manager'),
55 $container->get('plugin.manager.alias_type')
62 public function getFormId() {
63 return 'pathauto_settings_form';
69 protected function getEditableConfigNames() {
70 return ['pathauto.settings'];
76 public function buildForm(array $form, FormStateInterface $form_state) {
77 $config = $this->config('pathauto.settings');
79 $form['enabled_entity_types'] = [
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.'),
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)
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),
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).'),
114 $form['separator'] = array(
115 '#type' => 'textfield',
116 '#title' => $this->t('Separator'),
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.'),
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.'),
130 $max_length = \Drupal::service('pathauto.alias_storage_helper')->getAliasSchemaMaxlength();
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()]);
137 $form['max_length'] = array(
139 '#title' => $this->t('Maximum alias length'),
142 '#default_value' => $config->get('max_length'),
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,
148 $form['max_component_length'] = array(
150 '#title' => $this->t('Maximum component length'),
153 '#default_value' => $config->get('max_component_length'),
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,
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()));
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'));
167 $form['update_action'] = array(
169 '#title' => $this->t('Update action'),
170 '#default_value' => $config->get('update_action'),
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.'),
176 '#description' => $description,
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?'),
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.'),
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.'),
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.'),
207 $form['punctuation'] = array(
208 '#type' => 'details',
209 '#title' => $this->t('Punctuation'),
214 $punctuation = \Drupal::service('pathauto.alias_cleaner')->getPunctuationCharacters();
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);
222 // Otherwise use the correct default.
223 $details['default'] = $details['value'] == $config->get('separator') ? PathautoGeneratorInterface::PUNCTUATION_REPLACE : PathautoGeneratorInterface::PUNCTUATION_REMOVE;
225 $form['punctuation'][$name] = array(
227 '#title' => $details['name'] . ' (<code>' . Html::escape($details['value']) . '</code>)',
228 '#default_value' => $details['default'],
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)'),
237 return parent::buildForm($form, $form_state);
243 public function submitForm(array &$form, FormStateInterface $form_state) {
245 $config = $this->config('pathauto.settings');
247 $form_state->cleanValues();
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;
261 $value = $enabled_entity_types;
263 elseif ($key == 'safe_tokens') {
264 $value = array_filter(array_map('trim', explode(',', $value)));
266 $config->set($key, $value);
270 parent::submitForm($form, $form_state);