class MessageBannerSettingsForm in Message Banner 8
The message banner settings form.
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\message_banner\Form\MessageBannerSettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of MessageBannerSettingsForm
1 string reference to 'MessageBannerSettingsForm'
File
- src/
Form/ MessageBannerSettingsForm.php, line 15
Namespace
Drupal\message_banner\FormView source
class MessageBannerSettingsForm extends ConfigFormBase {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The state storage service.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() : array {
return [
'message_banner.settings',
];
}
/**
* {@inheritdoc}
*/
public function getFormId() : string {
return 'message_banner.settings';
}
/**
* Constructs a message banner settings form.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\State\StateInterface $state
* The state storage service.
*/
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, StateInterface $state) {
parent::__construct($config_factory);
$this->moduleHandler = $module_handler;
$this->state = $state;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('module_handler'), $container
->get('state'));
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) : array {
$config = $this
->config('message_banner.settings');
$form['banner_enabled'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable banner'),
'#default_value' => $config
->get('banner_enabled') ?: FALSE,
];
$form['message_banner'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Message banner settings'),
'#states' => [
'visible' => [
':input[name="banner_enabled"]' => [
'checked' => TRUE,
],
],
],
];
$form['message_banner']['banner_enabled_on_admin_routes'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable banner on admin routes'),
'#description' => $this
->t('If not checked, the banner will only appear on non-admin routes.'),
'#default_value' => $config
->get('banner_enabled_on_admin_routes') ?: FALSE,
'#states' => [
'visible' => [
':input[name="banner_enabled"]' => [
'checked' => TRUE,
],
],
],
];
$form['message_banner']['banner_show_again_minutes'] = [
'#type' => 'number',
'#step' => 1,
'#title' => $this
->t('Minutes before showing banner after dismissing it'),
'#default_value' => $config
->get('banner_show_again_minutes'),
'#description' => $this
->t('The number of minutes to elapse after dimissing the banner before showing it again (0 to disable).'),
];
$form['message_banner']['banner_color'] = [
'#type' => 'select',
'#title' => $this
->t('Banner color'),
'#description' => $this
->t('Choose the background color for the banner.'),
'#options' => $this
->getBannerColors(),
'#default_value' => $config
->get('banner_color') ?: NULL,
'#states' => [
'required' => [
':input[name="banner_enabled"]' => [
'checked' => TRUE,
],
],
],
];
$form['message_banner']['banner_text'] = [
'#type' => 'text_format',
'#title' => $this
->t('Message'),
'#description' => $this
->t('This message will be shown to every site visitor, so make sure it does not contain any sensitive information!'),
'#default_value' => $config
->get('banner_text.value') ?: '',
'#format' => $config
->get('banner_text.format') ?: 'basic_html',
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this
->config('message_banner.settings')
->set('banner_enabled', $form_state
->getValue('banner_enabled'))
->set('banner_enabled_on_admin_routes', $form_state
->getValue('banner_enabled_on_admin_routes'))
->set('banner_color', $form_state
->getValue('banner_color'))
->set('banner_text', $form_state
->getValue('banner_text'))
->set('banner_show_again_minutes', $form_state
->getValue('banner_show_again_minutes'))
->save();
// Save the save time as a state value, so that config is not affected.
$this->state
->set('banner_saved', time());
return parent::submitForm($form, $form_state);
}
/**
* Gets the available colors for the message banner.
*
* @see hook_message_banner_colors_alter()
*
* @return array
* An array of background colors.
*/
protected function getBannerColors() : array {
$colors = [
'default--red' => $this
->t('Red'),
'default--amber' => $this
->t('Amber'),
'default--green' => $this
->t('Green'),
'default--black' => $this
->t('Black'),
'default--gray' => $this
->t('Gray'),
'default--white' => $this
->t('White'),
];
// Allow other developers to add extra colors, such as brand colors.
$this->moduleHandler
->alter('message_banner_colors', $colors);
return $colors;
}
}
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. | |
MessageBannerSettingsForm:: |
protected | property | The module handler. | |
MessageBannerSettingsForm:: |
protected | property | The state storage service. | |
MessageBannerSettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
MessageBannerSettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
MessageBannerSettingsForm:: |
protected | function | Gets the available colors for the message banner. | |
MessageBannerSettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
MessageBannerSettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
MessageBannerSettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
MessageBannerSettingsForm:: |
public | function |
Constructs a message banner settings form. Overrides ConfigFormBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
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. |