3 namespace Drupal\image\Form;
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Form\SubformState;
8 use Drupal\image\ConfigurableImageEffectInterface;
9 use Drupal\image\ImageStyleInterface;
10 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
11 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
14 * Provides a base form for image effects.
16 abstract class ImageEffectFormBase extends FormBase {
21 * @var \Drupal\image\ImageStyleInterface
23 protected $imageStyle;
28 * @var \Drupal\image\ImageEffectInterface|\Drupal\image\ConfigurableImageEffectInterface
30 protected $imageEffect;
35 public function getFormId() {
36 return 'image_effect_form';
42 * @param \Drupal\image\ImageStyleInterface $image_style
44 * @param string $image_effect
45 * The image effect ID.
50 * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
52 public function buildForm(array $form, FormStateInterface $form_state, ImageStyleInterface $image_style = NULL, $image_effect = NULL) {
53 $this->imageStyle = $image_style;
55 $this->imageEffect = $this->prepareImageEffect($image_effect);
57 catch (PluginNotFoundException $e) {
58 throw new NotFoundHttpException("Invalid effect id: '$image_effect'.");
60 $request = $this->getRequest();
62 if (!($this->imageEffect instanceof ConfigurableImageEffectInterface)) {
63 throw new NotFoundHttpException();
66 $form['#attached']['library'][] = 'image/admin';
69 '#value' => $this->imageEffect->getUuid(),
73 '#value' => $this->imageEffect->getPluginId(),
77 $subform_state = SubformState::createForSubform($form['data'], $form, $form_state);
78 $form['data'] = $this->imageEffect->buildConfigurationForm($form['data'], $subform_state);
79 $form['data']['#tree'] = TRUE;
81 // Check the URL for a weight, then the image effect, otherwise use default.
84 '#value' => $request->query->has('weight') ? (int) $request->query->get('weight') : $this->imageEffect->getWeight(),
87 $form['actions'] = ['#type' => 'actions'];
88 $form['actions']['submit'] = [
90 '#button_type' => 'primary',
92 $form['actions']['cancel'] = [
94 '#title' => $this->t('Cancel'),
95 '#url' => $this->imageStyle->urlInfo('edit-form'),
96 '#attributes' => ['class' => ['button']],
104 public function validateForm(array &$form, FormStateInterface $form_state) {
105 // The image effect configuration is stored in the 'data' key in the form,
106 // pass that through for validation.
107 $this->imageEffect->validateConfigurationForm($form['data'], SubformState::createForSubform($form['data'], $form, $form_state));
113 public function submitForm(array &$form, FormStateInterface $form_state) {
114 $form_state->cleanValues();
116 // The image effect configuration is stored in the 'data' key in the form,
117 // pass that through for submission.
118 $this->imageEffect->submitConfigurationForm($form['data'], SubformState::createForSubform($form['data'], $form, $form_state));
120 $this->imageEffect->setWeight($form_state->getValue('weight'));
121 if (!$this->imageEffect->getUuid()) {
122 $this->imageStyle->addImageEffect($this->imageEffect->getConfiguration());
124 $this->imageStyle->save();
126 drupal_set_message($this->t('The image effect was successfully applied.'));
127 $form_state->setRedirectUrl($this->imageStyle->urlInfo('edit-form'));
131 * Converts an image effect ID into an object.
133 * @param string $image_effect
134 * The image effect ID.
136 * @return \Drupal\image\ImageEffectInterface
137 * The image effect object.
139 abstract protected function prepareImageEffect($image_effect);