Newer
Older
Alex Pott
committed
<?php
namespace Drupal\image\Form;
use Drupal\Component\Utility\Unicode;
catch
committed
use Drupal\Core\Entity\EntityStorageInterface;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
Alex Pott
committed
use Drupal\image\ConfigurableImageEffectInterface;
use Drupal\image\ImageEffectManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Controller for image style edit form.
*
* @internal
Alex Pott
committed
*/
class ImageStyleEditForm extends ImageStyleFormBase {
/**
* The image effect manager service.
*
* @var \Drupal\image\ImageEffectManager
*/
protected $imageEffectManager;
/**
* Constructs an ImageStyleEditForm object.
*
catch
committed
* @param \Drupal\Core\Entity\EntityStorageInterface $image_style_storage
* The storage.
Alex Pott
committed
* @param \Drupal\image\ImageEffectManager $image_effect_manager
* The image effect manager service.
*/
catch
committed
public function __construct(EntityStorageInterface $image_style_storage, ImageEffectManager $image_effect_manager) {
Alex Pott
committed
parent::__construct($image_style_storage);
Alex Pott
committed
$this->imageEffectManager = $image_effect_manager;
}
/**
* {@inheritdoc}
*/
Alex Pott
committed
public static function create(ContainerInterface $container) {
Alex Pott
committed
return new static(
catch
committed
$container->get('entity.manager')->getStorage('image_style'),
Alex Pott
committed
$container->get('plugin.manager.image.effect')
);
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function form(array $form, FormStateInterface $form_state) {
Angie Byron
committed
$user_input = $form_state->getUserInput();
$form['#title'] = $this->t('Edit style %name', ['%name' => $this->entity->label()]);
Alex Pott
committed
$form['#tree'] = TRUE;
$form['#attached']['library'][] = 'image/admin';
Alex Pott
committed
// Show the thumbnail preview.
$preview_arguments = ['#theme' => 'image_style_preview', '#style' => $this->entity];
$form['preview'] = [
Alex Pott
committed
'#type' => 'item',
Alex Pott
committed
'#title' => $this->t('Preview'),
'#markup' => \Drupal::service('renderer')->render($preview_arguments),
Alex Pott
committed
// Render preview above parent elements.
'#weight' => -5,
];
Alex Pott
committed
// Build the list of existing image effects for this image style.
$form['effects'] = [
'#type' => 'table',
'#header' => [
$this->t('Effect'),
$this->t('Weight'),
$this->t('Operations'),
],
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'image-effect-order-weight',
],
],
'#attributes' => [
'id' => 'image-style-effects',
],
'#empty' => t('There are currently no effects in this style. Add one by selecting an option below.'),
Alex Pott
committed
// Render effects below parent elements.
'#weight' => 5,
];
foreach ($this->entity->getEffects() as $effect) {
Alex Pott
committed
$key = $effect->getUuid();
$form['effects'][$key]['#attributes']['class'][] = 'draggable';
Angie Byron
committed
$form['effects'][$key]['#weight'] = isset($user_input['effects']) ? $user_input['effects'][$key]['weight'] : NULL;
$form['effects'][$key]['effect'] = [
'#tree' => FALSE,
'data' => [
'label' => [
'#plain_text' => $effect->label(),
],
],
];
$summary = $effect->getSummary();
if (!empty($summary)) {
$summary['#prefix'] = ' ';
$form['effects'][$key]['effect']['data']['summary'] = $summary;
}
$form['effects'][$key]['weight'] = [
Alex Pott
committed
'#type' => 'weight',
'#title' => $this->t('Weight for @title', ['@title' => $effect->label()]),
Alex Pott
committed
'#title_display' => 'invisible',
'#default_value' => $effect->getWeight(),
'#attributes' => [
'class' => ['image-effect-order-weight'],
],
];
Alex Pott
committed
$links = [];
Alex Pott
committed
$is_configurable = $effect instanceof ConfigurableImageEffectInterface;
if ($is_configurable) {
$links['edit'] = [
Angie Byron
committed
'title' => $this->t('Edit'),
'url' => Url::fromRoute('image.effect_edit_form', [
'image_style' => $this->entity->id(),
'image_effect' => $key,
]),
];
Alex Pott
committed
}
$links['delete'] = [
Angie Byron
committed
'title' => $this->t('Delete'),
'url' => Url::fromRoute('image.effect_delete', [
'image_style' => $this->entity->id(),
'image_effect' => $key,
]),
];
$form['effects'][$key]['operations'] = [
Alex Pott
committed
'#type' => 'operations',
'#links' => $links,
];
Alex Pott
committed
}
// Build the new image effect addition form and add it to the effect list.
$new_effect_options = [];
Alex Pott
committed
$effects = $this->imageEffectManager->getDefinitions();
uasort($effects, function ($a, $b) {
return Unicode::strcasecmp($a['label'], $b['label']);
Alex Pott
committed
});
foreach ($effects as $effect => $definition) {
$new_effect_options[$effect] = $definition['label'];
}
$form['effects']['new'] = [
Alex Pott
committed
'#tree' => FALSE,
Angie Byron
committed
'#weight' => isset($user_input['weight']) ? $user_input['weight'] : NULL,
'#attributes' => ['class' => ['draggable']],
];
$form['effects']['new']['effect'] = [
'data' => [
'new' => [
'#type' => 'select',
'#title' => $this->t('Effect'),
'#title_display' => 'invisible',
'#options' => $new_effect_options,
'#empty_option' => $this->t('Select a new effect'),
],
[
'add' => [
'#type' => 'submit',
'#value' => $this->t('Add'),
'#validate' => ['::effectValidate'],
'#submit' => ['::submitForm', '::effectSave'],
],
],
],
'#prefix' => '<div class="image-style-new">',
'#suffix' => '</div>',
];
$form['effects']['new']['weight'] = [
Alex Pott
committed
'#type' => 'weight',
Alex Pott
committed
'#title' => $this->t('Weight for new effect'),
Alex Pott
committed
'#title_display' => 'invisible',
'#default_value' => count($this->entity->getEffects()) + 1,
'#attributes' => ['class' => ['image-effect-order-weight']],
];
$form['effects']['new']['operations'] = [
'data' => [],
];
Alex Pott
committed
return parent::form($form, $form_state);
}
/**
* Validate handler for image effect.
*/
Dries Buytaert
committed
public function effectValidate($form, FormStateInterface $form_state) {
Alex Pott
committed
if (!$form_state->getValue('new')) {
$form_state->setErrorByName('new', $this->t('Select an effect to add.'));
Alex Pott
committed
}
}
/**
* Submit handler for image effect.
*/
Dries Buytaert
committed
public function effectSave($form, FormStateInterface $form_state) {
$this->save($form, $form_state);
Alex Pott
committed
// Check if this field has any configuration options.
Alex Pott
committed
$effect = $this->imageEffectManager->getDefinition($form_state->getValue('new'));
Alex Pott
committed
// Load the configuration form for this option.
if (is_subclass_of($effect['class'], '\Drupal\image\ConfigurableImageEffectInterface')) {
Alex Pott
committed
$form_state->setRedirect(
'image.effect_add_form',
[
Angie Byron
committed
'image_style' => $this->entity->id(),
Alex Pott
committed
'image_effect' => $form_state->getValue('new'),
],
['query' => ['weight' => $form_state->getValue('weight')]]
Angie Byron
committed
);
Alex Pott
committed
}
// If there's no form, immediately add the image effect.
else {
$effect = [
Alex Pott
committed
'id' => $effect['id'],
'data' => [],
Alex Pott
committed
'weight' => $form_state->getValue('weight'),
];
$effect_id = $this->entity->addImageEffect($effect);
$this->entity->save();
Alex Pott
committed
if (!empty($effect_id)) {
$this->messenger()->addStatus($this->t('The image effect was successfully applied.'));
Alex Pott
committed
}
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
Alex Pott
committed
// Update image effect weights.
Alex Pott
committed
if (!$form_state->isValueEmpty('effects')) {
$this->updateEffectWeights($form_state->getValue('effects'));
Alex Pott
committed
}
parent::submitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
Alex Pott
committed
parent::save($form, $form_state);
$this->messenger()->addStatus($this->t('Changes to the style have been saved.'));
Alex Pott
committed
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function actions(array $form, FormStateInterface $form_state) {
Alex Pott
committed
$actions = parent::actions($form, $form_state);
Alex Pott
committed
$actions['submit']['#value'] = $this->t('Update style');
Alex Pott
committed
return $actions;
}
/**
* Updates image effect weights.
*
* @param array $effects
Alex Pott
committed
* Associative array with effects having effect uuid as keys and array
Alex Pott
committed
* with effect data as values.
*/
protected function updateEffectWeights(array $effects) {
foreach ($effects as $uuid => $effect_data) {
if ($this->entity->getEffects()->has($uuid)) {
$this->entity->getEffect($uuid)->setWeight($effect_data['weight']);
}
}
}
}