ImageEffectFormBase.php 4.42 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\image\Form\ImageEffectFormBase.
 */

namespace Drupal\image\Form;

10
use Drupal\Core\Form\FormBase;
11 12
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
13 14
use Drupal\image\ConfigurableImageEffectInterface;
use Drupal\image\ImageStyleInterface;
15
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
16
use Drupal\Component\Utility\String;
17 18 19 20 21
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * Provides a base form for image effects.
 */
22
abstract class ImageEffectFormBase extends FormBase {
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

  /**
   * The image style.
   *
   * @var \Drupal\image\ImageStyleInterface
   */
  protected $imageStyle;

  /**
   * The image effect.
   *
   * @var \Drupal\image\ImageEffectInterface
   */
  protected $imageEffect;

  /**
   * {@inheritdoc}
   */
41
  public function getFormId() {
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    return 'image_effect_form';
  }

  /**
   * {@inheritdoc}
   *
   * @param \Drupal\image\ImageStyleInterface $image_style
   *   The image style.
   * @param string $image_effect
   *   The image effect ID.
   *
   * @return array
   *   The form structure.
   *
   * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
   */
58
  public function buildForm(array $form, FormStateInterface $form_state, ImageStyleInterface $image_style = NULL, $image_effect = NULL) {
59
    $this->imageStyle = $image_style;
60 61 62
    try {
      $this->imageEffect = $this->prepareImageEffect($image_effect);
    }
63
    catch (PluginNotFoundException $e) {
64 65
      throw new NotFoundHttpException(String::format("Invalid effect id: '@id'.", array('@id' => $image_effect)));
    }
66
    $request = $this->getRequest();
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

    if (!($this->imageEffect instanceof ConfigurableImageEffectInterface)) {
      throw new NotFoundHttpException();
    }

    $form['#attached']['css'][drupal_get_path('module', 'image') . '/css/image.admin.css'] = array();
    $form['uuid'] = array(
      '#type' => 'value',
      '#value' => $this->imageEffect->getUuid(),
    );
    $form['id'] = array(
      '#type' => 'value',
      '#value' => $this->imageEffect->getPluginId(),
    );

82
    $form['data'] = $this->imageEffect->buildConfigurationForm(array(), $form_state);
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    $form['data']['#tree'] = TRUE;

    // Check the URL for a weight, then the image effect, otherwise use default.
    $form['weight'] = array(
      '#type' => 'hidden',
      '#value' => $request->query->has('weight') ? (int) $request->query->get('weight') : $this->imageEffect->getWeight(),
    );

    $form['actions'] = array('#type' => 'actions');
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#button_type' => 'primary',
    );
    $form['actions']['cancel'] = array(
      '#type' => 'link',
98
      '#title' => $this->t('Cancel'),
99
    ) + $this->imageStyle->urlInfo('edit-form')->toRenderArray();
100 101 102
    return $form;
  }

103 104 105
  /**
   * {@inheritdoc}
   */
106
  public function validateForm(array &$form, FormStateInterface $form_state) {
107 108
    // The image effect configuration is stored in the 'data' key in the form,
    // pass that through for validation.
109
    $effect_data = (new FormState())->setValues($form_state->getValue('data'));
110
    $this->imageEffect->validateConfigurationForm($form, $effect_data);
111
    // Update the original form values.
112
    $form_state->setValue('data', $effect_data['values']);
113 114
  }

115 116 117
  /**
   * {@inheritdoc}
   */
118
  public function submitForm(array &$form, FormStateInterface $form_state) {
119
    form_state_values_clean($form_state);
120 121 122

    // The image effect configuration is stored in the 'data' key in the form,
    // pass that through for submission.
123
    $effect_data = (new FormState())->setValues($form_state->getValue('data'));
124
    $this->imageEffect->submitConfigurationForm($form, $effect_data);
125
    // Update the original form values.
126
    $form_state->setValue('data', $effect_data['values']);
127

128
    $this->imageEffect->setWeight($form_state->getValue('weight'));
129 130 131 132
    if (!$this->imageEffect->getUuid()) {
      $this->imageStyle->addImageEffect($this->imageEffect->getConfiguration());
    }
    $this->imageStyle->save();
133

134
    drupal_set_message($this->t('The image effect was successfully applied.'));
135
    $form_state->setRedirectUrl($this->imageStyle->urlInfo('edit-form'));
136 137 138 139 140 141 142 143 144 145 146 147 148 149
  }

  /**
   * Converts an image effect ID into an object.
   *
   * @param string $image_effect
   *   The image effect ID.
   *
   * @return \Drupal\image\ImageEffectInterface
   *   The image effect object.
   */
  abstract protected function prepareImageEffect($image_effect);

}