Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / file_mdm / src / Form / SettingsForm.php
1 <?php
2
3 namespace Drupal\file_mdm\Form;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Form\ConfigFormBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\file_mdm\Plugin\FileMetadataPluginManager;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Configures file_mdm settings for this site.
14  */
15 class SettingsForm extends ConfigFormBase {
16
17   /**
18    * An array containing the available metadata plugins.
19    *
20    * @var \Drupal\file_mdm\Plugin\FileMetadataPluginInterface[]
21    */
22   protected $metadataPlugins = [];
23
24   /**
25    * Constructs a SettingsForm object.
26    *
27    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
28    *   The factory for configuration objects.
29    * @param \Drupal\file_mdm\Plugin\FileMetadataPluginManager $manager
30    *   The file metadata plugin manager.
31    */
32   public function __construct(ConfigFactoryInterface $config_factory, FileMetadataPluginManager $manager) {
33     parent::__construct($config_factory);
34     foreach ($manager->getDefinitions() as $id => $definition) {
35       $this->metadataPlugins[$id] = $manager->createInstance($id);
36     }
37     uasort($this->metadataPlugins, function ($a, $b) {
38       return Unicode::strcasecmp((string) $a->getPluginDefinition()['title'], (string) $b->getPluginDefinition()['title']);
39     });
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public static function create(ContainerInterface $container) {
46     return new static(
47       $container->get('config.factory'),
48       $container->get('plugin.manager.file_metadata')
49     );
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function getFormId() {
56     return 'file_mdm_settings';
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   protected function getEditableConfigNames() {
63     return ['file_mdm.settings'];
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function buildForm(array $form, FormStateInterface $form_state) {
70     // Cache metadata.
71     $form['metadata_cache'] = [
72       '#type' => 'details',
73       '#open' => TRUE,
74       '#collapsible' => FALSE,
75       '#title' => $this->t('Metadata caching'),
76       '#tree' => TRUE,
77     ];
78     $form['metadata_cache']['settings'] = [
79       '#type' => 'file_mdm_caching',
80       '#default_value' => $this->config('file_mdm.settings')->get('metadata_cache'),
81     ];
82
83     // Settings tabs.
84     $form['plugins'] = array(
85       '#type' => 'vertical_tabs',
86       '#tree' => FALSE,
87     );
88
89     // Load subforms from each plugin.
90     foreach ($this->metadataPlugins as $id => $plugin) {
91       $definition = $plugin->getPluginDefinition();
92       $form['file_mdm_plugin_settings'][$id] = array(
93         '#type' => 'details',
94         '#title' => $definition['title'],
95         '#description' => $definition['help'],
96         '#open' => FALSE,
97         '#tree' => TRUE,
98         '#group' => 'plugins',
99       );
100       $form['file_mdm_plugin_settings'][$id] += $plugin->buildConfigurationForm(array(), $form_state);
101     }
102
103     return parent::buildForm($form, $form_state);
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   public function validateForm(array &$form, FormStateInterface $form_state) {
110     parent::validateForm($form, $form_state);
111     // Call the form validation handler for each of the plugins.
112     foreach ($this->metadataPlugins as $plugin) {
113       $plugin->validateConfigurationForm($form, $form_state);
114     }
115   }
116
117   /**
118    * {@inheritdoc}
119    */
120   public function submitForm(array &$form, FormStateInterface $form_state) {
121     // Call the form submit handler for each of the plugins.
122     foreach ($this->metadataPlugins as $plugin) {
123       $plugin->submitConfigurationForm($form, $form_state);
124     }
125
126     $this->config('file_mdm.settings')->set('metadata_cache', $form_state->getValue(['metadata_cache', 'settings']));
127
128     // Only save settings if they have changed to prevent unnecessary cache
129     // invalidations.
130     if ($this->config('file_mdm.settings')->getOriginal() != $this->config('file_mdm.settings')->get()) {
131       $this->config('file_mdm.settings')->save();
132     }
133     parent::submitForm($form, $form_state);
134   }
135
136 }