3 namespace Drupal\image\Form;
5 use Drupal\Core\Entity\EntityStorageInterface;
6 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\image\ConfigurableImageEffectInterface;
9 use Drupal\image\ImageEffectManager;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Controller for image style edit form.
15 class ImageStyleEditForm extends ImageStyleFormBase {
18 * The image effect manager service.
20 * @var \Drupal\image\ImageEffectManager
22 protected $imageEffectManager;
25 * Constructs an ImageStyleEditForm object.
27 * @param \Drupal\Core\Entity\EntityStorageInterface $image_style_storage
29 * @param \Drupal\image\ImageEffectManager $image_effect_manager
30 * The image effect manager service.
32 public function __construct(EntityStorageInterface $image_style_storage, ImageEffectManager $image_effect_manager) {
33 parent::__construct($image_style_storage);
34 $this->imageEffectManager = $image_effect_manager;
40 public static function create(ContainerInterface $container) {
42 $container->get('entity.manager')->getStorage('image_style'),
43 $container->get('plugin.manager.image.effect')
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';
56 // Show the thumbnail preview.
57 $preview_arguments = ['#theme' => 'image_style_preview', '#style' => $this->entity];
60 '#title' => $this->t('Preview'),
61 '#markup' => drupal_render($preview_arguments),
62 // Render preview above parent elements.
66 // Build the list of existing image effects for this image style.
72 $this->t('Operations'),
77 'relationship' => 'sibling',
78 'group' => 'image-effect-order-weight',
82 'id' => 'image-style-effects',
84 '#empty' => t('There are currently no effects in this style. Add one by selecting an option below.'),
85 // Render effects below parent elements.
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'] = [
96 '#plain_text' => $effect->label(),
101 $summary = $effect->getSummary();
103 if (!empty($summary)) {
104 $summary['#prefix'] = ' ';
105 $form['effects'][$key]['effect']['data']['summary'] = $summary;
108 $form['effects'][$key]['weight'] = [
110 '#title' => $this->t('Weight for @title', ['@title' => $effect->label()]),
111 '#title_display' => 'invisible',
112 '#default_value' => $effect->getWeight(),
114 'class' => ['image-effect-order-weight'],
119 $is_configurable = $effect instanceof ConfigurableImageEffectInterface;
120 if ($is_configurable) {
122 'title' => $this->t('Edit'),
123 'url' => Url::fromRoute('image.effect_edit_form', [
124 'image_style' => $this->entity->id(),
125 'image_effect' => $key,
130 'title' => $this->t('Delete'),
131 'url' => Url::fromRoute('image.effect_delete', [
132 'image_style' => $this->entity->id(),
133 'image_effect' => $key,
136 $form['effects'][$key]['operations'] = [
137 '#type' => 'operations',
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']);
148 foreach ($effects as $effect => $definition) {
149 $new_effect_options[$effect] = $definition['label'];
151 $form['effects']['new'] = [
153 '#weight' => isset($user_input['weight']) ? $user_input['weight'] : NULL,
154 '#attributes' => ['class' => ['draggable']],
156 $form['effects']['new']['effect'] = [
160 '#title' => $this->t('Effect'),
161 '#title_display' => 'invisible',
162 '#options' => $new_effect_options,
163 '#empty_option' => $this->t('Select a new effect'),
168 '#value' => $this->t('Add'),
169 '#validate' => ['::effectValidate'],
170 '#submit' => ['::submitForm', '::effectSave'],
174 '#prefix' => '<div class="image-style-new">',
175 '#suffix' => '</div>',
178 $form['effects']['new']['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']],
185 $form['effects']['new']['operations'] = [
189 return parent::form($form, $form_state);
193 * Validate handler for image effect.
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.'));
202 * Submit handler for image effect.
204 public function effectSave($form, FormStateInterface $form_state) {
205 $this->save($form, $form_state);
207 // Check if this field has any configuration options.
208 $effect = $this->imageEffectManager->getDefinition($form_state->getValue('new'));
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',
215 'image_style' => $this->entity->id(),
216 'image_effect' => $form_state->getValue('new'),
218 ['query' => ['weight' => $form_state->getValue('weight')]]
221 // If there's no form, immediately add the image effect.
224 'id' => $effect['id'],
226 'weight' => $form_state->getValue('weight'),
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.'));
239 public function submitForm(array &$form, FormStateInterface $form_state) {
241 // Update image effect weights.
242 if (!$form_state->isValueEmpty('effects')) {
243 $this->updateEffectWeights($form_state->getValue('effects'));
246 parent::submitForm($form, $form_state);
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.'));
260 public function actions(array $form, FormStateInterface $form_state) {
261 $actions = parent::actions($form, $form_state);
262 $actions['submit']['#value'] = $this->t('Update style');
268 * Updates image effect weights.
270 * @param array $effects
271 * Associative array with effects having effect uuid as keys and array
272 * with effect data as values.
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']);