You are here

class ModuleConfigForm in Field Label 8

Class ModuleConfigForm.

Configuration settings for Field Label module.

Hierarchy

Expanded class hierarchy of ModuleConfigForm

1 string reference to 'ModuleConfigForm'
field_label.routing.yml in ./field_label.routing.yml
field_label.routing.yml

File

src/Form/ModuleConfigForm.php, line 15

Namespace

Drupal\field_label\Form
View source
class ModuleConfigForm extends ConfigFormBase {

  /**
   * Drupal\Core\Config\ConfigManager definition.
   *
   * @var \Drupal\Core\Config\ConfigManager
   */
  protected $configManager;

  /**
   * Config settings.
   *
   * @var string
   */
  const SETTINGS = 'field_label.settings';

  /**
   * Constructs ModuleConfigForm object.
   *
   * @param \Drupal\Core\Config\ConfigManager $config_manager
   *   The factory for configuration objects.
   */
  public function __construct(ConfigManager $config_manager) {

    // parent::__construct($config_factory);
    $this->configManager = $config_manager;
  }

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

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      static::SETTINGS,
    ];
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config(static::SETTINGS);

    // Field state helper.
    $depends_on = function ($name, $states) {
      $conditions = [];
      foreach ($states as $state) {
        $conditions[$state] = [
          ':input[name="' . $name . '"]' => [
            'checked' => TRUE,
          ],
        ];
      }
      return $conditions;
    };

    // Label value settings.
    $form['label_value'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Label Values'),
    ];
    $form['label_value']['label_value_enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable label value'),
      '#description' => $this
        ->t('Allow authorized users to change the field label text.'),
      '#default_value' => $config
        ->get('label_value_enabled'),
    ];

    // Label class settings.
    $form['label_class'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Label Classes'),
    ];
    $form['label_class']['label_class_enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable label classes field'),
      '#description' => $this
        ->t('Allow authorized users to add "free-form" list of CSS classes to the label tag.'),
      '#default_value' => $config
        ->get('label_class_enabled'),
    ];
    $form['label_class']['label_class_select_enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable label class select list'),
      '#description' => $this
        ->t('Allow authorized users to select a CSS class from a list of configured classes.'),
      '#default_value' => $config
        ->get('label_class_select_enabled'),
    ];

    // Classs list container.
    $form['label_class']['container'] = [
      '#type' => 'container',
      '#states' => $depends_on('label_class_select_enabled', [
        'visible',
      ]),
    ];
    $form['label_class']['container']['class_list'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Class list'),
      '#description' => $this
        ->t('Enter one or more classes on each line in the format: <code>.classA.classB|Label</code>. <b>Example:</b><br><code>.title|Title<br>.title.title--large|Large title</code>'),
      '#default_value' => $config
        ->get('class_list'),
      '#states' => $depends_on('label_class_select_enabled', [
        'required',
      ]),
    ];

    // Label tag settings.
    $form['label_tag'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Label Tags'),
    ];
    $form['label_tag']['label_tag_enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable label tag selection'),
      '#description' => $this
        ->t('Allow authorized users to select a wrapper from a list of allowed HTML tags.'),
      '#default_value' => $config
        ->get('label_tag_enabled'),
    ];
    $form['label_tag']['container'] = [
      '#type' => 'container',
      '#states' => $depends_on('label_tag_enabled', [
        'visible',
      ]),
    ];
    $form['label_tag']['container']['allowed_tags'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Allowed label tags'),
      '#description' => $this
        ->t('A space-separated list of HTML tags that should appear in the "Label tag" setting dropdown.'),
      '#default_value' => implode(' ', array_filter($config
        ->get('allowed_tags'))),
      '#states' => $depends_on('label_tag_enabled', [
        'required',
      ]),
    ];
    $form['label_tag']['container']['instructions'] = [
      '#type' => 'html_tag',
      '#tag' => 'p',
      '#value' => $this
        ->t("<strong>IMPORTANT:</strong><br>If your theme provides/overrides field templates, you may need to modify them to use Field Label's <code><b>label_tag</b></code> twig variable. For example:<br><code>&lt;{{ <b>label_tag</b>|default('div') }}{{ title_attributes }}&gt;{{ label }}&lt;/{{ <b>label_tag</b>|default('div') }}&gt;</code>"),
    ];
    return parent::buildForm($form, $form_state);
  }

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

    // Retrieve the configuration.
    $config = $this
      ->configFactory()
      ->getEditable(static::SETTINGS);

    // Set all feature 'enabled' values.
    $config
      ->set('label_value_enabled', $form_state
      ->getValue('label_value_enabled'))
      ->set('label_class_enabled', $form_state
      ->getValue('label_class_enabled'))
      ->set('label_class_select_enabled', $form_state
      ->getValue('label_class_select_enabled'))
      ->set('class_list', $form_state
      ->getValue('class_list'))
      ->set('label_tag_enabled', $form_state
      ->getValue('label_tag_enabled'));

    // Process allowed tags even if disabled to keep defaults.
    $tags = array_filter(explode(' ', $form_state
      ->getValue('allowed_tags')));
    $config
      ->set('allowed_tags', $tags);
    $config
      ->save();
    parent::submitForm($form, $form_state);
  }

}

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.
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.
ModuleConfigForm::$configManager protected property Drupal\Core\Config\ConfigManager definition.
ModuleConfigForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
ModuleConfigForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
ModuleConfigForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
ModuleConfigForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ModuleConfigForm::SETTINGS constant Config settings.
ModuleConfigForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
ModuleConfigForm::__construct public function Constructs ModuleConfigForm object. 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.