You are here

public function MaskImageEffect::buildConfigurationForm in Image Effects 8.3

Same name and namespace in other branches
  1. 8 src/Plugin/ImageEffect/MaskImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\MaskImageEffect::buildConfigurationForm()
  2. 8.2 src/Plugin/ImageEffect/MaskImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\MaskImageEffect::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/MaskImageEffect.php, line 111

Class

MaskImageEffect
Class MaskImageEffect.

Namespace

Drupal\image_effects\Plugin\ImageEffect

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $options = [
    '#title' => $this
      ->t('Mask image'),
    '#description' => $this
      ->t('Image to use for masking. The mask file should be a grayscale image, where full white pixels will let the original image through, and full black pixels will hide it.'),
    '#default_value' => $this->configuration['mask_image'],
    '#required' => TRUE,
  ];
  $form['mask_image'] = $this->imageSelector
    ->selectionElement($options);
  $form['mask_resize'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Mask 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 mask maintaing its aspect ratio. Leave both dimensions empty to apply the mask in its original dimensions.'),
    '#open' => TRUE,
  ];
  $form['mask_resize']['mask_width'] = [
    '#type' => 'image_effects_px_perc',
    '#title' => $this
      ->t('Mask width'),
    '#default_value' => $this->configuration['mask_width'],
    '#size' => 5,
    '#maxlength' => 5,
    '#required' => FALSE,
  ];
  $form['mask_resize']['mask_height'] = [
    '#type' => 'image_effects_px_perc',
    '#title' => $this
      ->t('Mask height'),
    '#default_value' => $this->configuration['mask_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 mask on the canvas.'),
    '#required' => TRUE,
  ];
  $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 mask 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 mask downward, '-' sign upward."),
    '#default_value' => $this->configuration['y_offset'],
    '#maxlength' => 4,
    '#size' => 4,
  ];
  return $form;
}