8684c773123fb3032a9776c049a1fe1a3fc2938f
[yaffs-website] / web / core / modules / system / src / Form / ImageToolkitForm.php
1 <?php
2
3 namespace Drupal\system\Form;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Form\ConfigFormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\ImageToolkit\ImageToolkitManager;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Configures image toolkit settings for this site.
13  *
14  * @internal
15  */
16 class ImageToolkitForm extends ConfigFormBase {
17
18   /**
19    * An array containing currently available toolkits.
20    *
21    * @var \Drupal\Core\ImageToolkit\ImageToolkitInterface[]
22    */
23   protected $availableToolkits = [];
24
25   /**
26    * Constructs a ImageToolkitForm object.
27    *
28    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
29    *   The factory for configuration objects.
30    * @param \Drupal\Core\ImageToolkit\ImageToolkitManager $manager
31    *   The image toolkit plugin manager.
32    */
33   public function __construct(ConfigFactoryInterface $config_factory, ImageToolkitManager $manager) {
34     parent::__construct($config_factory);
35
36     foreach ($manager->getAvailableToolkits() as $id => $definition) {
37       $this->availableToolkits[$id] = $manager->createInstance($id);
38     }
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public static function create(ContainerInterface $container) {
45     return new static(
46       $container->get('config.factory'),
47       $container->get('image.toolkit.manager')
48     );
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function getFormId() {
55     return 'system_image_toolkit_settings';
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   protected function getEditableConfigNames() {
62     return ['system.image'];
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function buildForm(array $form, FormStateInterface $form_state) {
69     $current_toolkit = $this->config('system.image')->get('toolkit');
70
71     $form['image_toolkit'] = [
72       '#type' => 'radios',
73       '#title' => $this->t('Select an image processing toolkit'),
74       '#default_value' => $current_toolkit,
75       '#options' => [],
76     ];
77
78     // If we have more than one image toolkit, allow the user to select the one
79     // to use, and load each of the toolkits' settings form.
80     foreach ($this->availableToolkits as $id => $toolkit) {
81       $definition = $toolkit->getPluginDefinition();
82       $form['image_toolkit']['#options'][$id] = $definition['title'];
83       $form['image_toolkit_settings'][$id] = [
84         '#type' => 'details',
85         '#title' => $this->t('@toolkit settings', ['@toolkit' => $definition['title']]),
86         '#open' => TRUE,
87         '#tree' => TRUE,
88         '#states' => [
89           'visible' => [
90             ':radio[name="image_toolkit"]' => ['value' => $id],
91           ],
92         ],
93       ];
94       $form['image_toolkit_settings'][$id] += $toolkit->buildConfigurationForm([], $form_state);
95     }
96
97     return parent::buildForm($form, $form_state);
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function validateForm(array &$form, FormStateInterface $form_state) {
104     parent::validateForm($form, $form_state);
105
106     // Call the form validation handler for each of the toolkits.
107     foreach ($this->availableToolkits as $toolkit) {
108       $toolkit->validateConfigurationForm($form, $form_state);
109     }
110   }
111
112   /**
113    * {@inheritdoc}
114    */
115   public function submitForm(array &$form, FormStateInterface $form_state) {
116     $this->config('system.image')
117       ->set('toolkit', $form_state->getValue('image_toolkit'))
118       ->save();
119
120     // Call the form submit handler for each of the toolkits.
121     foreach ($this->availableToolkits as $toolkit) {
122       $toolkit->submitConfigurationForm($form, $form_state);
123     }
124
125     parent::submitForm($form, $form_state);
126   }
127
128 }