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