Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / image / src / Form / ImageStyleEditForm.php
1 <?php
2
3 namespace Drupal\image\Form;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Url;
9 use Drupal\image\ConfigurableImageEffectInterface;
10 use Drupal\image\ImageEffectManager;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Controller for image style edit form.
15  */
16 class ImageStyleEditForm extends ImageStyleFormBase {
17
18   /**
19    * The image effect manager service.
20    *
21    * @var \Drupal\image\ImageEffectManager
22    */
23   protected $imageEffectManager;
24
25   /**
26    * Constructs an ImageStyleEditForm object.
27    *
28    * @param \Drupal\Core\Entity\EntityStorageInterface $image_style_storage
29    *   The storage.
30    * @param \Drupal\image\ImageEffectManager $image_effect_manager
31    *   The image effect manager service.
32    */
33   public function __construct(EntityStorageInterface $image_style_storage, ImageEffectManager $image_effect_manager) {
34     parent::__construct($image_style_storage);
35     $this->imageEffectManager = $image_effect_manager;
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public static function create(ContainerInterface $container) {
42     return new static(
43       $container->get('entity.manager')->getStorage('image_style'),
44       $container->get('plugin.manager.image.effect')
45     );
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function form(array $form, FormStateInterface $form_state) {
52     $user_input = $form_state->getUserInput();
53     $form['#title'] = $this->t('Edit style %name', ['%name' => $this->entity->label()]);
54     $form['#tree'] = TRUE;
55     $form['#attached']['library'][] = 'image/admin';
56
57     // Show the thumbnail preview.
58     $preview_arguments = ['#theme' => 'image_style_preview', '#style' => $this->entity];
59     $form['preview'] = [
60       '#type' => 'item',
61       '#title' => $this->t('Preview'),
62       '#markup' => \Drupal::service('renderer')->render($preview_arguments),
63       // Render preview above parent elements.
64       '#weight' => -5,
65     ];
66
67     // Build the list of existing image effects for this image style.
68     $form['effects'] = [
69       '#type' => 'table',
70       '#header' => [
71         $this->t('Effect'),
72         $this->t('Weight'),
73         $this->t('Operations'),
74       ],
75       '#tabledrag' => [
76         [
77           'action' => 'order',
78           'relationship' => 'sibling',
79           'group' => 'image-effect-order-weight',
80         ],
81       ],
82       '#attributes' => [
83         'id' => 'image-style-effects',
84       ],
85       '#empty' => t('There are currently no effects in this style. Add one by selecting an option below.'),
86       // Render effects below parent elements.
87       '#weight' => 5,
88     ];
89     foreach ($this->entity->getEffects() as $effect) {
90       $key = $effect->getUuid();
91       $form['effects'][$key]['#attributes']['class'][] = 'draggable';
92       $form['effects'][$key]['#weight'] = isset($user_input['effects']) ? $user_input['effects'][$key]['weight'] : NULL;
93       $form['effects'][$key]['effect'] = [
94         '#tree' => FALSE,
95         'data' => [
96           'label' => [
97             '#plain_text' => $effect->label(),
98           ],
99         ],
100       ];
101
102       $summary = $effect->getSummary();
103
104       if (!empty($summary)) {
105         $summary['#prefix'] = ' ';
106         $form['effects'][$key]['effect']['data']['summary'] = $summary;
107       }
108
109       $form['effects'][$key]['weight'] = [
110         '#type' => 'weight',
111         '#title' => $this->t('Weight for @title', ['@title' => $effect->label()]),
112         '#title_display' => 'invisible',
113         '#default_value' => $effect->getWeight(),
114         '#attributes' => [
115           'class' => ['image-effect-order-weight'],
116         ],
117       ];
118
119       $links = [];
120       $is_configurable = $effect instanceof ConfigurableImageEffectInterface;
121       if ($is_configurable) {
122         $links['edit'] = [
123           'title' => $this->t('Edit'),
124           'url' => Url::fromRoute('image.effect_edit_form', [
125             'image_style' => $this->entity->id(),
126             'image_effect' => $key,
127           ]),
128         ];
129       }
130       $links['delete'] = [
131         'title' => $this->t('Delete'),
132         'url' => Url::fromRoute('image.effect_delete', [
133           'image_style' => $this->entity->id(),
134           'image_effect' => $key,
135         ]),
136       ];
137       $form['effects'][$key]['operations'] = [
138         '#type' => 'operations',
139         '#links' => $links,
140       ];
141     }
142
143     // Build the new image effect addition form and add it to the effect list.
144     $new_effect_options = [];
145     $effects = $this->imageEffectManager->getDefinitions();
146     uasort($effects, function ($a, $b) {
147       return Unicode::strcasecmp($a['label'], $b['label']);
148     });
149     foreach ($effects as $effect => $definition) {
150       $new_effect_options[$effect] = $definition['label'];
151     }
152     $form['effects']['new'] = [
153       '#tree' => FALSE,
154       '#weight' => isset($user_input['weight']) ? $user_input['weight'] : NULL,
155       '#attributes' => ['class' => ['draggable']],
156     ];
157     $form['effects']['new']['effect'] = [
158       'data' => [
159         'new' => [
160           '#type' => 'select',
161           '#title' => $this->t('Effect'),
162           '#title_display' => 'invisible',
163           '#options' => $new_effect_options,
164           '#empty_option' => $this->t('Select a new effect'),
165         ],
166         [
167           'add' => [
168             '#type' => 'submit',
169             '#value' => $this->t('Add'),
170             '#validate' => ['::effectValidate'],
171             '#submit' => ['::submitForm', '::effectSave'],
172           ],
173         ],
174       ],
175       '#prefix' => '<div class="image-style-new">',
176       '#suffix' => '</div>',
177     ];
178
179     $form['effects']['new']['weight'] = [
180       '#type' => 'weight',
181       '#title' => $this->t('Weight for new effect'),
182       '#title_display' => 'invisible',
183       '#default_value' => count($this->entity->getEffects()) + 1,
184       '#attributes' => ['class' => ['image-effect-order-weight']],
185     ];
186     $form['effects']['new']['operations'] = [
187       'data' => [],
188     ];
189
190     return parent::form($form, $form_state);
191   }
192
193   /**
194    * Validate handler for image effect.
195    */
196   public function effectValidate($form, FormStateInterface $form_state) {
197     if (!$form_state->getValue('new')) {
198       $form_state->setErrorByName('new', $this->t('Select an effect to add.'));
199     }
200   }
201
202   /**
203    * Submit handler for image effect.
204    */
205   public function effectSave($form, FormStateInterface $form_state) {
206     $this->save($form, $form_state);
207
208     // Check if this field has any configuration options.
209     $effect = $this->imageEffectManager->getDefinition($form_state->getValue('new'));
210
211     // Load the configuration form for this option.
212     if (is_subclass_of($effect['class'], '\Drupal\image\ConfigurableImageEffectInterface')) {
213       $form_state->setRedirect(
214         'image.effect_add_form',
215         [
216           'image_style' => $this->entity->id(),
217           'image_effect' => $form_state->getValue('new'),
218         ],
219         ['query' => ['weight' => $form_state->getValue('weight')]]
220       );
221     }
222     // If there's no form, immediately add the image effect.
223     else {
224       $effect = [
225         'id' => $effect['id'],
226         'data' => [],
227         'weight' => $form_state->getValue('weight'),
228       ];
229       $effect_id = $this->entity->addImageEffect($effect);
230       $this->entity->save();
231       if (!empty($effect_id)) {
232         drupal_set_message($this->t('The image effect was successfully applied.'));
233       }
234     }
235   }
236
237   /**
238    * {@inheritdoc}
239    */
240   public function submitForm(array &$form, FormStateInterface $form_state) {
241
242     // Update image effect weights.
243     if (!$form_state->isValueEmpty('effects')) {
244       $this->updateEffectWeights($form_state->getValue('effects'));
245     }
246
247     parent::submitForm($form, $form_state);
248   }
249
250   /**
251    * {@inheritdoc}
252    */
253   public function save(array $form, FormStateInterface $form_state) {
254     parent::save($form, $form_state);
255     drupal_set_message($this->t('Changes to the style have been saved.'));
256   }
257
258   /**
259    * {@inheritdoc}
260    */
261   public function actions(array $form, FormStateInterface $form_state) {
262     $actions = parent::actions($form, $form_state);
263     $actions['submit']['#value'] = $this->t('Update style');
264
265     return $actions;
266   }
267
268   /**
269    * Updates image effect weights.
270    *
271    * @param array $effects
272    *   Associative array with effects having effect uuid as keys and array
273    *   with effect data as values.
274    */
275   protected function updateEffectWeights(array $effects) {
276     foreach ($effects as $uuid => $effect_data) {
277       if ($this->entity->getEffects()->has($uuid)) {
278         $this->entity->getEffect($uuid)->setWeight($effect_data['weight']);
279       }
280     }
281   }
282
283 }