You are here

class SettingsForm in Improved Multi Select 8

Class SettingsForm.

Defines improved_multi_select settings form.

@package Drupal\improved_multi_select\Form

Hierarchy

Expanded class hierarchy of SettingsForm

1 string reference to 'SettingsForm'
improved_multi_select.routing.yml in ./improved_multi_select.routing.yml
improved_multi_select.routing.yml

File

src/Form/SettingsForm.php, line 15

Namespace

Drupal\improved_multi_select\Form
View source
class SettingsForm extends ConfigFormBase {

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('improved_multi_select.settings');
    $form['isall'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Replace all multi-select lists'),
      '#default_value' => $config
        ->get('isall'),
    ];
    $form['url'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Replace multi-select lists on specific pages'),
      '#description' => $this
        ->t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", [
        '%blog' => '/blog',
        '%blog-wildcard' => '/blog/*',
        '%front' => '<front>',
      ]),
      '#default_value' => $config
        ->get('url'),
    ];
    $form['selectors'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Replace multi-select with specified selectors'),
      '#description' => $this
        ->t('Enter jQuery selectors (one selector per line). Example: select[multiple]'),
      '#default_value' => $config
        ->get('selectors'),
    ];
    $form['placeholder_text'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Search box placeholder text'),
      '#description' => $this
        ->t('Enter placeholder text to appear in search box'),
      '#default_value' => $config
        ->get('placeholder_text'),
    ];
    $form['filtertype'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Filter functionality'),
      '#description' => $this
        ->t('Choose how you would like the filter textfield to function.'),
      '#options' => [
        'partial' => $this
          ->t('Partial match: Shows options that contain the filter text.'),
        'exact' => $this
          ->t('Exact match: Shows options that exactly match the filter text.'),
        'anywords' => $this
          ->t('Any words: Shows options that contain any of the individual words in the filter text. Only exact word matches count.'),
        'anywords_partial' => $this
          ->t('Any words (partial): Shows options that contain any of the individual words in the filter text. Partial word matches count.'),
        'allwords' => $this
          ->t('All words: Shows options that contain all of the individual words in the filter text (in any order). Only exact word matches count.'),
        'allwords_partial' => $this
          ->t('All words (partial): Shows options that contain all of the individual words in the filter text (in any order). Partial word matches count.'),
      ],
      '#default_value' => $config
        ->get('filtertype'),
    ];
    $form['js_regex'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Allow JavaScript regular expressions in filter'),
      '#description' => $this
        ->t('If checked, a site visitor will be able to use JavaScript regular expressions in the filter input.'),
      '#default_value' => $config
        ->get('js_regex'),
    ];
    $form['orderable'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Allow re-ordering of selected items'),
      '#description' => $this
        ->t('If checked, the user will be able to re-order the selected items using "Move up" and "Move down" buttons. Also, when adding items they will remain in the order they were added instead of keeping the order of the original field. Note: some Drupal fields (like list) will keep this order on an entity view, but will not keep this order on an entity edit form. You have to use the <em>IMS options widget</em> submodule or alter your edit form.'),
      '#default_value' => $config
        ->get('orderable'),
    ];
    $form['groupresetfilter'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Reset filter when selecting a group'),
      '#description' => $this
        ->t('If checked and a select has optgroups, when a group is selected the filter text field is cleared. If unchecked, any existing filter will be applied only to items of the selected group.'),
      '#default_value' => $config
        ->get('groupresetfilter'),
    ];
    $form['remove_required_attr'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Remove required attribute'),
      '#description' => $this
        ->t('If checked removes the required attribute from select elements to allow form submission and validation on Drupal side. Otherwise mandatory HTML5 popup not showed because html select element is hidden by JavaScript.'),
      '#default_value' => $config
        ->get('remove_required_attr'),
    ];
    $form['button_text'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Button text'),
      '#description' => $this
        ->t('Set the text used for the improved multi-select buttons.'),
    ];
    $form['button_text']['buttontext_add'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Add button'),
      '#default_value' => $config
        ->get('buttontext_add'),
    ];
    $form['button_text']['buttontext_addall'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Add all button'),
      '#default_value' => $config
        ->get('buttontext_addall'),
    ];
    $form['button_text']['buttontext_del'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Remove button'),
      '#default_value' => $config
        ->get('buttontext_del'),
    ];
    $form['button_text']['buttontext_delall'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Remove all button'),
      '#default_value' => $config
        ->get('buttontext_delall'),
    ];
    $form['button_text']['buttontext_moveup'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Move up button'),
      '#default_value' => $config
        ->get('buttontext_moveup'),
      // Hide the settings when the move buttons are disabled.
      '#states' => [
        'invisible' => [
          ':input[name="orderable"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['button_text']['buttontext_movedown'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Move down button'),
      '#default_value' => $config
        ->get('buttontext_movedown'),
      '#states' => [
        // Hide the settings when the move buttons are disabled.
        'invisible' => [
          ':input[name="orderable"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values_to_save = [
      'isall',
      'url',
      'selectors',
      'placeholder_text',
      'filtertype',
      'orderable',
      'js_regex',
      'groupresetfilter',
      'remove_required_attr',
      'buttontext_add',
      'buttontext_addall',
      'buttontext_del',
      'buttontext_delall',
      'buttontext_moveup',
      'buttontext_movedown',
    ];
    $values = $form_state
      ->getValues();
    foreach ($values as $key => $value) {
      if (array_search($key, $values_to_save) === FALSE) {
        unset($values[$key]);
      }
    }
    $this
      ->config('improved_multi_select.settings')
      ->setData($values)
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
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.
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.
SettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
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.