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 * @var \Drupal\pathauto\AliasTypeManager
24 * @var \Drupal\pathauto\PathautoPatternInterface
29 * The entity type bundle info service.
31 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
33 protected $entityTypeBundleInfo;
36 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
38 protected $entityTypeManager;
41 * @var \Drupal\Core\Language\LanguageManagerInterface
43 protected $languageManager;
48 public static function create(ContainerInterface $container) {
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')
58 * PatternEditForm constructor.
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
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;
75 public function buildForm(array $form, FormStateInterface $form_state) {
78 foreach ($this->manager->getVisibleDefinitions() as $plugin_id => $plugin_definition) {
79 $options[$plugin_id] = $plugin_definition['label'];
83 '#title' => $this->t('Pattern type'),
84 '#default_value' => $this->entity->getType(),
85 '#options' => $options,
87 '#limit_validation_errors' => array(array('type')),
88 '#submit' => array('::submitSelectType'),
89 '#executes_submit_callback' => TRUE,
91 'callback' => '::ajaxReplacePatternForm',
92 'wrapper' => 'pathauto-pattern',
93 'method' => 'replace',
97 $form['pattern_container'] = [
98 '#type' => 'container',
99 '#prefix' => '<div id="pathauto-pattern">',
100 '#suffix' => '</div>',
103 // if there is no type yet, stop here.
104 if ($this->entity->getType()) {
106 $alias_type = $this->entity->getAliasType();
108 $form['pattern_container']['pattern'] = array(
109 '#type' => 'textfield',
110 '#title' => 'Path pattern',
111 '#default_value' => $this->entity->getPattern(),
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(),
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(),
127 // Expose bundle and language conditions.
128 if ($alias_type->getDerivativeId() && $entity_type = $this->entityTypeManager->getDefinition($alias_type->getDerivativeId())) {
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'];
136 elseif ($condition->getPluginId() == 'language') {
137 $default_languages = $condition->getConfiguration()['langcodes'];
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'];
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.'),
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();
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.'),
171 $form['label'] = array(
172 '#type' => 'textfield',
173 '#title' => $this->t('Label'),
175 '#default_value' => $this->entity->label(),
177 '#description' => $this->t('A short name to help you identify this pattern in the patterns list.'),
181 '#type' => 'machine_name',
182 '#title' => $this->t('ID'),
184 '#default_value' => $this->entity->id(),
186 '#disabled' => !$this->entity->isNew(),
187 '#machine_name' => array(
188 'exists' => 'Drupal\pathauto\Entity\PathautoPattern::load',
193 '#title' => $this->t('Enabled'),
194 '#type' => 'checkbox',
195 '#default_value' => $this->entity->status(),
198 return parent::buildForm($form, $form_state);
204 public function buildEntity(array $form, FormStateInterface $form_state) {
205 /** @var \Drupal\pathauto\PathautoPatternInterface $entity */
206 $entity = parent::buildEntity($form, $form_state);
208 // Will only be used for new patterns.
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);
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(
227 'bundles' => $bundles,
229 'context_mapping' => [
230 $entity_type => $entity_type,
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(
242 'langcodes' => array_combine($languages, $languages),
244 'context_mapping' => [
245 'language' => $language_mapping,
249 $entity->addRelationship($language_mapping, t('Language'));
254 if ($entity->isNew()) {
255 $entity->setWeight($default_weight);
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'));
271 * Handles switching the type selector.
273 public function ajaxReplacePatternForm($form, FormStateInterface $form_state) {
274 return $form['pattern_container'];
278 * Handles submit call when alias type is selected.
280 public function submitSelectType(array $form, FormStateInterface $form_state) {
281 $this->entity = $this->buildEntity($form, $form_state);
282 $form_state->setRebuild();