You are here

public function WatermarkImageEffect::buildConfigurationForm in Image Effects 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/ImageEffect/WatermarkImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\WatermarkImageEffect::buildConfigurationForm()
  2. 8 src/Plugin/ImageEffect/WatermarkImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\WatermarkImageEffect::buildConfigurationForm()

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PluginFormInterface::buildConfigurationForm

File

src/Plugin/ImageEffect/WatermarkImageEffect.php, line 112

Class

WatermarkImageEffect
Class WatermarkImageEffect.

Namespace

Drupal\image_effects\Plugin\ImageEffect

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $options = [
    '#title' => $this
      ->t('Watermark image'),
    '#description' => $this
      ->t('Image to use as watermark.'),
    '#default_value' => $this->configuration['watermark_image'],
  ];
  $form['watermark_image'] = $this->imageSelector
    ->selectionElement($options);
  $form['watermark_resize'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Watermark resize'),
    '#description' => $this
      ->t('Select dimensions either in pixels or as percentage of the <strong>current canvas</strong>. Leaving one dimension empty will resize the watermark maintaing its aspect ratio. Leave both dimensions empty to apply the watermark in its original dimensions.'),
    '#open' => TRUE,
  ];
  $form['watermark_resize']['watermark_width'] = [
    '#type' => 'image_effects_px_perc',
    '#title' => $this
      ->t('Watermark width'),
    '#default_value' => $this->configuration['watermark_width'],
    '#size' => 5,
    '#maxlength' => 5,
    '#required' => FALSE,
  ];
  $form['watermark_resize']['watermark_height'] = [
    '#type' => 'image_effects_px_perc',
    '#title' => $this
      ->t('Watermark height'),
    '#default_value' => $this->configuration['watermark_height'],
    '#size' => 5,
    '#maxlength' => 5,
    '#required' => FALSE,
  ];
  $form['placement'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Placement'),
    '#options' => $this
      ->anchorOptions(),
    '#theme' => 'image_anchor',
    '#default_value' => $this->configuration['placement'],
    '#description' => $this
      ->t('Position of the watermark on the canvas.'),
  ];
  $form['x_offset'] = [
    '#type' => 'image_effects_px_perc',
    '#title' => $this
      ->t('Horizontal offset'),
    '#description' => $this
      ->t("Additional horizontal offset from placement. Enter a value, and specify if pixels or percent of the canvas width. '+' or no sign shifts the watermark rightward, '-' sign leftward."),
    '#default_value' => $this->configuration['x_offset'],
    '#maxlength' => 4,
    '#size' => 4,
  ];
  $form['y_offset'] = [
    '#type' => 'image_effects_px_perc',
    '#title' => $this
      ->t('Vertical offset'),
    '#description' => $this
      ->t("Additional vertical offset from placement. Enter a value, and specify if pixels or percent of the canvas height. '+' or no sign shifts the watermark downward, '-' sign upward."),
    '#default_value' => $this->configuration['y_offset'],
    '#maxlength' => 4,
    '#size' => 4,
  ];
  $form['opacity'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Opacity'),
    '#field_suffix' => '%',
    '#description' => $this
      ->t('Opacity, in percentage, of the watermark on the canvas. 0% is fully transparent, 100% is fully opaque.'),
    '#default_value' => $this->configuration['opacity'],
    '#min' => 0,
    '#max' => 100,
    '#maxlength' => 3,
    '#size' => 3,
  ];
  return $form;
}