Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / metatag / src / Form / MetatagSettingsForm.php
1 <?php
2
3 namespace Drupal\metatag\Form;
4
5 use Drupal\Core\Form\ConfigFormBase;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Defines the configuration export form.
10  */
11 class MetatagSettingsForm extends ConfigFormBase {
12
13   /**
14    * {@inheritdoc}
15    */
16   public function getFormId() {
17     return 'metatag_admin_settings';
18   }
19
20   /**
21    * {@inheritdoc}
22    */
23   protected function getEditableConfigNames() {
24     return ['metatag.settings'];
25   }
26
27   /**
28    * {@inheritdoc}
29    */
30   public function buildForm(array $form, FormStateInterface $form_state) {
31     $settings = $this->config('metatag.settings')->get('entity_type_groups');
32
33     $form['entity_type_groups'] = [
34       '#type' => 'details',
35       '#open' => TRUE,
36       '#title' => $this->t('Entity type / Group Mapping'),
37       '#description' => $this->t('Identify which metatag groups should be available on which entity type / bundle combination. Unselected groups will not appear on the configuration form for that entity type, reducing the size of the form and increasing performance. If no groups are selected for a type, all groups will appear.'),
38       '#tree' => TRUE,
39     ];
40
41     $metatag_manager = \Drupal::service('metatag.manager');
42     $bundle_manager = \Drupal::service('entity_type.bundle.info');
43     $metatag_groups = $metatag_manager->sortedGroups();
44     $entity_types = MetatagDefaultsForm::getSupportedEntityTypes();
45     foreach ($entity_types as $entity_type => $entity_label) {
46       $bundles = $bundle_manager->getBundleInfo($entity_type);
47       foreach ($bundles as $bundle_id => $bundle_info) {
48         // Create an option list for each bundle.
49         $options = [];
50         foreach ($metatag_groups as $group_name => $group_info) {
51           $options[$group_name] = $group_info['label'];
52         }
53         // Format a collapsible fieldset for each group for easier readability.
54         $form['entity_type_groups'][$entity_type][$bundle_id] = [
55           '#type' => 'details',
56           '#title' => $entity_label . ': ' . $bundle_info['label'],
57         ];
58         $form['entity_type_groups'][$entity_type][$bundle_id][] = [
59           '#type' => 'checkboxes',
60           '#options' => $options,
61           '#default_value' => isset($settings[$entity_type]) && isset($settings[$entity_type][$bundle_id]) ? $settings[$entity_type][$bundle_id] : [],
62         ];
63       }
64     }
65
66     return parent::buildForm($form, $form_state);
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function submitForm(array &$form, FormStateInterface $form_state) {
73     $settings = $this->config('metatag.settings');
74     $value = $form_state->getValue('entity_type_groups');
75     $value = static::arrayFilterRecursive($value);
76     // Remove the extra layer created by collapsible fieldsets.
77     foreach ($value as $entity_type => $bundle) {
78       foreach ($bundle as $bundle_id => $groups) {
79         $value[$entity_type][$bundle_id] = $groups[0];
80       }
81     }
82     $settings->set('entity_type_groups', $value)->save();
83     parent::submitForm($form, $form_state);
84   }
85
86   /**
87    * Recursively filter results.
88    *
89    * @param array $input
90    *   The array to filter.
91    *
92    * @return array
93    *   The filtered array.
94    */
95   public static function arrayFilterRecursive(array $input) {
96     foreach ($input as &$value) {
97       if (is_array($value)) {
98         $value = static::arrayFilterRecursive($value);
99       }
100     }
101     return array_filter($input);
102   }
103
104 }