You are here

public function AnnotateImageEffect::buildConfigurationForm in Imagick 8

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/AnnotateImageEffect.php, line 58

Class

AnnotateImageEffect
Annotates an image resource.

Namespace

Drupal\imagick\Plugin\ImageEffect

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'colorform',
      ],
    ],
  ];

  // Get fonts
  $imagick = new Imagick();
  $available_fonts = $imagick
    ->queryFonts();
  $form['text_fieldset'] = [
    '#type' => 'details',
    '#open' => TRUE,
    '#title' => $this
      ->t('Text'),
    'text' => [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Text'),
      '#description' => $this
        ->t('Text to annotate the image with.'),
      '#default_value' => $this->configuration['text_fieldset']['text'],
    ],
    'font' => [
      '#type' => 'select',
      '#options' => array_combine($available_fonts, $available_fonts),
      '#title' => $this
        ->t('Font'),
      '#description' => $this
        ->t('Fonts that ImageMagick knows about.'),
      '#default_value' => $this->configuration['text_fieldset']['font'],
    ],
    'size' => [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Font size'),
      '#default_value' => $this->configuration['text_fieldset']['size'],
      '#size' => 3,
    ],
  ];
  $form['text_fieldset']['HEX'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('HEX'),
    '#default_value' => $this->configuration['text_fieldset']['HEX'],
    '#attributes' => [
      'class' => [
        'colorentry',
      ],
    ],
  ];
  $form['text_fieldset']['colorpicker'] = [
    '#weight' => -1,
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'colorpicker',
      ],
      'style' => [
        'float:right',
      ],
    ],
  ];

  // Add Farbtastic color picker.
  $form['text_fieldset']['matte_color']['#attached'] = [
    'library' => [
      'imagick/colorpicker',
    ],
  ];
  $form['position_fieldset'] = [
    '#type' => 'details',
    '#open' => TRUE,
    '#title' => $this
      ->t('Position'),
    'anchor' => [
      '#type' => 'radios',
      '#title' => $this
        ->t('Anchor'),
      '#options' => [
        'left-top' => $this
          ->t('Top left'),
        'center-top' => $this
          ->t('Top center'),
        'right-top' => $this
          ->t('Top right'),
        'left-center' => $this
          ->t('Center left'),
        'center-center' => $this
          ->t('Center'),
        'right-center' => $this
          ->t('Center right'),
        'left-bottom' => $this
          ->t('Bottom left'),
        'center-bottom' => $this
          ->t('Bottom center'),
        'right-bottom' => $this
          ->t('Bottom right'),
      ],
      '#theme' => 'image_anchor',
      '#default_value' => $this->configuration['position_fieldset']['anchor'],
    ],
    'padding_x' => [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Padding X'),
      '#default_value' => $this->configuration['position_fieldset']['padding_x'],
      '#description' => $this
        ->t('Enter a value in pixels or percent'),
      '#size' => 3,
    ],
    'padding_y' => [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Padding Y'),
      '#default_value' => $this->configuration['position_fieldset']['padding_y'],
      '#description' => $this
        ->t('Enter a value in pixels or percent'),
      '#size' => 3,
    ],
  ];
  return $form;
}