You are here

public function AddWatermarkEffect::validateConfigurationForm in Basic Watermark 8

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 ConfigurableImageEffectBase::validateConfigurationForm

File

src/Plugin/ImageEffect/AddWatermarkEffect.php, line 186

Class

AddWatermarkEffect
Converts an image resource.

Namespace

Drupal\basic_watermark\Plugin\ImageEffect

Code

public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  $path = DRUPAL_ROOT . $form_state
    ->getValue('watermark_path');
  if (!file_exists($path)) {
    $form_state
      ->setError($form['watermark_path'], $this
      ->t('File does not exist.'));
  }
  else {
    $image_details = getimagesize($path);
    if (!$image_details || $image_details['mime'] != 'image/png') {
      $form_state
        ->setError($form['watermark_path'], $this
        ->t('File not a png.'));
    }
  }
  $margins = $form_state
    ->getValue('margins');
  foreach ($margins as $field => $margin) {
    if ($margin !== '' && (!is_numeric($margin) || intval($margin) != $margin || $margin < 0)) {
      $form_state
        ->setError($form['margins'][$field], $this
        ->t('%name must be a non negative integer.', [
        '%name' => $form['margins'][$field]['#title'],
      ]));
    }
  }
}