class ModuleConfigForm in Field Label 8
Class ModuleConfigForm.
Configuration settings for Field Label module.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\field_label\Form\ModuleConfigForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of ModuleConfigForm
1 string reference to 'ModuleConfigForm'
File
- src/
Form/ ModuleConfigForm.php, line 15
Namespace
Drupal\field_label\FormView 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><{{ <b>label_tag</b>|default('div') }}{{ title_attributes }}>{{ label }}</{{ <b>label_tag</b>|default('div') }}></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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
ModuleConfigForm:: |
protected | property | Drupal\Core\Config\ConfigManager definition. | |
ModuleConfigForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
ModuleConfigForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
ModuleConfigForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
ModuleConfigForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ModuleConfigForm:: |
constant | Config settings. | ||
ModuleConfigForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
ModuleConfigForm:: |
public | function |
Constructs ModuleConfigForm object. Overrides ConfigFormBase:: |
|
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |