Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / pathauto / src / Form / PatternEditForm.php
1 <?php
2
3 namespace Drupal\pathauto\Form;
4
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;
12
13 /**
14  * Edit form for pathauto patterns.
15  */
16 class PatternEditForm extends EntityForm {
17
18   /**
19    * The alias type manager.
20    *
21    * @var \Drupal\pathauto\AliasTypeManager
22    */
23   protected $manager;
24
25   /**
26    * @var \Drupal\pathauto\PathautoPatternInterface
27    */
28   protected $entity;
29
30   /**
31    * The entity type bundle info service.
32    *
33    * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
34    */
35   protected $entityTypeBundleInfo;
36
37   /**
38    * The entity manager service.
39    *
40    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
41    */
42   protected $entityTypeManager;
43
44   /**
45    * The language manager service.
46    *
47    * @var \Drupal\Core\Language\LanguageManagerInterface
48    */
49   protected $languageManager;
50
51   /**
52    * {@inheritdoc}
53    */
54   public static function create(ContainerInterface $container) {
55     return new static(
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')
60     );
61   }
62
63   /**
64    * PatternEditForm constructor.
65    *
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.
74    */
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;
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function buildForm(array $form, FormStateInterface $form_state) {
86
87     $options = [];
88     foreach ($this->manager->getVisibleDefinitions() as $plugin_id => $plugin_definition) {
89       $options[$plugin_id] = $plugin_definition['label'];
90     }
91     $form['type'] = [
92       '#type' => 'select',
93       '#title' => $this->t('Pattern type'),
94       '#default_value' => $this->entity->getType(),
95       '#options' => $options,
96       '#required' => TRUE,
97       '#limit_validation_errors' => array(array('type')),
98       '#submit' => array('::submitSelectType'),
99       '#executes_submit_callback' => TRUE,
100       '#ajax' => array(
101         'callback' => '::ajaxReplacePatternForm',
102         'wrapper' => 'pathauto-pattern',
103         'method' => 'replace',
104       ),
105     ];
106
107     $form['pattern_container'] = [
108       '#type' => 'container',
109       '#prefix' => '<div id="pathauto-pattern">',
110       '#suffix' => '</div>',
111     ];
112
113     // if there is no type yet, stop here.
114     if ($this->entity->getType()) {
115
116       $alias_type = $this->entity->getAliasType();
117
118       $form['pattern_container']['pattern'] = array(
119         '#type' => 'textfield',
120         '#title' => 'Path pattern',
121         '#default_value' => $this->entity->getPattern(),
122         '#size' => 65,
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(),
127         '#min_tokens' => 1,
128         '#required' => TRUE,
129       );
130
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(),
135       );
136
137       // Expose bundle and language conditions.
138       if ($alias_type->getDerivativeId() && $entity_type = $this->entityTypeManager->getDefinition($alias_type->getDerivativeId())) {
139
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'];
145           }
146           elseif ($condition->getPluginId() == 'language') {
147             $default_languages = $condition->getConfiguration()['langcodes'];
148           }
149         }
150
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'];
155           }
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.'),
162           );
163         }
164
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();
169           }
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.'),
176           );
177         }
178       }
179     }
180
181     $form['label'] = array(
182       '#type' => 'textfield',
183       '#title' => $this->t('Label'),
184       '#maxlength' => 255,
185       '#default_value' => $this->entity->label(),
186       '#required' => TRUE,
187       '#description' => $this->t('A short name to help you identify this pattern in the patterns list.'),
188     );
189
190     $form['id'] = array(
191       '#type' => 'machine_name',
192       '#title' => $this->t('ID'),
193       '#maxlength' => 255,
194       '#default_value' => $this->entity->id(),
195       '#required' => TRUE,
196       '#disabled' => !$this->entity->isNew(),
197       '#machine_name' => array(
198         'exists' => 'Drupal\pathauto\Entity\PathautoPattern::load',
199       ),
200     );
201
202     $form['status'] = [
203       '#title' => $this->t('Enabled'),
204       '#type' => 'checkbox',
205       '#default_value' => $this->entity->status(),
206     ];
207
208     return parent::buildForm($form, $form_state);
209   }
210
211   /**
212    * {@inheritdoc}
213    */
214   public function buildEntity(array $form, FormStateInterface $form_state) {
215     /** @var \Drupal\pathauto\PathautoPatternInterface $entity */
216     $entity = parent::buildEntity($form, $form_state);
217
218     // Will only be used for new patterns.
219     $default_weight = 0;
220
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);
228         }
229       }
230
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(
235           [
236             'id' => $plugin_id,
237             'bundles' => $bundles,
238             'negate' => FALSE,
239             'context_mapping' => [
240               $entity_type => $entity_type,
241             ]
242           ]
243         );
244       }
245
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(
250           [
251             'id' => 'language',
252             'langcodes' => array_combine($languages, $languages),
253             'negate' => FALSE,
254             'context_mapping' => [
255               'language' => $language_mapping,
256             ]
257           ]
258         );
259         $entity->addRelationship($language_mapping, t('Language'));
260       }
261
262     }
263
264     if ($entity->isNew()) {
265       $entity->setWeight($default_weight);
266     }
267
268     return $entity;
269   }
270
271   /**
272    * {@inheritdoc}
273    */
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(),
278     ]));
279     $form_state->setRedirectUrl($this->entity->toUrl('collection'));
280   }
281
282   /**
283    * Handles switching the type selector.
284    */
285   public function ajaxReplacePatternForm($form, FormStateInterface $form_state) {
286     return $form['pattern_container'];
287   }
288
289   /**
290    * Handles submit call when alias type is selected.
291    */
292   public function submitSelectType(array $form, FormStateInterface $form_state) {
293     $this->entity = $this->buildEntity($form, $form_state);
294     $form_state->setRebuild();
295   }
296
297 }