Skip to content
Snippets Groups Projects

Issue #3231343: Allow Per-Group Dispatcher Settings

Files
3
@@ -2,16 +2,67 @@
@@ -2,16 +2,67 @@
namespace Drupal\notification_system_dispatch\Form;
namespace Drupal\notification_system_dispatch\Form;
use Drupal;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\notification_system_dispatch\NotificationSystemDispatcherInterface;
use Drupal\notification_system_dispatch\NotificationSystemDispatcherInterface;
 
use Drupal\notification_system_dispatch\NotificationSystemDispatcherPluginManager;
 
use Drupal\notification_system_dispatch\Service\UserSettingsService;
 
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
/**
* Provides a Notification System Dispatch form.
* Provides a Notification System Dispatch form.
*/
*/
class UserSettingsForm extends FormBase {
class UserSettingsForm extends FormBase {
 
/**
 
* The user setting service.
 
*
 
* @var \Drupal\notification_system_dispatch\Service\UserSettingsService
 
*/
 
protected $userService;
 
 
/**
 
* The dispatch plugin manager.
 
*
 
* @var \Drupal\notification_system_dispatch\NotificationSystemDispatcherPluginManager
 
*/
 
protected $dispatcherPluginManager;
 
 
/**
 
* The entity manager service.
 
*
 
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
 
*/
 
protected $entityTypeManager;
 
 
/**
 
* {@inheritdoc}
 
*/
 
public static function create(ContainerInterface $container) {
 
return new static(
 
$container->get('notification_system_dispatch.user_settings'),
 
$container->get('plugin.manager.notification_system_dispatcher'),
 
$container->get('entity_type.manager')
 
);
 
}
 
 
/**
 
* Constructs a new user settings form.
 
*
 
* @param \Drupal\notification_system_dispatch\Service\UserSettingsService $user_service
 
* The user setting service.
 
* @param \Drupal\notification_system_dispatch\NotificationSystemDispatcherPluginManager $dispatcher_plugin_manager
 
* The dispatch plugin manager.
 
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
 
* The Entity Manager.
 
*/
 
public function __construct(UserSettingsService $user_service, NotificationSystemDispatcherPluginManager $dispatcher_plugin_manager, EntityTypeManagerInterface $entity_type_manager) {
 
$this->userService = $user_service;
 
$this->dispatcherPluginManager = $dispatcher_plugin_manager;
 
$this->entityTypeManager = $entity_type_manager;
 
}
 
/**
/**
* {@inheritdoc}
* {@inheritdoc}
*/
*/
@@ -23,26 +74,30 @@ class UserSettingsForm extends FormBase {
@@ -23,26 +74,30 @@ class UserSettingsForm extends FormBase {
* {@inheritdoc}
* {@inheritdoc}
*/
*/
public function buildForm(array $form, FormStateInterface $form_state) {
public function buildForm(array $form, FormStateInterface $form_state) {
/** @var \Drupal\notification_system_dispatch\Service\UserSettingsService $userSettingsService */
$pluginDefinitions = $this->dispatcherPluginManager->getDefinitions();
$userSettingsService = Drupal::service('notification_system_dispatch.user_settings');
$groups = $this->entityTypeManager->getStorage('notification_group')->loadMultiple();
$group_settings = $this->userService->dispatcherGroupEnabled();
/** @var \Drupal\notification_system_dispatch\NotificationSystemDispatcherPluginManager $notificationSystemDispatcherPluginManager */
$notificationSystemDispatcherPluginManager = Drupal::service('plugin.manager.notification_system_dispatcher');
$pluginDefinitions = $notificationSystemDispatcherPluginManager->getDefinitions();
foreach ($pluginDefinitions as $definition) {
foreach ($pluginDefinitions as $definition) {
/** @var \Drupal\notification_system_dispatch\NotificationSystemDispatcherInterface $dispatcher */
/** @var \Drupal\notification_system_dispatch\NotificationSystemDispatcherInterface $dispatcher */
$dispatcher = $notificationSystemDispatcherPluginManager->createInstance($definition['id']);
$dispatcher = $this->dispatcherPluginManager->createInstance($definition['id']);
 
$id = $dispatcher->id();
 
 
$form['container-' . $id] = [
 
'#type' => 'container',
 
'#attributes' => [
 
'class' => ['dispatcher-group'],
 
],
 
];
$form['dispatcher_' . $dispatcher->id()] = [
$form['container-' . $id]['dispatcher_' . $id] = [
'#type' => 'checkbox',
'#type' => 'checkbox',
'#title' => $this->t('Receive notifications via @dispatcher', [
'#title' => $this->t('Receive notifications via @dispatcher', [
'@dispatcher' => $this->t($dispatcher->label(), [], [
'@dispatcher' => $this->t($dispatcher->label(), [], [
'context' => 'notification_system dispatcher label',
'context' => 'notification_system dispatcher label',
]),
]),
]),
]),
'#default_value' => $userSettingsService->dispatcherEnabled($dispatcher->id()),
'#default_value' => $this->userService->dispatcherEnabled($id),
'#ajax' => [
'#ajax' => [
'callback' => '::autosave',
'callback' => '::autosave',
'event' => 'change',
'event' => 'change',
@@ -53,10 +108,35 @@ class UserSettingsForm extends FormBase {
@@ -53,10 +108,35 @@ class UserSettingsForm extends FormBase {
],
],
],
],
];
];
 
 
// Setting per dispatcher, per group.
 
foreach ($groups as $gid => $group) {
 
$form['container-' . $id]['dispatcher_' . $id . '_group_' . $gid] = [
 
'#type' => 'checkbox',
 
'#title' => $group->label(),
 
'#default_value' => $group_settings[$id][$gid] ?? TRUE,
 
'#ajax' => [
 
'callback' => '::autosave',
 
'event' => 'change',
 
'wrapper' => 'ajax_placeholder',
 
'progress' => [
 
'type' => 'throbber',
 
'message' => '',
 
],
 
],
 
'#wrapper_attributes' => [
 
'class' => ['group-item'],
 
],
 
'#states' => [
 
'disabled' => [
 
':input[name="dispatcher_' . $id . '"]' => ['checked' => FALSE],
 
],
 
],
 
];
 
}
}
}
$enableBundling = $this->config('notification_system_dispatch.settings')->get('enable_bundling');
$enableBundling = $this->config('notification_system_dispatch.settings')->get('enable_bundling');
if ($enableBundling) {
if ($enableBundling) {
$form['send_mode'] = [
$form['send_mode'] = [
'#type' => 'select',
'#type' => 'select',
@@ -66,7 +146,7 @@ class UserSettingsForm extends FormBase {
@@ -66,7 +146,7 @@ class UserSettingsForm extends FormBase {
NotificationSystemDispatcherInterface::SEND_MODE_DAILY => $this->t('Daily summary'),
NotificationSystemDispatcherInterface::SEND_MODE_DAILY => $this->t('Daily summary'),
NotificationSystemDispatcherInterface::SEND_MODE_WEEKLY => $this->t('Weekly summary'),
NotificationSystemDispatcherInterface::SEND_MODE_WEEKLY => $this->t('Weekly summary'),
],
],
'#default_value' => $userSettingsService->getSendMode(),
'#default_value' => $this->userService->getSendMode(),
'#ajax' => [
'#ajax' => [
'callback' => '::autosave',
'callback' => '::autosave',
'event' => 'change',
'event' => 'change',
@@ -86,28 +166,31 @@ class UserSettingsForm extends FormBase {
@@ -86,28 +166,31 @@ class UserSettingsForm extends FormBase {
* Autosave callback for the form.
* Autosave callback for the form.
*
*
* @param array $form
* @param array $form
 
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param \Drupal\Core\Form\FormStateInterface $form_state
 
* The form state object.
*
*
* @return string[]
* @return string[]
 
* The save message markup.
*
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
* @throws \Drupal\Component\Plugin\Exception\PluginException
*/
*/
public function autosave(array &$form, FormStateInterface $form_state) {
public function autosave(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\notification_system_dispatch\Service\UserSettingsService $userSettingsService */
$userSettingsService = Drupal::service('notification_system_dispatch.user_settings');
/** @var \Drupal\notification_system_dispatch\NotificationSystemDispatcherPluginManager $notificationSystemDispatcherPluginManager */
$notificationSystemDispatcherPluginManager = Drupal::service('plugin.manager.notification_system_dispatcher');
$triggering_element = $form_state->getTriggeringElement();
$triggering_element = $form_state->getTriggeringElement();
if (str_starts_with($triggering_element['#name'], 'dispatcher_')) {
// We're dealing with dispatch settings.
$pluginId = str_replace('dispatcher_', '', $triggering_element['#name']);
if (strpos($triggering_element['#name'], 'dispatcher_') === 0) {
 
$trigger = str_replace('dispatcher_', '', $triggering_element['#name']);
/** @var \Drupal\notification_system_dispatch\NotificationSystemDispatcherInterface $dispatcher */
// A group setting.
$dispatcher = $notificationSystemDispatcherPluginManager->createInstance($pluginId);
if ($pluginId = strstr($trigger, '_group_', TRUE)) {
$group_id = str_replace($pluginId . '_group_', '', $trigger);
$userSettingsService->setDispatcherEnabled($dispatcher->id(), $triggering_element['#value']);
$this->userService->setDispatcherGroupEnabled($pluginId, $group_id, $triggering_element['#value']);
 
}
 
// Main Dispatcher checkbox.
 
else {
 
$this->userService->setDispatcherEnabled($trigger, $triggering_element['#value']);
 
}
}
}
if ($triggering_element['#name'] === 'send_mode') {
if ($triggering_element['#name'] === 'send_mode') {
@@ -115,7 +198,7 @@ class UserSettingsForm extends FormBase {
@@ -115,7 +198,7 @@ class UserSettingsForm extends FormBase {
if ($enableBundling) {
if ($enableBundling) {
$sendMode = $triggering_element['#value'];
$sendMode = $triggering_element['#value'];
$userSettingsService->setSendMode($sendMode);
$this->userService->setSendMode($sendMode);
}
}
}
}
Loading