You are here

function _image_captcha_get_available_fonts_from_directories in CAPTCHA 8

Same name and namespace in other branches
  1. 6.2 image_captcha/image_captcha.admin.inc \_image_captcha_get_available_fonts_from_directories()
  2. 7 image_captcha/image_captcha.admin.inc \_image_captcha_get_available_fonts_from_directories()

Helper function to get fonts from the given directories.

Parameters

array|null $directories: (Optional) an array of directories to recursively search through, if not given, the default directories will be used.

Return value

array Fonts file objects (with fields 'name', 'basename' and 'filename'), keyed on the sha256 hash of the font path (to have an easy token that can be used in an url without en/decoding issues).

2 calls to _image_captcha_get_available_fonts_from_directories()
ImageCaptchaSettingsForm::settingsDotSection in image_captcha/src/Form/ImageCaptchaSettingsForm.php
Form elements for the font specific setting.
_image_captcha_get_font_uri in image_captcha/image_captcha.module
Helper function to get font(s).

File

image_captcha/image_captcha.module, line 82
Implements image CAPTCHA for use with the CAPTCHA module.

Code

function _image_captcha_get_available_fonts_from_directories($directories = NULL) {

  // If no fonts directories are given: use the default.
  if ($directories === NULL) {
    $request = \Drupal::service('request_stack')
      ->getCurrentRequest();
    $directories = [
      drupal_get_path('module', 'image_captcha') . '/fonts',
      'sites/all/libraries/fonts',
      DrupalKernel::findSitePath($request) . '/libraries/fonts',
    ];
  }

  // Collect the font information.
  $fonts = [];
  foreach ($directories as $directory) {
    if (\Drupal::service('file_system')
      ->prepareDirectory($directory)) {
      $files = \Drupal::service('file_system')
        ->scanDirectory($directory, '/\\.[tT][tT][fF]$/');
      foreach ($files as $filename => $font) {
        $fonts[hash('sha256', $filename)] = (array) $font;
      }
    }
  }
  return $fonts;
}