You are here

class CaptchaQuestionsSettingsForm in Captcha Questions 8

Displays the captcha_questions settings form.

Hierarchy

Expanded class hierarchy of CaptchaQuestionsSettingsForm

1 string reference to 'CaptchaQuestionsSettingsForm'
captcha_questions.routing.yml in ./captcha_questions.routing.yml
captcha_questions.routing.yml

File

src/Form/CaptchaQuestionsSettingsForm.php, line 19

Namespace

Drupal\captcha_questions\Form
View source
class CaptchaQuestionsSettingsForm extends ConfigFormBase {

  /**
   * ModuleHandler services object.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  private $moduleHandler;

  /**
   * The Database Connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * CaptchaQuestionsSettingsForm constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection to be used.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, Connection $database, MessengerInterface $messenger) {
    parent::__construct($config_factory);
    $this->moduleHandler = $module_handler;
    $this->database = $database;
    $this->messenger = $messenger;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('module_handler'), $container
      ->get('database'), $container
      ->get('messenger'));
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'captcha_questions.settings',
    ];
  }

  /**
   * Implements \Drupal\Core\Form\FormInterface::getFormID().
   */
  public function getFormId() {
    return 'captcha_questions_settings_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['#attached']['library'][] = 'core/drupal.ajax';
    $form['description'] = [
      '#type' => 'markup',
      '#markup' => $this
        ->t("This is just a very simple mechanism to protect from automated bots. To make it as easy for real humans, consider providing the answer with the question. Example '<em>What is Mickeys last name? It's \"Mouse\"</em>'. You could also do simply 'What is 1+1?', but some bots may figure that out."),
    ];
    $form['configuration'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Logging options'),
      // Controls the HTML5 'open' attribute. Defaults to FALSE.
      '#open' => TRUE,
    ];

    // Disables checkbox if dblog module is not enabled.
    $form['configuration']['captcha_questions_watchdog'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Log to watchdog'),
      '#description' => $this
        ->t('Check this box to log all failed submissions to watchdog'),
      '#default_value' => $this
        ->config('captcha_questions.settings')
        ->get('captcha_questions_watchdog'),
      '#disabled' => $this->moduleHandler
        ->moduleExists('dblog') ? FALSE : TRUE,
    ];

    // Disables checkbox if captcha_questions_dblog module is not enabled.
    $form['configuration']['captcha_questions_dblog'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable logging internal database table'),
      '#description' => $this
        ->t('Check this box to log all failed submissions in separate database table'),
      '#default_value' => $this
        ->config('captcha_questions.settings')
        ->get('captcha_questions_dblog'),
      '#disabled' => $this->moduleHandler
        ->moduleExists('captcha_questions_dblog') ? FALSE : TRUE,
    ];
    $form['settings'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('CAPTCHA'),
      // Controls the HTML5 'open' attribute. Defaults to FALSE.
      '#open' => TRUE,
    ];
    $question = $this
      ->config('captcha_questions.settings')
      ->get('captcha_questions_question');
    $form['settings']['captcha_questions_question'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Question to ask'),
      '#default_value' => $question,
      '#maxlength' => 256,
      '#required' => TRUE,
    ];
    $answers = $this
      ->config('captcha_questions.settings')
      ->get('captcha_questions_answers');
    $form['settings']['captcha_questions_answers'] = [
      '#type' => 'textarea',
      '#title' => 'Accepted answer(s)',
      '#default_value' => implode("\n", $answers),
      '#description' => $this
        ->t('Please provide one or more accepted answers, one per line. Answers are case-insensitive'),
      '#format' => NULL,
      '#required' => TRUE,
    ];
    $description = $this
      ->config('captcha_questions.settings')
      ->get('captcha_questions_description');
    $form['settings']['captcha_questions_description'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Description'),
      '#default_value' => $description,
      '#maxlength' => 256,
    ];
    $form['forms'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Form protection'),
      // Controls the HTML5 'open' attribute. Defaults to FALSE.
      '#open' => TRUE,
      '#prefix' => '<div id="captcha-questions-form-id-wrapper">',
      '#suffix' => '</div>',
    ];

    // Getting form_ids.
    if (!$form_state
      ->getValue('form_ids')) {
      $form_state
        ->setValue('form_ids', $this
        ->captchaQuestionsGetFormIds());
    }

    // Adding new custom form_ids.
    if ($form_state
      ->getValue('add_form_name') != '') {
      $form_ids = $form_state
        ->getValue('form_ids');
      $selected_all_form_ids = $form_state
        ->getValue('captcha_questions_form_ids');
      $selected_all_form_ids = array_flip($selected_all_form_ids);
      $all_form_ids = array_merge($form_ids, $selected_all_form_ids);
      $all_form_ids[] = $form_state
        ->getValue('add_form_name');
      $form_state
        ->setValue('form_ids', $all_form_ids);
    }

    // Getting previously selected form_ids.
    $form_state
      ->setValue('selected_form_ids', $this
      ->config('captcha_questions.settings')
      ->get('captcha_questions_form_ids'));

    // Create select list with previously selected form_ids as selected.
    $form['forms']['captcha_questions_form_ids'] = [
      '#type' => 'checkboxes',
      '#options' => array_combine($form_state
        ->getValue('form_ids'), $form_state
        ->getValue('form_ids')),
      '#default_value' => array_intersect($form_state
        ->getValue('selected_form_ids'), $form_state
        ->getValue('form_ids')),
      '#title' => $this
        ->t('What forms should be protected?'),
    ];

    // Adding node title and link to webform_ids options text.
    if ($this->moduleHandler
      ->moduleExists('webform')) {
      $webform_forms = $this
        ->captchaQuestionsGetWebforms();
      foreach ($webform_forms as $webform_id => $webform) {
        $url = Url::fromUri('base://form/' . $webform['id']);
        $webform_node_link = Link::fromTextAndUrl($webform['title'], $url)
          ->toString();
        $webform_form_id_option_text = $webform_id . ' (' . $webform_node_link . ')';
        $form['forms']['captcha_questions_form_ids']['#options'][$webform_id] = $webform_form_id_option_text;
      }
    }
    $form['forms']['add_form_name'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Custom form_id'),
      '#default_value' => '',
      '#maxlength' => 256,
    ];
    $form['forms']['add_form'] = [
      '#type' => 'button',
      '#value' => $this
        ->t('Add custom form_id'),
      '#submit' => [
        'captchaQuestionsAddForm',
      ],
      '#ajax' => [
        'callback' => '::captchaQuestionsAddFormCallback',
        'wrapper' => 'captcha-questions-form-id-wrapper',
      ],
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

    // If question is specified without answer, ask for answer.
    if (!empty($form_state
      ->getValue('captcha_questions_question')) && empty($form_state
      ->getValue('captcha_questions_answers'))) {
      $form_state
        ->setErrorByName('captcha_questions_answer', $this
        ->t('Please provide an answer'));
    }

    // If answer given, but no question, ask for question.
    if (empty($form_state
      ->getValue('captcha_questions_question')) && !empty($form_state
      ->getValue('captcha_questions_answers'))) {
      $form_state
        ->setErrorByName('captcha_questions_question', $this
        ->t('Please add a question'));
    }
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // If question, answer and description are all empty, delete the variables.
    // The last variable, captcha_questions_form_ids are removed on uninstall.
    if (empty($form_state
      ->getValue('captcha_questions_question')) && empty($form_state
      ->getValue('captcha_questions_answer'))) {
      $this
        ->config('captcha_questions.settings')
        ->delete('captcha_questions_question');
      $this
        ->config('captcha_questions.settings')
        ->delete('captcha_questions_answer');
      $this
        ->config('captcha_questions.settings')
        ->delete('captcha_questions_description');
    }
    if (empty($form_state
      ->getValue('captcha_questions_question')) && empty($form_state
      ->getValue('captcha_questions_answers'))) {
      $this
        ->config('captcha_questions.settings')
        ->delete('captcha_questions_question');
      $this
        ->config('captcha_questions.settings')
        ->delete('captcha_questions_answers');
      $this
        ->config('captcha_questions.settings')
        ->delete('captcha_questions_description');
    }

    // Split answers into arrays and set variable manually.
    $answers = explode("\n", $form_state
      ->getValue('captcha_questions_answers'));
    $answers = array_map('trim', $answers);
    asort($answers);
    $this
      ->config('captcha_questions.settings')
      ->set('captcha_questions_answers', $answers);
    $form_state
      ->unsetValue('captcha_questions_answers');
    $count_protected_form_ids = 0;

    // Counting number of selected form_ids, removing unselected form_ids.
    foreach ($form_state
      ->getValue('captcha_questions_form_ids') as $form_id => $value) {
      if ($form_id === $value) {
        $count_protected_form_ids++;
      }
      else {
        $captcha_questions_form_ids = $form_state
          ->getValue('captcha_questions_form_ids');
        unset($captcha_questions_form_ids[$form_id]);
        $form_state
          ->setValue('captcha_questions_form_ids', $captcha_questions_form_ids);
      }
    }
    if ($count_protected_form_ids == 0) {
      $this->messenger
        ->addMessage($this
        ->t('No forms selected'));
    }
    else {
      $message = $this
        ->formatPlural($count_protected_form_ids, '1 form protected', '@count forms protected');
      $this->messenger
        ->addMessage($message);
    }
    $this
      ->config('captcha_questions.settings')
      ->set('captcha_questions_watchdog', $form_state
      ->getValue('captcha_questions_watchdog'))
      ->set('captcha_questions_dblog', $form_state
      ->getValue('captcha_questions_dblog'))
      ->set('captcha_questions_question', $form_state
      ->getValue('captcha_questions_question'))
      ->set('captcha_questions_description', $form_state
      ->getValue('captcha_questions_description'))
      ->set('captcha_questions_form_ids', $form_state
      ->getValue('captcha_questions_form_ids'))
      ->set('add_form_name', $form_state
      ->getValue('add_form_name'))
      ->save();
    parent::submitForm($form, $form_state);
  }

  /**
   * Helper function to make a list of candidates of form ids.
   */
  public function captchaQuestionsGetFormIds() {

    // Some form nids that might exist on the site.
    $form_ids = [
      'contact_site_form',
      'contact_personal_form',
      'user_register_form',
      'user_pass',
      'user_login_form',
      'user_login_block',
      'forum_node_form',
    ];

    // Fetching form ids for all node types.
    foreach (node_type_get_names() as $type => $name) {
      $form_ids[] = 'comment_node_' . $type . '_form';
    }

    // Fetching webform form_ids.
    if ($this->moduleHandler
      ->moduleExists('webform')) {
      $webform_forms = $this
        ->captchaQuestionsGetWebforms();
      foreach ($webform_forms as $webform_id => $webform) {
        $form_ids[] = $webform_id;
      }
    }

    // Adding custom form_ids. This will add all selected forms.
    $custom_form_ids = $this
      ->config('captcha_questions.settings')
      ->get('captcha_questions_form_ids');
    foreach ($custom_form_ids as $custom_form_id) {
      $form_ids[] = $custom_form_id;
    }

    // Removing possible duplicates from last step.
    $form_ids = array_unique($form_ids);
    return $form_ids;
  }

  /**
   * Helper function to find webform form_ids.
   *
   * Adapted from the function webform_mollom_form_list() in webform.module.
   * Returns array keyed by webform form_ids and title, id of the webform.
   */
  public function captchaQuestionsGetWebforms() {
    $forms = [];
    foreach (Webform::loadMultiple() as $webform_obj) {
      $form_id = 'webform_submission_' . $webform_obj
        ->id() . '_form';
      $forms[$form_id] = [
        'title' => $this
          ->t('@name form', [
          '@name' => $webform_obj
            ->label(),
        ]),
        'id' => $webform_obj
          ->id(),
      ];
    }
    return $forms;
  }

  /**
   * Callback for ajax-enabled button.
   *
   * Selects and returns the fieldset with the names in it.
   */
  public function captchaQuestionsAddFormCallback(array &$form, FormStateInterface $form_state) {
    return $form['forms'];
  }

  /**
   * Submit handler for the "add-one-more" button.
   *
   * Causes a rebuild.
   */
  public function captchaQuestionsAddForm(array &$form, FormStateInterface $form_state) {
    $form_state
      ->setRebuild();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CaptchaQuestionsSettingsForm::$database protected property The Database Connection.
CaptchaQuestionsSettingsForm::$messenger protected property The messenger. Overrides MessengerTrait::$messenger
CaptchaQuestionsSettingsForm::$moduleHandler private property ModuleHandler services object.
CaptchaQuestionsSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
CaptchaQuestionsSettingsForm::captchaQuestionsAddForm public function Submit handler for the "add-one-more" button.
CaptchaQuestionsSettingsForm::captchaQuestionsAddFormCallback public function Callback for ajax-enabled button.
CaptchaQuestionsSettingsForm::captchaQuestionsGetFormIds public function Helper function to make a list of candidates of form ids.
CaptchaQuestionsSettingsForm::captchaQuestionsGetWebforms public function Helper function to find webform form_ids.
CaptchaQuestionsSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
CaptchaQuestionsSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
CaptchaQuestionsSettingsForm::getFormId public function Implements \Drupal\Core\Form\FormInterface::getFormID(). Overrides FormInterface::getFormId
CaptchaQuestionsSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
CaptchaQuestionsSettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
CaptchaQuestionsSettingsForm::__construct public function CaptchaQuestionsSettingsForm constructor. Overrides ConfigFormBase::__construct
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.