You are here

protected function ImageCaptchaSettingsForm::settingsDotSection in CAPTCHA 8

Form elements for the font specific setting.

This is refactored to a separate function to avoid polluting the general form function image_captcha_settings_form with some specific logic.

Return value

array The font settings specific form elements.

1 call to ImageCaptchaSettingsForm::settingsDotSection()
ImageCaptchaSettingsForm::buildForm in image_captcha/src/Form/ImageCaptchaSettingsForm.php
Form constructor.

File

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

Class

ImageCaptchaSettingsForm
Displays the pants settings form.

Namespace

Drupal\image_captcha\Form

Code

protected function settingsDotSection() {
  $config = $this
    ->config('image_captcha.settings');

  // Put it all in a details element.
  $form = [
    '#type' => 'details',
    '#title' => $this
      ->t('Font settings'),
  ];

  // First check if there is TrueType support.
  $setup_status = _image_captcha_check_setup(FALSE);
  if ($setup_status & IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT) {

    // Show a warning that there is no TrueType support.
    $form['no_ttf_support'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('No TrueType support'),
      '#markup' => $this
        ->t('The Image CAPTCHA module can not use TrueType fonts because your PHP setup does not support it. You can only use a PHP built-in bitmap font of fixed size.'),
    ];
  }
  else {

    // Build a list of  all available fonts.
    $available_fonts = [];

    // List of folders to search through for TrueType fonts.
    $fonts = _image_captcha_get_available_fonts_from_directories();

    // Cache the list of previewable fonts. All the previews are done
    // in separate requests, and we don't want to rescan the filesystem
    // every time, so we cache the result.
    $config
      ->set('image_captcha_fonts_preview_map_cache', $fonts);
    $config
      ->save();

    // Put these fonts with preview image in the list.
    foreach ($fonts as $token => $font) {
      $title = $this
        ->t('Font preview of @font (@file)', [
        '@font' => $font['name'],
        '@file' => $font['uri'],
      ]);
      $attributes = [
        'src' => Url::fromRoute('image_captcha.font_preview', [
          'token' => $token,
        ])
          ->toString(),
        'title' => $title,
        'alt' => $title,
      ];
      $available_fonts[$token] = '<img' . new Attribute($attributes) . ' />';
    }

    // Append the PHP built-in font at the end.
    $title = $this
      ->t('Preview of built-in font');
    $available_fonts['BUILTIN'] = $this
      ->t('PHP built-in font: <img src="@font_preview_url" alt="@title" title="@title"', [
      '@font_preview_url' => Url::fromRoute('image_captcha.font_preview', [
        'token' => 'BUILTIN',
      ])
        ->toString(),
      '@title' => $title,
    ])
      ->__toString();
    $default_fonts = _image_captcha_get_enabled_fonts();
    $conf_path = DrupalKernel::findSitePath($this
      ->getRequest());
    $form['image_captcha_fonts'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Fonts'),
      '#default_value' => $default_fonts,
      '#description' => $this
        ->t('Select the fonts to use for the text in the image CAPTCHA. Apart from the provided defaults, you can also use your own TrueType fonts (filename extension .ttf) by putting them in %fonts_library_general or %fonts_library_specific.', [
        '%fonts_library_general' => 'sites/all/libraries/fonts',
        '%fonts_library_specific' => $conf_path . '/libraries/fonts',
      ]),
      '#options' => $available_fonts,
      '#attributes' => [
        'class' => [
          'image_captcha_admin_fonts_selection',
        ],
      ],
    ];
    $form['image_captcha_font_size'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Font size'),
      '#options' => [
        9 => '9 pt - ' . $this
          ->t('tiny'),
        12 => '12 pt - ' . $this
          ->t('small'),
        18 => '18 pt',
        24 => '24 pt - ' . $this
          ->t('normal'),
        30 => '30 pt',
        36 => '36 pt - ' . $this
          ->t('large'),
        48 => '48 pt',
        64 => '64 pt - ' . $this
          ->t('extra large'),
      ],
      '#default_value' => (int) $config
        ->get('image_captcha_font_size'),
      '#description' => $this
        ->t('The font size influences the size of the image. Note that larger values make the image generation more CPU intensive.'),
    ];
  }

  // Character spacing (available for both the TrueType
  // fonts and the builtin font.
  $form['image_captcha_font_settings']['image_captcha_character_spacing'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Character spacing'),
    '#description' => $this
      ->t('Define the average spacing between characters. Note that larger values make the image generation more CPU intensive.'),
    '#default_value' => $config
      ->get('image_captcha_character_spacing'),
    '#options' => [
      '0.75' => $this
        ->t('tight'),
      '1' => $this
        ->t('normal'),
      '1.2' => $this
        ->t('wide'),
      '1.5' => $this
        ->t('extra wide'),
    ],
  ];
  return $form;
}