class NotificationsWidgetLoggerSettingsForm in Notifications widget 8
Provides settings for Activity tracking 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\notifications_widget\Form\NotificationsWidgetLoggerSettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of NotificationsWidgetLoggerSettingsForm
1 string reference to 'NotificationsWidgetLoggerSettingsForm'
File
- src/
Form/ NotificationsWidgetLoggerSettingsForm.php, line 17
Namespace
Drupal\notifications_widget\FormView source
class NotificationsWidgetLoggerSettingsForm extends ConfigFormBase {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The messager type manager.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messengerTypeManager;
/**
* The module manager service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructs an AutologoutSettingsForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module manager service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
* The module entity manager service.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger_manager
* The module messager manager service.
*/
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_manager, MessengerInterface $messenger_manager) {
parent::__construct($config_factory);
$this->moduleHandler = $module_handler;
$this->entityTypeManager = $entity_manager;
$this->messengerTypeManager = $messenger_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('module_handler'), $container
->get('entity_type.manager'), $container
->get('messenger'));
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'notifications_widget.settings',
];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'notifications_widget_admin_settings';
}
/**
* Build form.
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('notifications_widget.settings');
$form['excluded_entities'] = [
'#type' => 'textarea',
'#title' => $this
->t('Exclude Entities (comma seperated)'),
'#description' => $this
->t('Exclude the notification entities for excluding'),
'#default_value' => $config
->get('excluded_entities'),
];
$form['additional_entity_type'] = [
'#type' => 'textarea',
'#title' => $this
->t('Additional Entity Types (comma seperated)'),
'#description' => $this
->t('Provide the Entity Types to enable and configure notifications'),
'#default_value' => $config
->get('additional_entity_type'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$haystack = [
'user',
'node_type',
'comment_type',
'taxonomy_vocabulary',
];
$values = $form_state
->getValues();
$entities = explode(',', $values['additional_entity_type']);
if (count(array_intersect($haystack, $entities)) > 0) {
$form_state
->setErrorByName('additional_entity_type', $this
->t('Default entity type already exists.'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$values = $form_state
->getValues();
$savedEntities = explode(',', $this
->config('notifications_widget.settings')
->get('additional_entity_type'));
$revisedEntities = explode(',', $values['additional_entity_type']);
// Prepare removed entity types and remove its configuration.
$deletedEntities = array_diff($savedEntities, $revisedEntities);
$this
->removeEntityNotificationsSettings($deletedEntities, $values);
$this
->config('notifications_widget.settings')
->set('excluded_entities', $values['excluded_entities'])
->set('additional_entity_type', $values['additional_entity_type'])
->save();
$this->messengerTypeManager
->addMessage($this
->t('Notification widget will work well once you saved the configuration from <a href=":user_settings_url">Notification Widget Settings</a>.', [
':user_settings_url' => Url::fromRoute('notifications_widget.notifications_widget_settings')
->toString(),
]), 'warning');
}
/**
* Remove addtional configuration for entity types.
*/
public function removeEntityNotificationsSettings($entities, $values) {
foreach ($entities as $entityType) {
if ($this->entityTypeManager
->hasDefinition($entityType)) {
$entityTypeStorage = $this->entityTypeManager
->getStorage($entityType);
$entityTypes = $entityTypeStorage
->loadMultiple();
if (!empty($entityTypes)) {
foreach ($entityTypes as $entityType => $entityTypeData) {
$enableSettings = $entityType . '_enable';
$enableCreate = $entityType . '_noti_create_message';
$enableUpdate = $entityType . '_noti_update_message';
$enableDelete = $entityType . '_noti_delete_message';
$createLink = $entityType . '_redirect_create_link';
$updateLink = $entityType . '_redirect_update_link';
$deleteLink = $entityType . '_redirect_delete_link';
$this
->config('notifications_widget.settings')
->clear($enableSettings)
->clear($enableCreate)
->clear($enableUpdate)
->clear($enableDelete)
->clear($createLink)
->clear($updateLink)
->clear($deleteLink)
->save();
}
}
}
}
}
}
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. | |
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. | |
NotificationsWidgetLoggerSettingsForm:: |
protected | property | The entity type manager. | |
NotificationsWidgetLoggerSettingsForm:: |
protected | property | The messager type manager. | |
NotificationsWidgetLoggerSettingsForm:: |
protected | property | The module manager service. | |
NotificationsWidgetLoggerSettingsForm:: |
public | function |
Build form. Overrides ConfigFormBase:: |
|
NotificationsWidgetLoggerSettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
NotificationsWidgetLoggerSettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
NotificationsWidgetLoggerSettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
NotificationsWidgetLoggerSettingsForm:: |
public | function | Remove addtional configuration for entity types. | |
NotificationsWidgetLoggerSettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
NotificationsWidgetLoggerSettingsForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
NotificationsWidgetLoggerSettingsForm:: |
public | function |
Constructs an AutologoutSettingsForm 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. |