49efc7e77464befd413d86cdc1bb910e0bedbd89
[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    * @var \Drupal\pathauto\AliasTypeManager
20    */
21   protected $manager;
22
23   /**
24    * @var \Drupal\pathauto\PathautoPatternInterface
25    */
26   protected $entity;
27
28   /**
29    * The entity type bundle info service.
30    *
31    * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
32    */
33   protected $entityTypeBundleInfo;
34
35   /**
36    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
37    */
38   protected $entityTypeManager;
39
40   /**
41    * @var \Drupal\Core\Language\LanguageManagerInterface
42    */
43   protected $languageManager;
44
45   /**
46    * {@inheritdoc}
47    */
48   public static function create(ContainerInterface $container) {
49     return new static(
50       $container->get('plugin.manager.alias_type'),
51       $container->get('entity_type.bundle.info'),
52       $container->get('entity_type.manager'),
53       $container->get('language_manager')
54     );
55   }
56
57   /**
58    * PatternEditForm constructor.
59    *
60    * @param \Drupal\pathauto\AliasTypeManager $manager
61    * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
62    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
63    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
64    */
65   function __construct(AliasTypeManager $manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager) {
66     $this->manager = $manager;
67     $this->entityTypeBundleInfo = $entity_type_bundle_info;
68     $this->entityTypeManager = $entity_type_manager;
69     $this->languageManager = $language_manager;
70   }
71
72   /**
73    * {@inheritDoc}
74    */
75   public function buildForm(array $form, FormStateInterface $form_state) {
76
77     $options = [];
78     foreach ($this->manager->getVisibleDefinitions() as $plugin_id => $plugin_definition) {
79       $options[$plugin_id] = $plugin_definition['label'];
80     }
81     $form['type'] = [
82       '#type' => 'select',
83       '#title' => $this->t('Pattern type'),
84       '#default_value' => $this->entity->getType(),
85       '#options' => $options,
86       '#required' => TRUE,
87       '#limit_validation_errors' => array(array('type')),
88       '#submit' => array('::submitSelectType'),
89       '#executes_submit_callback' => TRUE,
90       '#ajax' => array(
91         'callback' => '::ajaxReplacePatternForm',
92         'wrapper' => 'pathauto-pattern',
93         'method' => 'replace',
94       ),
95     ];
96
97     $form['pattern_container'] = [
98       '#type' => 'container',
99       '#prefix' => '<div id="pathauto-pattern">',
100       '#suffix' => '</div>',
101     ];
102
103     // if there is no type yet, stop here.
104     if ($this->entity->getType()) {
105
106       $alias_type = $this->entity->getAliasType();
107
108       $form['pattern_container']['pattern'] = array(
109         '#type' => 'textfield',
110         '#title' => 'Path pattern',
111         '#default_value' => $this->entity->getPattern(),
112         '#size' => 65,
113         '#maxlength' => 1280,
114         '#element_validate' => array('token_element_validate', 'pathauto_pattern_validate'),
115         '#after_build' => array('token_element_validate'),
116         '#token_types' => $alias_type->getTokenTypes(),
117         '#min_tokens' => 1,
118         '#required' => TRUE,
119       );
120
121       // Show the token help relevant to this pattern type.
122       $form['pattern_container']['token_help'] = array(
123         '#theme' => 'token_tree_link',
124         '#token_types' => $alias_type->getTokenTypes(),
125       );
126
127       // Expose bundle and language conditions.
128       if ($alias_type->getDerivativeId() && $entity_type = $this->entityTypeManager->getDefinition($alias_type->getDerivativeId())) {
129
130         $default_bundles = [];
131         $default_languages = [];
132         foreach ($this->entity->getSelectionConditions() as $condition_id => $condition) {
133           if (in_array($condition->getPluginId(), ['entity_bundle:' . $entity_type->id(), 'node_type'])) {
134             $default_bundles = $condition->getConfiguration()['bundles'];
135           }
136           elseif ($condition->getPluginId() == 'language') {
137             $default_languages = $condition->getConfiguration()['langcodes'];
138           }
139         }
140
141         if ($entity_type->hasKey('bundle') && $bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type->id())) {
142           $bundle_options = [];
143           foreach ($bundles as $id => $info) {
144             $bundle_options[$id] = $info['label'];
145           }
146           $form['pattern_container']['bundles'] = array(
147             '#title' => $entity_type->getBundleLabel(),
148             '#type' => 'checkboxes',
149             '#options' => $bundle_options,
150             '#default_value' => $default_bundles,
151             '#description' => $this->t('Check to which types this pattern should be applied. Leave empty to allow any.'),
152           );
153         }
154
155         if ($this->languageManager->isMultilingual() && $entity_type->isTranslatable()) {
156           $language_options = [];
157           foreach ($this->languageManager->getLanguages() as $id => $language) {
158             $language_options[$id] = $language->getName();
159           }
160           $form['pattern_container']['languages'] = array(
161             '#title' => $this->t('Languages'),
162             '#type' => 'checkboxes',
163             '#options' => $language_options,
164             '#default_value' => $default_languages,
165             '#description' => $this->t('Check to which languages this pattern should be applied. Leave empty to allow any.'),
166           );
167         }
168       }
169     }
170
171     $form['label'] = array(
172       '#type' => 'textfield',
173       '#title' => $this->t('Label'),
174       '#maxlength' => 255,
175       '#default_value' => $this->entity->label(),
176       '#required' => TRUE,
177       '#description' => $this->t('A short name to help you identify this pattern in the patterns list.'),
178     );
179
180     $form['id'] = array(
181       '#type' => 'machine_name',
182       '#title' => $this->t('ID'),
183       '#maxlength' => 255,
184       '#default_value' => $this->entity->id(),
185       '#required' => TRUE,
186       '#disabled' => !$this->entity->isNew(),
187       '#machine_name' => array(
188         'exists' => 'Drupal\pathauto\Entity\PathautoPattern::load',
189       ),
190     );
191
192     $form['status'] = [
193       '#title' => $this->t('Enabled'),
194       '#type' => 'checkbox',
195       '#default_value' => $this->entity->status(),
196     ];
197
198     return parent::buildForm($form, $form_state);
199   }
200
201   /**
202    * {@inheritDoc}
203    */
204   public function buildEntity(array $form, FormStateInterface $form_state) {
205     /** @var \Drupal\pathauto\PathautoPatternInterface $entity */
206     $entity = parent::buildEntity($form, $form_state);
207
208     // Will only be used for new patterns.
209     $default_weight = 0;
210
211     $alias_type = $entity->getAliasType();
212     if ($alias_type->getDerivativeId() && $this->entityTypeManager->hasDefinition($alias_type->getDerivativeId())) {
213       $entity_type = $alias_type->getDerivativeId();
214       // First, remove bundle and language conditions.
215       foreach ($entity->getSelectionConditions() as $condition_id => $condition) {
216         if (in_array($condition->getPluginId(), ['entity_bundle:' . $entity_type, 'node_type', 'language'])) {
217           $entity->removeSelectionCondition($condition_id);
218         }
219       }
220
221       if ($bundles = array_filter((array) $form_state->getValue('bundles'))) {
222         $default_weight -= 5;
223         $plugin_id = $entity_type == 'node' ? 'node_type' : 'entity_bundle:' . $entity_type;
224         $entity->addSelectionCondition(
225           [
226             'id' => $plugin_id,
227             'bundles' => $bundles,
228             'negate' => FALSE,
229             'context_mapping' => [
230               $entity_type => $entity_type,
231             ]
232           ]
233         );
234       }
235
236       if ($languages = array_filter((array) $form_state->getValue('languages'))) {
237         $default_weight -= 5;
238         $language_mapping = $entity_type . ':' . $this->entityTypeManager->getDefinition($entity_type)->getKey('langcode') . ':language';
239         $entity->addSelectionCondition(
240           [
241             'id' => 'language',
242             'langcodes' => array_combine($languages, $languages),
243             'negate' => FALSE,
244             'context_mapping' => [
245               'language' => $language_mapping,
246             ]
247           ]
248         );
249         $entity->addRelationship($language_mapping, t('Language'));
250       }
251
252     }
253
254     if ($entity->isNew()) {
255       $entity->setWeight($default_weight);
256     }
257
258     return $entity;
259   }
260
261   /**
262    * {@inheritDoc}
263    */
264   public function save(array $form, FormStateInterface $form_state) {
265     parent::save($form, $form_state);
266     drupal_set_message($this->t('Pattern @label saved.', ['@label' => $this->entity->label()]));
267     $form_state->setRedirectUrl($this->entity->toUrl('collection'));
268   }
269
270   /**
271    * Handles switching the type selector.
272    */
273   public function ajaxReplacePatternForm($form, FormStateInterface $form_state) {
274     return $form['pattern_container'];
275   }
276
277   /**
278    * Handles submit call when alias type is selected.
279    */
280   public function submitSelectType(array $form, FormStateInterface $form_state) {
281     $this->entity = $this->buildEntity($form, $form_state);
282     $form_state->setRebuild();
283   }
284
285 }