You are here

public function ImageCaptchaSettingsForm::buildForm in CAPTCHA 8

Form constructor.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

image_captcha/src/Form/ImageCaptchaSettingsForm.php, line 79

Class

ImageCaptchaSettingsForm
Displays the pants settings form.

Namespace

Drupal\image_captcha\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('image_captcha.settings');

  // Add CSS and JS for theming and added usability on admin form.
  $form['#attached']['library'][] = 'image_captcha/base';

  // First some error checking.
  $setup_status = _image_captcha_check_setup(FALSE);
  if ($setup_status & IMAGE_CAPTCHA_ERROR_NO_GDLIB) {
    $this
      ->messenger()
      ->addError($this
      ->t('The Image CAPTCHA module can not generate images because your PHP setup does not support it (no <a href="!gdlib" target="_blank">GD library</a> with JPEG support).', [
      '!gdlib' => 'http://php.net/manual/en/book.image.php',
    ]));

    // It is no use to continue building the rest of the settings form.
    return $form;
  }
  $form['image_captcha_example'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Example'),
    '#description' => $this
      ->t('Presolved image CAPTCHA example, generated with the current settings.'),
  ];
  $form['image_captcha_example']['image'] = [
    '#type' => 'captcha',
    '#captcha_type' => 'image_captcha/Image',
    '#captcha_admin_mode' => TRUE,
  ];

  // General code settings.
  $form['image_captcha_code_settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Code settings'),
  ];
  $form['image_captcha_code_settings']['image_captcha_image_allowed_chars'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Characters to use in the code'),
    '#default_value' => $config
      ->get('image_captcha_image_allowed_chars'),
  ];
  $form['image_captcha_code_settings']['image_captcha_code_length'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Code length'),
    '#options' => [
      2 => 2,
      3,
      4,
      5,
      6,
      7,
      8,
      9,
      10,
    ],
    '#default_value' => $config
      ->get('image_captcha_code_length'),
    '#description' => $this
      ->t('The code length influences the size of the image. Note that larger values make the image generation more CPU intensive.'),
  ];

  // RTL support option (only show this option when there are RTL languages).
  $language = $this->languageManager
    ->getCurrentLanguage();
  if ($language
    ->getDirection() == Language::DIRECTION_RTL) {
    $form['image_captcha_code_settings']['image_captcha_rtl_support'] = [
      '#title' => $this
        ->t('RTL support'),
      '#type' => 'checkbox',
      '#default_value' => $config
        ->get('image_captcha_rtl_support'),
      '#description' => $this
        ->t('Enable this option to render the code from right to left for right to left languages.'),
    ];
  }

  // Font related stuff.
  $form['image_captcha_font_settings'] = $this
    ->settingsDotSection();

  // Color and file format settings.
  $form['image_captcha_color_settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Color and image settings'),
    '#description' => $this
      ->t('Configuration of the background, text colors and file format of the image CAPTCHA.'),
  ];
  $form['image_captcha_color_settings']['image_captcha_background_color'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Background color'),
    '#description' => $this
      ->t('Enter the hexadecimal code for the background color (e.g. #FFF or #FFCE90). When using the PNG file format with transparent background, it is recommended to set this close to the underlying background color.'),
    '#default_value' => $config
      ->get('image_captcha_background_color'),
    '#maxlength' => 7,
    '#size' => 8,
  ];
  $form['image_captcha_color_settings']['image_captcha_foreground_color'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Text color'),
    '#description' => $this
      ->t('Enter the hexadecimal code for the text color (e.g. #000 or #004283).'),
    '#default_value' => $config
      ->get('image_captcha_foreground_color'),
    '#maxlength' => 7,
    '#size' => 8,
  ];
  $form['image_captcha_color_settings']['image_captcha_foreground_color_randomness'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Additional variation of text color'),
    '#options' => [
      0 => $this
        ->t('No variation'),
      50 => $this
        ->t('Little variation'),
      100 => $this
        ->t('Medium variation'),
      150 => $this
        ->t('High variation'),
      200 => $this
        ->t('Very high variation'),
    ],
    '#default_value' => $config
      ->get('image_captcha_foreground_color_randomness'),
    '#description' => $this
      ->t('The different characters will have randomized colors in the specified range around the text color.'),
  ];
  $form['image_captcha_color_settings']['image_captcha_file_format'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('File format'),
    '#description' => $this
      ->t('Select the file format for the image. JPEG usually results in smaller files, PNG allows tranparency.'),
    '#default_value' => $config
      ->get('image_captcha_file_format'),
    '#options' => [
      IMAGE_CAPTCHA_FILE_FORMAT_JPG => $this
        ->t('JPEG'),
      IMAGE_CAPTCHA_FILE_FORMAT_PNG => $this
        ->t('PNG'),
      IMAGE_CAPTCHA_FILE_FORMAT_TRANSPARENT_PNG => $this
        ->t('PNG with transparent background'),
    ],
  ];

  // Distortion and noise settings.
  $form['image_captcha_distortion_and_noise'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Distortion and noise'),
    '#description' => $this
      ->t('With these settings you can control the degree of obfuscation by distortion and added noise. Do not exaggerate the obfuscation and assure that the code in the image is reasonably readable. For example, do not combine high levels of distortion and noise.'),
  ];
  $form['image_captcha_distortion_and_noise']['image_captcha_distortion_amplitude'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Distortion level'),
    '#options' => [
      0 => $this
        ->t('@level - no distortion', [
        '@level' => '0',
      ]),
      1 => $this
        ->t('@level - low', [
        '@level' => '1',
      ]),
      2 => '2',
      3 => '3',
      4 => '4',
      5 => $this
        ->t('@level - medium', [
        '@level' => '5',
      ]),
      6 => '6',
      7 => '7',
      8 => '8',
      9 => '9',
      10 => $this
        ->t('@level - high', [
        '@level' => '10',
      ]),
    ],
    '#default_value' => $config
      ->get('image_captcha_distortion_amplitude'),
    '#description' => $this
      ->t('Set the degree of wave distortion in the image.'),
  ];
  $form['image_captcha_distortion_and_noise']['image_captcha_bilinear_interpolation'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Smooth distortion'),
    '#default_value' => $config
      ->get('image_captcha_bilinear_interpolation'),
    '#description' => $this
      ->t('This option enables bilinear interpolation of the distortion which makes the image look smoother, but it is more CPU intensive.'),
  ];
  $form['image_captcha_distortion_and_noise']['image_captcha_dot_noise'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Add salt and pepper noise'),
    '#default_value' => $config
      ->get('image_captcha_dot_noise'),
    '#description' => $this
      ->t('This option adds randomly colored point noise.'),
  ];
  $form['image_captcha_distortion_and_noise']['image_captcha_line_noise'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Add line noise'),
    '#default_value' => $config
      ->get('image_captcha_line_noise', 0),
    '#description' => $this
      ->t('This option enables lines randomly drawn on top of the text code.'),
  ];
  $form['image_captcha_distortion_and_noise']['image_captcha_noise_level'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Noise level'),
    '#options' => [
      1 => '1 - ' . $this
        ->t('low'),
      2 => '2',
      3 => '3 - ' . $this
        ->t('medium'),
      4 => '4',
      5 => '5 - ' . $this
        ->t('high'),
      7 => '7',
      10 => '10 - ' . $this
        ->t('severe'),
    ],
    '#default_value' => (int) $config
      ->get('image_captcha_noise_level'),
  ];
  return parent::buildForm($form, $form_state);
}