3 namespace Drupal\image\Form;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\image\ConfigurableImageEffectInterface;
10 use Drupal\image\ImageEffectManager;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Controller for image style edit form.
16 class ImageStyleEditForm extends ImageStyleFormBase {
19 * The image effect manager service.
21 * @var \Drupal\image\ImageEffectManager
23 protected $imageEffectManager;
26 * Constructs an ImageStyleEditForm object.
28 * @param \Drupal\Core\Entity\EntityStorageInterface $image_style_storage
30 * @param \Drupal\image\ImageEffectManager $image_effect_manager
31 * The image effect manager service.
33 public function __construct(EntityStorageInterface $image_style_storage, ImageEffectManager $image_effect_manager) {
34 parent::__construct($image_style_storage);
35 $this->imageEffectManager = $image_effect_manager;
41 public static function create(ContainerInterface $container) {
43 $container->get('entity.manager')->getStorage('image_style'),
44 $container->get('plugin.manager.image.effect')
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';
57 // Show the thumbnail preview.
58 $preview_arguments = ['#theme' => 'image_style_preview', '#style' => $this->entity];
61 '#title' => $this->t('Preview'),
62 '#markup' => \Drupal::service('renderer')->render($preview_arguments),
63 // Render preview above parent elements.
67 // Build the list of existing image effects for this image style.
73 $this->t('Operations'),
78 'relationship' => 'sibling',
79 'group' => 'image-effect-order-weight',
83 'id' => 'image-style-effects',
85 '#empty' => t('There are currently no effects in this style. Add one by selecting an option below.'),
86 // Render effects below parent elements.
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'] = [
97 '#plain_text' => $effect->label(),
102 $summary = $effect->getSummary();
104 if (!empty($summary)) {
105 $summary['#prefix'] = ' ';
106 $form['effects'][$key]['effect']['data']['summary'] = $summary;
109 $form['effects'][$key]['weight'] = [
111 '#title' => $this->t('Weight for @title', ['@title' => $effect->label()]),
112 '#title_display' => 'invisible',
113 '#default_value' => $effect->getWeight(),
115 'class' => ['image-effect-order-weight'],
120 $is_configurable = $effect instanceof ConfigurableImageEffectInterface;
121 if ($is_configurable) {
123 'title' => $this->t('Edit'),
124 'url' => Url::fromRoute('image.effect_edit_form', [
125 'image_style' => $this->entity->id(),
126 'image_effect' => $key,
131 'title' => $this->t('Delete'),
132 'url' => Url::fromRoute('image.effect_delete', [
133 'image_style' => $this->entity->id(),
134 'image_effect' => $key,
137 $form['effects'][$key]['operations'] = [
138 '#type' => 'operations',
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']);
149 foreach ($effects as $effect => $definition) {
150 $new_effect_options[$effect] = $definition['label'];
152 $form['effects']['new'] = [
154 '#weight' => isset($user_input['weight']) ? $user_input['weight'] : NULL,
155 '#attributes' => ['class' => ['draggable']],
157 $form['effects']['new']['effect'] = [
161 '#title' => $this->t('Effect'),
162 '#title_display' => 'invisible',
163 '#options' => $new_effect_options,
164 '#empty_option' => $this->t('Select a new effect'),
169 '#value' => $this->t('Add'),
170 '#validate' => ['::effectValidate'],
171 '#submit' => ['::submitForm', '::effectSave'],
175 '#prefix' => '<div class="image-style-new">',
176 '#suffix' => '</div>',
179 $form['effects']['new']['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']],
186 $form['effects']['new']['operations'] = [
190 return parent::form($form, $form_state);
194 * Validate handler for image effect.
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.'));
203 * Submit handler for image effect.
205 public function effectSave($form, FormStateInterface $form_state) {
206 $this->save($form, $form_state);
208 // Check if this field has any configuration options.
209 $effect = $this->imageEffectManager->getDefinition($form_state->getValue('new'));
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',
216 'image_style' => $this->entity->id(),
217 'image_effect' => $form_state->getValue('new'),
219 ['query' => ['weight' => $form_state->getValue('weight')]]
222 // If there's no form, immediately add the image effect.
225 'id' => $effect['id'],
227 'weight' => $form_state->getValue('weight'),
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.'));
240 public function submitForm(array &$form, FormStateInterface $form_state) {
242 // Update image effect weights.
243 if (!$form_state->isValueEmpty('effects')) {
244 $this->updateEffectWeights($form_state->getValue('effects'));
247 parent::submitForm($form, $form_state);
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.'));
261 public function actions(array $form, FormStateInterface $form_state) {
262 $actions = parent::actions($form, $form_state);
263 $actions['submit']['#value'] = $this->t('Update style');
269 * Updates image effect weights.
271 * @param array $effects
272 * Associative array with effects having effect uuid as keys and array
273 * with effect data as values.
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']);