3 namespace Drupal\pathauto\Form;
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Language\LanguageManagerInterface;
10 use Drupal\pathauto\AliasTypeManager;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Edit form for pathauto patterns.
16 class PatternEditForm extends EntityForm {
19 * The alias type manager.
21 * @var \Drupal\pathauto\AliasTypeManager
26 * @var \Drupal\pathauto\PathautoPatternInterface
31 * The entity type bundle info service.
33 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
35 protected $entityTypeBundleInfo;
38 * The entity manager service.
40 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
42 protected $entityTypeManager;
45 * The language manager service.
47 * @var \Drupal\Core\Language\LanguageManagerInterface
49 protected $languageManager;
54 public static function create(ContainerInterface $container) {
56 $container->get('plugin.manager.alias_type'),
57 $container->get('entity_type.bundle.info'),
58 $container->get('entity_type.manager'),
59 $container->get('language_manager')
64 * PatternEditForm constructor.
66 * @param \Drupal\pathauto\AliasTypeManager $manager
67 * The alias type manager.
68 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
69 * The entity type bundle info service.
70 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
71 * The entity manager service.
72 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
73 * The language manager service.
75 function __construct(AliasTypeManager $manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager) {
76 $this->manager = $manager;
77 $this->entityTypeBundleInfo = $entity_type_bundle_info;
78 $this->entityTypeManager = $entity_type_manager;
79 $this->languageManager = $language_manager;
85 public function buildForm(array $form, FormStateInterface $form_state) {
88 foreach ($this->manager->getVisibleDefinitions() as $plugin_id => $plugin_definition) {
89 $options[$plugin_id] = $plugin_definition['label'];
93 '#title' => $this->t('Pattern type'),
94 '#default_value' => $this->entity->getType(),
95 '#options' => $options,
97 '#limit_validation_errors' => array(array('type')),
98 '#submit' => array('::submitSelectType'),
99 '#executes_submit_callback' => TRUE,
101 'callback' => '::ajaxReplacePatternForm',
102 'wrapper' => 'pathauto-pattern',
103 'method' => 'replace',
107 $form['pattern_container'] = [
108 '#type' => 'container',
109 '#prefix' => '<div id="pathauto-pattern">',
110 '#suffix' => '</div>',
113 // if there is no type yet, stop here.
114 if ($this->entity->getType()) {
116 $alias_type = $this->entity->getAliasType();
118 $form['pattern_container']['pattern'] = array(
119 '#type' => 'textfield',
120 '#title' => 'Path pattern',
121 '#default_value' => $this->entity->getPattern(),
123 '#maxlength' => 1280,
124 '#element_validate' => array('token_element_validate', 'pathauto_pattern_validate'),
125 '#after_build' => array('token_element_validate'),
126 '#token_types' => $alias_type->getTokenTypes(),
131 // Show the token help relevant to this pattern type.
132 $form['pattern_container']['token_help'] = array(
133 '#theme' => 'token_tree_link',
134 '#token_types' => $alias_type->getTokenTypes(),
137 // Expose bundle and language conditions.
138 if ($alias_type->getDerivativeId() && $entity_type = $this->entityTypeManager->getDefinition($alias_type->getDerivativeId())) {
140 $default_bundles = [];
141 $default_languages = [];
142 foreach ($this->entity->getSelectionConditions() as $condition_id => $condition) {
143 if (in_array($condition->getPluginId(), ['entity_bundle:' . $entity_type->id(), 'node_type'])) {
144 $default_bundles = $condition->getConfiguration()['bundles'];
146 elseif ($condition->getPluginId() == 'language') {
147 $default_languages = $condition->getConfiguration()['langcodes'];
151 if ($entity_type->hasKey('bundle') && $bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type->id())) {
152 $bundle_options = [];
153 foreach ($bundles as $id => $info) {
154 $bundle_options[$id] = $info['label'];
156 $form['pattern_container']['bundles'] = array(
157 '#title' => $entity_type->getBundleLabel(),
158 '#type' => 'checkboxes',
159 '#options' => $bundle_options,
160 '#default_value' => $default_bundles,
161 '#description' => $this->t('Check to which types this pattern should be applied. Leave empty to allow any.'),
165 if ($this->languageManager->isMultilingual() && $entity_type->isTranslatable()) {
166 $language_options = [];
167 foreach ($this->languageManager->getLanguages() as $id => $language) {
168 $language_options[$id] = $language->getName();
170 $form['pattern_container']['languages'] = array(
171 '#title' => $this->t('Languages'),
172 '#type' => 'checkboxes',
173 '#options' => $language_options,
174 '#default_value' => $default_languages,
175 '#description' => $this->t('Check to which languages this pattern should be applied. Leave empty to allow any.'),
181 $form['label'] = array(
182 '#type' => 'textfield',
183 '#title' => $this->t('Label'),
185 '#default_value' => $this->entity->label(),
187 '#description' => $this->t('A short name to help you identify this pattern in the patterns list.'),
191 '#type' => 'machine_name',
192 '#title' => $this->t('ID'),
194 '#default_value' => $this->entity->id(),
196 '#disabled' => !$this->entity->isNew(),
197 '#machine_name' => array(
198 'exists' => 'Drupal\pathauto\Entity\PathautoPattern::load',
203 '#title' => $this->t('Enabled'),
204 '#type' => 'checkbox',
205 '#default_value' => $this->entity->status(),
208 return parent::buildForm($form, $form_state);
214 public function buildEntity(array $form, FormStateInterface $form_state) {
215 /** @var \Drupal\pathauto\PathautoPatternInterface $entity */
216 $entity = parent::buildEntity($form, $form_state);
218 // Will only be used for new patterns.
221 $alias_type = $entity->getAliasType();
222 if ($alias_type->getDerivativeId() && $this->entityTypeManager->hasDefinition($alias_type->getDerivativeId())) {
223 $entity_type = $alias_type->getDerivativeId();
224 // First, remove bundle and language conditions.
225 foreach ($entity->getSelectionConditions() as $condition_id => $condition) {
226 if (in_array($condition->getPluginId(), ['entity_bundle:' . $entity_type, 'node_type', 'language'])) {
227 $entity->removeSelectionCondition($condition_id);
231 if ($bundles = array_filter((array) $form_state->getValue('bundles'))) {
232 $default_weight -= 5;
233 $plugin_id = $entity_type == 'node' ? 'node_type' : 'entity_bundle:' . $entity_type;
234 $entity->addSelectionCondition(
237 'bundles' => $bundles,
239 'context_mapping' => [
240 $entity_type => $entity_type,
246 if ($languages = array_filter((array) $form_state->getValue('languages'))) {
247 $default_weight -= 5;
248 $language_mapping = $entity_type . ':' . $this->entityTypeManager->getDefinition($entity_type)->getKey('langcode') . ':language';
249 $entity->addSelectionCondition(
252 'langcodes' => array_combine($languages, $languages),
254 'context_mapping' => [
255 'language' => $language_mapping,
259 $entity->addRelationship($language_mapping, t('Language'));
264 if ($entity->isNew()) {
265 $entity->setWeight($default_weight);
274 public function save(array $form, FormStateInterface $form_state) {
275 parent::save($form, $form_state);
276 $this->messenger()->addMessage($this->t('Pattern %label saved.', [
277 '%label' => $this->entity->label(),
279 $form_state->setRedirectUrl($this->entity->toUrl('collection'));
283 * Handles switching the type selector.
285 public function ajaxReplacePatternForm($form, FormStateInterface $form_state) {
286 return $form['pattern_container'];
290 * Handles submit call when alias type is selected.
292 public function submitSelectType(array $form, FormStateInterface $form_state) {
293 $this->entity = $this->buildEntity($form, $form_state);
294 $form_state->setRebuild();