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