Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / image / src / Form / ImageEffectFormBase.php
1 <?php
2
3 namespace Drupal\image\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Form\SubformState;
8 use Drupal\image\ConfigurableImageEffectInterface;
9 use Drupal\image\ImageStyleInterface;
10 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
11 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12
13 /**
14  * Provides a base form for image effects.
15  */
16 abstract class ImageEffectFormBase extends FormBase {
17
18   /**
19    * The image style.
20    *
21    * @var \Drupal\image\ImageStyleInterface
22    */
23   protected $imageStyle;
24
25   /**
26    * The image effect.
27    *
28    * @var \Drupal\image\ImageEffectInterface|\Drupal\image\ConfigurableImageEffectInterface
29    */
30   protected $imageEffect;
31
32   /**
33    * {@inheritdoc}
34    */
35   public function getFormId() {
36     return 'image_effect_form';
37   }
38
39   /**
40    * {@inheritdoc}
41    *
42    * @param \Drupal\image\ImageStyleInterface $image_style
43    *   The image style.
44    * @param string $image_effect
45    *   The image effect ID.
46    *
47    * @return array
48    *   The form structure.
49    *
50    * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
51    */
52   public function buildForm(array $form, FormStateInterface $form_state, ImageStyleInterface $image_style = NULL, $image_effect = NULL) {
53     $this->imageStyle = $image_style;
54     try {
55       $this->imageEffect = $this->prepareImageEffect($image_effect);
56     }
57     catch (PluginNotFoundException $e) {
58       throw new NotFoundHttpException("Invalid effect id: '$image_effect'.");
59     }
60     $request = $this->getRequest();
61
62     if (!($this->imageEffect instanceof ConfigurableImageEffectInterface)) {
63       throw new NotFoundHttpException();
64     }
65
66     $form['#attached']['library'][] = 'image/admin';
67     $form['uuid'] = [
68       '#type' => 'value',
69       '#value' => $this->imageEffect->getUuid(),
70     ];
71     $form['id'] = [
72       '#type' => 'value',
73       '#value' => $this->imageEffect->getPluginId(),
74     ];
75
76     $form['data'] = [];
77     $subform_state = SubformState::createForSubform($form['data'], $form, $form_state);
78     $form['data'] = $this->imageEffect->buildConfigurationForm($form['data'], $subform_state);
79     $form['data']['#tree'] = TRUE;
80
81     // Check the URL for a weight, then the image effect, otherwise use default.
82     $form['weight'] = [
83       '#type' => 'hidden',
84       '#value' => $request->query->has('weight') ? (int) $request->query->get('weight') : $this->imageEffect->getWeight(),
85     ];
86
87     $form['actions'] = ['#type' => 'actions'];
88     $form['actions']['submit'] = [
89       '#type' => 'submit',
90       '#button_type' => 'primary',
91     ];
92     $form['actions']['cancel'] = [
93       '#type' => 'link',
94       '#title' => $this->t('Cancel'),
95       '#url' => $this->imageStyle->urlInfo('edit-form'),
96       '#attributes' => ['class' => ['button']],
97     ];
98     return $form;
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function validateForm(array &$form, FormStateInterface $form_state) {
105     // The image effect configuration is stored in the 'data' key in the form,
106     // pass that through for validation.
107     $this->imageEffect->validateConfigurationForm($form['data'], SubformState::createForSubform($form['data'], $form, $form_state));
108   }
109
110   /**
111    * {@inheritdoc}
112    */
113   public function submitForm(array &$form, FormStateInterface $form_state) {
114     $form_state->cleanValues();
115
116     // The image effect configuration is stored in the 'data' key in the form,
117     // pass that through for submission.
118     $this->imageEffect->submitConfigurationForm($form['data'], SubformState::createForSubform($form['data'], $form, $form_state));
119
120     $this->imageEffect->setWeight($form_state->getValue('weight'));
121     if (!$this->imageEffect->getUuid()) {
122       $this->imageStyle->addImageEffect($this->imageEffect->getConfiguration());
123     }
124     $this->imageStyle->save();
125
126     drupal_set_message($this->t('The image effect was successfully applied.'));
127     $form_state->setRedirectUrl($this->imageStyle->urlInfo('edit-form'));
128   }
129
130   /**
131    * Converts an image effect ID into an object.
132    *
133    * @param string $image_effect
134    *   The image effect ID.
135    *
136    * @return \Drupal\image\ImageEffectInterface
137    *   The image effect object.
138    */
139   abstract protected function prepareImageEffect($image_effect);
140
141 }