You are here

function _random_captcha_type_get_enabled_types in CAPTCHA Pack 8

Same name and namespace in other branches
  1. 5 random_captcha_type/random_captcha_type.module \_random_captcha_type_get_enabled_types()
  2. 6 random_captcha_type/random_captcha_type.inc \_random_captcha_type_get_enabled_types()
  3. 7 random_captcha_type/random_captcha_type.module \_random_captcha_type_get_enabled_types()

Function for getting the enabled CAPTCHA types.

Alternative of variable_get('random_captcha_type_enabled_types', ...) with sensible default value. For use as #default_value of checkboxes widget. Returns an array mapping "$module/$type" to "$module/$type" for the enabled types.

2 calls to _random_captcha_type_get_enabled_types()
RandomCaptchaTypeSettingsForm::buildForm in random_captcha_type/src/Form/RandomCaptchaTypeSettingsForm.php
Form constructor.
random_captcha_type_captcha in random_captcha_type/random_captcha_type.module
Implements hook_captcha().

File

random_captcha_type/random_captcha_type.module, line 90
Contains general functionality of the module.

Code

function _random_captcha_type_get_enabled_types() {

  /** @var \Drupal\Core\Config\Config $config */
  $config = \Drupal::service('config.factory')
    ->getEditable('random_captcha_type.settings');
  $enabled_types = [];
  $_enabled_types = $config
    ->get('random_captcha_type_enabled_types');
  if ($_enabled_types === NULL) {
    foreach (_random_captcha_type_get_all_types() as $key => $value) {
      $enabled_types[$key] = $key;
    }
    $config
      ->set('random_captcha_type_enabled_types', $enabled_types);
  }
  else {
    foreach ($_enabled_types as $key => $value) {
      if ($value) {
        list($module, $type) = explode('/', $value);

        // Check if type is still available.
        $list = \Drupal::moduleHandler()
          ->invoke($module, 'captcha', [
          'list',
        ]);
        if ($list && in_array($type, $list)) {
          $enabled_types[$key] = $value;
        }
      }
    }
  }
  return $enabled_types;
}