3 namespace Drupal\metatag\Form;
5 use Drupal\Core\Entity\ContentEntityType;
6 use Drupal\Core\Entity\EntityForm;
7 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\Core\Entity\EntityTypeManagerInterface;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\StringTranslation\TranslatableMarkup;
14 * Class MetatagDefaultsForm.
16 * @package Drupal\metatag\Form
18 class MetatagDefaultsForm extends EntityForm {
23 public function form(array $form, FormStateInterface $form_state) {
24 $form = parent::form($form, $form_state);
25 $metatag_defaults = $this->entity;
26 $metatag_manager = \Drupal::service('metatag.manager');
28 $form['#ajax_wrapper_id'] = 'metatag-defaults-form-ajax-wrapper';
30 'wrapper' => $form['#ajax_wrapper_id'],
31 'callback' => '::rebuildForm'
33 $form['#prefix'] = '<div id="' . $form['#ajax_wrapper_id'] . '">';
34 $form['#suffix'] = '</div>';
37 if (!empty($metatag_defaults)) {
38 $default_type = $metatag_defaults->getOriginalId();
41 $form_state->set('default_type', $default_type);
44 $token_types = empty($default_type) ? [] : [explode('__', $default_type)[0]];
46 // Add the token browser at the top.
47 $form += \Drupal::service('metatag.token')->tokenBrowser($token_types);
49 // If this is a new Metatag defaults, then list available bundles.
50 if ($metatag_defaults->isNew()) {
51 $options = $this->getAvailableBundles();
54 '#title' => t('Type'),
55 '#description' => t('Select the type of default meta tags you would like to add.'),
56 '#options' => $options,
58 '#default_value' => $default_type,
59 '#ajax' => $ajax + ['trigger_as' => ['name' => 'select_id_submit']]
61 $form['select_id_submit'] = [
63 '#value' => $this->t('Submit'),
64 '#name' => 'select_id_submit',
67 'class' => ['js-hide']
73 $values = $metatag_defaults->get('tags');
76 // Add metatag form fields.
77 $form = $metatag_manager->form($values, $form);
83 * Ajax form submit handler that will return the whole rebuilt form.
86 * An associative array containing the structure of the form.
87 * @param \Drupal\Core\Form\FormStateInterface $form_state
88 * The current state of the form.
93 public function rebuildForm(array &$form, FormStateInterface $form_state) {
100 public function submitForm(array &$form, FormStateInterface $form_state) {
101 if ($form_state->getTriggeringElement()['#name'] == 'select_id_submit') {
102 $form_state->set('default_type', $form_state->getValue('id'));
103 $form_state->setRebuild();
106 parent::submitForm($form, $form_state);
113 public function save(array $form, FormStateInterface $form_state) {
114 $metatag_defaults = $this->entity;
116 // Set the label on new defaults.
117 if ($metatag_defaults->isNew()) {
118 $metatag_defaults_id = $form_state->getValue('id');
120 $type_parts = explode('__', $metatag_defaults_id);
121 $entity_type = $type_parts[0];
122 $entity_bundle = isset($type_parts[1]) ? $type_parts[1] : NULL;
124 // Get the entity label.
125 $entity_manager = \Drupal::service('entity_type.manager');
126 $entity_info = $entity_manager->getDefinitions();
127 $entity_label = (string) $entity_info[$entity_type]->get('label');
129 if (!is_null($entity_bundle)) {
130 // Get the bundle label.
131 $bundle_manager = \Drupal::service('entity_type.bundle.info');
132 $bundle_info = $bundle_manager->getBundleInfo($entity_type);
133 $entity_label .= ': ' . $bundle_info[$entity_bundle]['label'];
136 // Set the label to the config entity.
137 $this->entity->set('label', $entity_label);
140 // Set tags within the Metatag entity.
141 $tag_manager = \Drupal::service('plugin.manager.metatag.tag');
142 $tags = $tag_manager->getDefinitions();
144 foreach ($tags as $tag_id => $tag_definition) {
145 if ($form_state->hasValue($tag_id)) {
146 // Some plugins need to process form input before storing it.
147 // Hence, we set it and then get it.
148 $tag = $tag_manager->createInstance($tag_id);
149 $tag->setValue($form_state->getValue($tag_id));
150 if (!empty($tag->value())) {
151 $tag_values[$tag_id] = $tag->value();
155 $metatag_defaults->set('tags', $tag_values);
156 $status = $metatag_defaults->save();
160 drupal_set_message($this->t('Created the %label Metatag defaults.', [
161 '%label' => $metatag_defaults->label(),
165 drupal_set_message($this->t('Saved the %label Metatag defaults.', [
166 '%label' => $metatag_defaults->label(),
170 $form_state->setRedirectUrl($metatag_defaults->toUrl('collection'));
174 * Returns an array of available bundles to override.
177 * A list of available bundles as $id => $label.
179 protected function getAvailableBundles() {
181 $entity_types = $this->getSupportedEntityTypes();
182 /** @var EntityTypeManagerInterface $entity_manager */
183 $entity_manager = \Drupal::service('entity_type.manager');
184 /** @var EntityTypeBundleInfoInterface $bundle_info */
185 $bundle_info = \Drupal::service('entity_type.bundle.info');
186 $metatags_defaults_manager = $entity_manager->getStorage('metatag_defaults');
187 foreach ($entity_types as $entity_type => $entity_label) {
188 if (empty($metatags_defaults_manager->load($entity_type))) {
189 $options[$entity_label][$entity_type] = "$entity_label (Default)";
192 $bundles = $bundle_info->getBundleInfo($entity_type);
193 foreach ($bundles as $bundle_id => $bundle_metadata) {
194 $metatag_defaults_id = $entity_type . '__' . $bundle_id;
196 if (empty($metatags_defaults_manager->load($metatag_defaults_id))) {
197 $options[$entity_label][$metatag_defaults_id] = $bundle_metadata['label'];
205 * Returns a list of supported entity types.
208 * A list of available entity types as $machine_name => $label.
210 protected function getSupportedEntityTypes() {
213 /** @var EntityTypeManagerInterface $entity_manager */
214 $entity_manager = \Drupal::service('entity_type.manager');
216 // A list of entity types that are not supported.
217 $unsupported_types = [
222 // Contact messages are the messages submitted on individual contact forms
223 // so obviously shouldn't get meta tags.
231 // Make a list of supported content types.
232 foreach ($entity_manager->getDefinitions() as $entity_name => $definition) {
233 // Skip some entity types that we don't want to support.
234 if (in_array($entity_name, $unsupported_types)) {
238 // Identify supported entities.
239 if ($definition instanceof ContentEntityType) {
240 // Only work with entity types that have a list of links, i.e. publicly
242 $links = $definition->get('links');
243 if (!empty($links)) {
244 $entity_types[$entity_name] = $this->getEntityTypeLabel($definition);
249 return $entity_types;
253 * Returns the text label for the entity type specified.
255 * @param EntityTypeInterface $entityType
259 protected function getEntityTypeLabel(EntityTypeInterface $entityType) {
260 $label = $entityType->getLabel();
262 if (is_a($label, 'Drupal\Core\StringTranslation\TranslatableMarkup')) {
263 /** @var TranslatableMarkup $label */
264 $label = $label->render();