You are here

public function ImagemagickToolkit::validateConfigurationForm in ImageMagick 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/ImageToolkit/ImagemagickToolkit.php \Drupal\imagemagick\Plugin\ImageToolkit\ImagemagickToolkit::validateConfigurationForm()
  2. 8.2 src/Plugin/ImageToolkit/ImagemagickToolkit.php \Drupal\imagemagick\Plugin\ImageToolkit\ImagemagickToolkit::validateConfigurationForm()

Form validation handler.

Parameters

array $form: An associative array containing the structure of the plugin form as built by static::buildConfigurationForm().

\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().

Overrides ImageToolkitBase::validateConfigurationForm

File

src/Plugin/ImageToolkit/ImagemagickToolkit.php, line 493

Class

ImagemagickToolkit
Provides ImageMagick integration toolkit for image manipulation.

Namespace

Drupal\imagemagick\Plugin\ImageToolkit

Code

public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  try {

    // Check that the format map contains valid YAML.
    $image_formats = Yaml::decode($form_state
      ->getValue([
      'imagemagick',
      'formats',
      'mapping',
      'image_formats',
    ]));

    // Validate the enabled image formats.
    $errors = $this->formatMapper
      ->validateMap($image_formats);
    if ($errors) {
      $form_state
        ->setErrorByName('imagemagick][formats][mapping][image_formats', new FormattableMarkup("<pre>@errors</pre>", [
        '@errors' => Yaml::encode($errors),
      ]));
    }
  } catch (InvalidDataTypeException $e) {

    // Invalid YAML detected, show details.
    $form_state
      ->setErrorByName('imagemagick][formats][mapping][image_formats', $this
      ->t("YAML syntax error: @error", [
      '@error' => $e
        ->getMessage(),
    ]));
  }

  // Validate the binaries path only if this toolkit is selected, otherwise
  // it will prevent the entire image toolkit selection form from being
  // submitted.
  if ($form_state
    ->getValue([
    'image_toolkit',
  ]) === 'imagemagick') {
    $status = $this
      ->checkPath($form_state
      ->getValue([
      'imagemagick',
      'suite',
      'path_to_binaries',
    ]), $form_state
      ->getValue([
      'imagemagick',
      'suite',
      'binaries',
    ]));
    if ($status['errors']) {
      $form_state
        ->setErrorByName('imagemagick][suite][path_to_binaries', new FormattableMarkup(implode('<br />', $status['errors']), []));
    }
  }
}