3 namespace Drupal\metatag\Form;
5 use Drupal\Core\Entity\ContentEntityType;
6 use Drupal\Core\Entity\EntityForm;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\metatag\MetatagManager;
10 use Drupal\page_manager\Entity\PageVariant;
13 * Class MetatagDefaultsForm.
15 * @package Drupal\metatag\Form
17 class MetatagDefaultsForm extends EntityForm {
22 public function form(array $form, FormStateInterface $form_state) {
23 $form = parent::form($form, $form_state);
24 $metatag_defaults = $this->entity;
25 $metatag_manager = \Drupal::service('metatag.manager');
27 $form['#ajax_wrapper_id'] = 'metatag-defaults-form-ajax-wrapper';
29 'wrapper' => $form['#ajax_wrapper_id'],
30 'callback' => '::rebuildForm',
32 $form['#prefix'] = '<div id="' . $form['#ajax_wrapper_id'] . '">';
33 $form['#suffix'] = '</div>';
36 if (!empty($metatag_defaults)) {
37 $default_type = $metatag_defaults->getOriginalId();
40 $form_state->set('default_type', $default_type);
43 $token_types = empty($default_type) ? [] : [explode('__', $default_type)[0]];
45 // Add the token browser at the top.
46 $form += \Drupal::service('metatag.token')->tokenBrowser($token_types);
48 // If this is a new Metatag defaults, then list available bundles.
49 if ($metatag_defaults->isNew()) {
50 $options = $this->getAvailableBundles();
53 '#title' => $this->t('Type'),
54 '#description' => $this->t('Select the type of default meta tags you would like to add.'),
55 '#options' => $options,
57 '#default_value' => $default_type,
60 'name' => 'select_id_submit',
64 $form['select_id_submit'] = [
66 '#value' => $this->t('Submit'),
67 '#name' => 'select_id_submit',
70 'class' => ['js-hide'],
76 $values = $metatag_defaults->get('tags');
79 // Retrieve configuration settings.
80 $settings = $this->config('metatag.settings');
81 $entity_type_groups = $settings->get('entity_type_groups');
83 // Find the current entity type and bundle.
84 $metatag_defaults_id = $metatag_defaults->id();
85 $type_parts = explode('__', $metatag_defaults_id);
86 $entity_type = $type_parts[0];
87 $entity_bundle = isset($type_parts[1]) ? $type_parts[1] : NULL;
89 // See if there are requested groups for this entity type and bundle.
90 $groups = !empty($entity_type_groups[$entity_type]) && !empty($entity_type_groups[$entity_type][$entity_bundle]) ? $entity_type_groups[$entity_type][$entity_bundle] : [];
91 // Limit the form to requested groups, if any.
92 if (!empty($groups)) {
93 $form = $metatag_manager->form($values, $form, [$entity_type], $groups);
95 // Otherwise, display all groups.
97 $form = $metatag_manager->form($values, $form);
104 * Ajax form submit handler that will return the whole rebuilt form.
107 * An associative array containing the structure of the form.
108 * @param \Drupal\Core\Form\FormStateInterface $form_state
109 * The current state of the form.
112 * The form structure.
114 public function rebuildForm(array &$form, FormStateInterface $form_state) {
121 protected function actions(array $form, FormStateInterface $form_state) {
122 $actions = parent::actions($form, $form_state);
123 if (isset($actions['delete'])) {
124 $actions['delete']['#access'] = $actions['delete']['#access'] && !in_array($this->entity->id(), MetatagManager::protectedDefaults());
132 public function submitForm(array &$form, FormStateInterface $form_state) {
133 if ($form_state->getTriggeringElement()['#name'] == 'select_id_submit') {
134 $form_state->set('default_type', $form_state->getValue('id'));
135 $form_state->setRebuild();
138 parent::submitForm($form, $form_state);
145 public function save(array $form, FormStateInterface $form_state) {
146 $metatag_defaults = $this->entity;
148 // Set the label on new defaults.
149 if ($metatag_defaults->isNew()) {
150 $metatag_defaults_id = $form_state->getValue('id');
152 $type_parts = explode('__', $metatag_defaults_id);
153 $entity_type = $type_parts[0];
154 $entity_bundle = isset($type_parts[1]) ? $type_parts[1] : NULL;
156 // Get the entity label.
157 $entity_manager = \Drupal::service('entity_type.manager');
158 $entity_info = $entity_manager->getDefinitions();
159 $entity_label = (string) $entity_info[$entity_type]->get('label');
161 if (!is_null($entity_bundle)) {
162 // Get the bundle label.
163 $bundle_manager = \Drupal::service('entity_type.bundle.info');
164 $bundle_info = $bundle_manager->getBundleInfo($entity_type);
165 if ($entity_type === 'page_variant') {
166 // Check if page manager is enabled and try to load the page variant
167 // so the label of the variant can be used.
168 $moduleHandler = \Drupal::service('module_handler');
169 if ($moduleHandler->moduleExists('metatag_page_manager')) {
170 $page_variant = PageVariant::load($entity_bundle);
171 $page = $page_variant->getPage();
173 $entity_label .= ': ' . $page->label() . ': ' . $page_variant->label();
178 $entity_label .= ': ' . $bundle_info[$entity_bundle]['label'];
182 // Set the label to the config entity.
183 $this->entity->set('label', $entity_label);
186 // Set tags within the Metatag entity.
187 $tag_manager = \Drupal::service('plugin.manager.metatag.tag');
188 $tags = $tag_manager->getDefinitions();
190 foreach ($tags as $tag_id => $tag_definition) {
191 if ($form_state->hasValue($tag_id)) {
192 // Some plugins need to process form input before storing it. Hence, we
193 // set it and then get it.
194 $tag = $tag_manager->createInstance($tag_id);
195 $tag->setValue($form_state->getValue($tag_id));
196 if (!empty($tag->value())) {
197 $tag_values[$tag_id] = $tag->value();
201 $metatag_defaults->set('tags', $tag_values);
202 $status = $metatag_defaults->save();
206 drupal_set_message($this->t('Created the %label Metatag defaults.', [
207 '%label' => $metatag_defaults->label(),
212 drupal_set_message($this->t('Saved the %label Metatag defaults.', [
213 '%label' => $metatag_defaults->label(),
217 $form_state->setRedirectUrl($metatag_defaults->toUrl('collection'));
221 * Returns an array of available bundles to override.
224 * A list of available bundles as $id => $label.
226 protected function getAvailableBundles() {
228 $entity_types = static::getSupportedEntityTypes();
229 /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager */
230 $entity_manager = \Drupal::service('entity_type.manager');
231 /** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info */
232 $bundle_info = \Drupal::service('entity_type.bundle.info');
233 $metatags_defaults_manager = $entity_manager->getStorage('metatag_defaults');
234 foreach ($entity_types as $entity_type => $entity_label) {
235 if (empty($metatags_defaults_manager->load($entity_type))) {
236 $options[$entity_label][$entity_type] = "$entity_label (Default)";
239 $bundles = $bundle_info->getBundleInfo($entity_type);
240 foreach ($bundles as $bundle_id => $bundle_metadata) {
241 $metatag_defaults_id = $entity_type . '__' . $bundle_id;
243 if (empty($metatags_defaults_manager->load($metatag_defaults_id))) {
244 $options[$entity_label][$metatag_defaults_id] = $bundle_metadata['label'];
252 * Returns a list of supported entity types.
255 * A list of available entity types as $machine_name => $label.
257 public static function getSupportedEntityTypes() {
260 /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager */
261 $entity_manager = \Drupal::service('entity_type.manager');
263 // A list of entity types that are not supported.
264 $unsupported_types = [
269 // Contact messages are the messages submitted on individual contact forms
270 // so obviously shouldn't get meta tags.
278 // Make a list of supported content types.
279 foreach ($entity_manager->getDefinitions() as $entity_name => $definition) {
280 // Skip some entity types that we don't want to support.
281 if (in_array($entity_name, $unsupported_types)) {
285 // Identify supported entities.
286 if ($definition instanceof ContentEntityType) {
287 // Only work with entity types that have a list of links, i.e. publicly
289 $links = $definition->get('links');
290 if (!empty($links)) {
291 $entity_types[$entity_name] = static::getEntityTypeLabel($definition);
296 return $entity_types;
300 * Returns the text label for the entity type specified.
302 * @param \Drupal\Core\Entity\EntityTypeInterface $entityType
303 * The entity type to process.
308 public static function getEntityTypeLabel(EntityTypeInterface $entityType) {
309 $label = $entityType->getLabel();
311 if (is_a($label, 'Drupal\Core\StringTranslation\TranslatableMarkup')) {
312 /** @var \Drupal\Core\StringTranslation\TranslatableMarkup $label */
313 $label = $label->render();