You are here

class ProtectedSubmissionsForm in Protected Submissions 8

ProtectedSubmissionsForm class.

Hierarchy

Expanded class hierarchy of ProtectedSubmissionsForm

1 string reference to 'ProtectedSubmissionsForm'
protected_submissions.routing.yml in ./protected_submissions.routing.yml
protected_submissions.routing.yml

File

src/Form/ProtectedSubmissionsForm.php, line 14

Namespace

Drupal\protected_submissions\Form
View source
class ProtectedSubmissionsForm extends ConfigFormBase {

  /**
   * Protected $moduleHandler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

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

  /**
   * ProtectedSubmissionsForm constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The moduler handler.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $moduleHandler) {
    parent::__construct($config_factory);
    $this->moduleHandler = $moduleHandler;
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'protected_submissions_form';
  }

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

    // Form constructor.
    $form = parent::buildForm($form, $form_state);

    // Default settings.
    $config = $this
      ->config('protected_submissions.settings');
    $language_scripts = [
      'Adlam' => 'Adlam',
      'Ahom' => 'Ahom',
      'Anatolian Hieroglyphs' => 'Anatolian Hieroglyphs',
      'Arabic' => 'Arabic',
      'Aramaic' => 'Aramaic',
      'Armenian' => 'Armenian',
      'Avestan' => 'Avestan',
      'Balinese' => 'Balinese',
      'Bamum' => 'Bamum',
      'Bassa Vah' => 'Bassa Vah',
      'Batak' => 'Batak',
      'Bengali and Assamese' => 'Bengali and Assamese',
      'Bhaiksuki' => 'Bhaiksuki',
      'Bopomofo' => 'Bopomofo',
      'Brahmi' => 'Brahmi',
      'Buginese' => 'Buginese',
      'Buhid' => 'Buhid',
      'Canadian Aboriginal' => 'Canadian Aboriginal',
      'Carian' => 'Carian',
      'Caucasian Albanian' => 'Caucasian Albanian',
      'Chakma' => 'Chakma',
      'Cham' => 'Cham',
      'Cherokee' => 'Cherokee',
      'Chorasmian' => 'Chorasmian',
      'CJK' => 'CJK',
      'Coptic' => 'Coptic',
      'Cuneiform' => 'Cuneiform',
      'Currency Symbols' => 'Currency Symbols',
      'Cypriot Syllabary' => 'Cypriot Syllabary',
      'Cyrillic' => 'Cyrillic',
      'Deseret' => 'Deseret',
      'Devanagari' => 'Devanagari',
      'Diacritical Marks' => 'Diacritical Marks',
      'Dives Akuru' => 'Dives Akuru',
      'Dogra' => 'Dogra',
      'Egyptian Hieroglyphs' => 'Egyptian Hieroglyphs',
      'Elbasan' => 'Elbasan',
      'Elymaic' => 'Elymaic',
      'Ethiopic' => 'Ethiopic',
      'Georgian' => 'Georgian',
      'Glagolitic' => 'Glagolitic',
      'Gothic' => 'Gothic',
      'Grantha' => 'Grantha',
      'Greek' => 'Greek',
      'Gujarati' => 'Gujarati',
      'Gunjala Gondi' => 'Gunjala Gondi',
      'Gurmukhi' => 'Gurmukhi',
      'Hangul Jamo' => 'Hangul Jamo',
      'Hangul Syllables' => 'Hangul Syllables',
      'Hanifi Rohingya' => 'Hanifi Rohingya',
      'Hanunoo' => 'Hanunoo',
      'Hatran' => 'Hatran',
      'Hebrew' => 'Hebrew',
      'Hiragana' => 'Hiragana',
      'Javanese' => 'Javanese',
      'Kaithi' => 'Kaithi',
      'Kana' => 'Kana',
      'Kanbun' => 'Kanbun',
      'Kannada' => 'Kannada',
      'Katakana' => 'Katakana',
      'Kayah Li' => 'Kayah Li',
      'Kharoshthi' => 'Kharoshthi',
      'Khitan Small Script' => 'Khitan Small Script',
      'Khmer' => 'Khmer',
      'Khojki' => 'Khojki',
      'Khudawadi' => 'Khudawadi',
      'Lao' => 'Lao',
      'Latin' => 'Latin',
      'Lepcha' => 'Lepcha',
      'Limbu' => 'Limbu',
      'Lisu' => 'Lisu',
      'Lycian' => 'Lycian',
      'Lydian' => 'Lydian',
      'Mahajani' => 'Mahajani',
      'Makasar' => 'Makasar',
      'Malayalam' => 'Malayalam',
      'Mandaic' => 'Mandaic',
      'Manichaean' => 'Manichaean',
      'Marchen' => 'Marchen',
      'Masaram Gondi' => 'Masaram Gondi',
      'Medefaidrin' => 'Medefaidrin',
      'Meetei Mayek' => 'Meetei Mayek',
      'Mende Kikaku' => 'Mende Kikaku',
      'Meroitic' => 'Meroitic',
      'Miao' => 'Miao',
      'Modi' => 'Modi',
      'Modifier Letters' => 'Modifier Letters',
      'Mongolian' => 'Mongolian',
      'Mro' => 'Mro',
      'Myanmar' => 'Myanmar',
      'Multani' => 'Multani',
      'Nabataean' => 'Nabataean',
      'Nandinagari' => 'Nandinagari',
      'New Tai Lue' => 'New Tai Lue',
      'Newa' => 'Newa',
      'Nko' => 'Nko',
      'Nushu' => 'Nushu',
      'Nyiakeng Puachue Hmong' => 'Nyiakeng Puachue Hmong',
      'Ogham' => 'Ogham',
      'Ol Chiki' => 'Ol Chiki',
      'Old Hungarian' => 'Old Hungarian',
      'Old Italic' => 'Old Italic',
      'Old Permic' => 'Old Permic',
      'Old Persian' => 'Old Persian',
      'Old Sogdian' => 'Old Sogdian',
      'Old Turkic' => 'Old Turkic',
      'Oriya' => 'Oriya',
      'Osage' => 'Osage',
      'Osmanya' => 'Osmanya',
      'Pahawh Hmong' => 'Pahawh Hmong',
      'Pahlavi' => 'Pahlavi',
      'Palmyrene' => 'Palmyrene',
      'Parthian' => 'Parthian',
      'Pau Cin Hau' => 'Pau Cin Hau',
      'Phags Pa' => 'Phags Pa',
      'Phoenician' => 'Phoenician',
      'Rejang' => 'Rejang',
      'Runic' => 'Runic',
      'Samaritan' => 'Samaritan',
      'Saurashtra' => 'Saurashtra',
      'Sharada' => 'Sharada',
      'Shavian' => 'Shavian',
      'Siddham' => 'Siddham',
      'Sinhala' => 'Sinhala',
      'Sogdian' => 'Sogdian',
      'Sora Sompeng' => 'Sora Sompeng',
      'Soyombo' => 'Soyombo',
      'Sundanese' => 'Sundanese',
      'Superscripts and Subscripts' => 'Superscripts and Subscripts',
      'Syloti Nagri' => 'Syloti Nagri',
      'Syriac' => 'Syriac',
      'Tagalog' => 'Tagalog',
      'Tagbanwa' => 'Tagbanwa',
      'Tai Le' => 'Tai Le',
      'Tai Tham' => 'Tai Tham',
      'Tai Viet' => 'Tai Viet',
      'Takri' => 'Takri',
      'Tamil' => 'Tamil',
      'Tangut' => 'Tangut',
      'Telugu' => 'Telugu',
      'Thaana' => 'Thaana',
      'Thai' => 'Thai',
      'Tibetan' => 'Tibetan',
      'Tifinagh' => 'Tifinagh',
      'Tirhuta' => 'Tirhuta',
      'Ugaritic' => 'Ugaritic',
      'Vai' => 'Vai',
      'Vedic Extensions' => 'Vedic Extensions',
      'Wancho' => 'Wancho',
      'Warang Citi' => 'Warang Citi',
      'Yezidi' => 'Yezidi',
      'Yi' => 'Yi',
      'Yijing' => 'Yijing',
      'Zanabazar Square' => 'Zanabazar Square',
    ];
    $form['allowed_scripts'] = [
      '#type' => 'checkboxes',
      '#options' => $language_scripts,
      '#default_value' => $config
        ->get('protected_submissions.allowed_scripts'),
      '#title' => $this
        ->t('Allowed language scripts'),
      '#required' => TRUE,
      '#description' => $this
        ->t('Select language scripts allowed for submission.'),
    ];
    $form['check_quantity'] = [
      '#type' => 'textfield',
      '#default_value' => $config
        ->get('protected_submissions.check_quantity'),
      '#title' => $this
        ->t('Number of characters to validate'),
      '#size' => 5,
      '#required' => TRUE,
      '#description' => $this
        ->t('Spam texts can be constructed using mixed characters from multiple languages.
      Checking each character would cause performance overhead, so we validate only randomly selected characters.
      Indicate how many characters from submitted text should be validated for allowed language scripts.'),
    ];

    // Page title field.
    $form['reject_message'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Reject message:'),
      '#default_value' => $config
        ->get('protected_submissions.reject_message'),
      '#required' => TRUE,
      '#description' => $this
        ->t('Enter a message to display when submission of undesired pattern is attempted.'),
    ];

    // Source text field.
    $form['reject_patterns'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Reject patterns:'),
      '#default_value' => $config
        ->get('protected_submissions.reject_patterns'),
      '#required' => TRUE,
      '#description' => $this
        ->t('Enter words or patterns to reject, separating them with commas or new lines.'),
    ];

    // Give users option to enable/disable logging if Database Logging is on.
    if ($this->moduleHandler
      ->moduleExists('dblog')) {
      $form['log_rejected'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Log rejected submissions'),
        '#default_value' => $config
          ->get('protected_submissions.log_rejected'),
        '#required' => FALSE,
        '#description' => $this
          ->t('Checking on this option could potentially flood <a href="@log">the website\'s log</a>, so use this feature only if you need to analyze the rejected submissions.', [
          '@log' => '/admin/reports/dblog',
        ]),
      ];
    }
    $form['#attached']['library'][] = 'protected_submissions/protected_submissions';
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if (strlen($form_state
      ->getValue('reject_message')) == 0) {
      $form_state
        ->setErrorByName('user', $this
        ->t('You must enter a reject message to be displayed when an undesired pattern is detected on submission.'));
    }
    elseif (strlen($form_state
      ->getValue('reject_patterns')) == 0) {
      $form_state
        ->setErrorByName('user', $this
        ->t('You must enter the pattern(s) to trigger a submission rejection when matched.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('protected_submissions.settings');
    $config
      ->set('protected_submissions.allowed_scripts', $form_state
      ->getValue('allowed_scripts'));
    $config
      ->set('protected_submissions.check_quantity', $form_state
      ->getValue('check_quantity'));
    $config
      ->set('protected_submissions.reject_message', $form_state
      ->getValue('reject_message'));
    $config
      ->set('protected_submissions.reject_patterns', $form_state
      ->getValue('reject_patterns'));
    $config
      ->set('protected_submissions.log_rejected', $form_state
      ->getValue('log_rejected'));
    $config
      ->save();
    return parent::submitForm($form, $form_state);
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
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 protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
ProtectedSubmissionsForm::$moduleHandler protected property Protected $moduleHandler.
ProtectedSubmissionsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
ProtectedSubmissionsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
ProtectedSubmissionsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
ProtectedSubmissionsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ProtectedSubmissionsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
ProtectedSubmissionsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
ProtectedSubmissionsForm::__construct public function ProtectedSubmissionsForm constructor. Overrides ConfigFormBase::__construct
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.