Skip to content
Snippets Groups Projects
Commit 17cc5507 authored by Ihor Mashevskyi's avatar Ihor Mashevskyi Committed by igor mashevskyi
Browse files

Issue #3432343: Add configuration for recipients plugin

parent e82a6623
No related branches found
No related tags found
1 merge request!2Issue #3432343: Add configuration for recipients plugin
...@@ -30,3 +30,24 @@ function message_plus_update_9001() { ...@@ -30,3 +30,24 @@ function message_plus_update_9001() {
$entity_definition_update->installFieldStorageDefinition('recipients', 'message', 'message_plus', $storage_definition); $entity_definition_update->installFieldStorageDefinition('recipients', 'message', 'message_plus', $storage_definition);
} }
/**
* Update the recipients field to according to new data structure.
*/
function message_plus_update_9002() {
$message_templates = \Drupal::entityTypeManager()
->getStorage('message_template')
->loadMultiple();
foreach ($message_templates as $message_template) {
$res_type = $message_template->getThirdPartySetting('message_plus', 'recipients_type', NULL);
if ($res_type) {
$message_template->setThirdPartySetting('message_plus', 'recipients', [
'plugin_id' => $res_type,
'configuration' => [],
]);
$message_template->unsetThirdPartySetting('message_plus', 'recipients_type');
$message_template->save();
}
}
}
...@@ -32,17 +32,53 @@ function message_plus_form_message_template_form_alter(array &$form, FormStateIn ...@@ -32,17 +32,53 @@ function message_plus_form_message_template_form_alter(array &$form, FormStateIn
'#default_value' => $entity->getThirdPartySetting('message_plus', 'sender_type') ?? [], '#default_value' => $entity->getThirdPartySetting('message_plus', 'sender_type') ?? [],
]; ];
$recipients_type_definitions = \Drupal::service('plugin.manager.message_recipients') $user_input = $form_state->getUserInput();
->getDefinitions(); $recipients_setting = $entity->getThirdPartySetting('message_plus', 'recipients');
/** @var \Drupal\message_plus\MessageRecipientsPluginManager $recipients_plugin_manager */
$recipients_plugin_manager = \Drupal::service('plugin.manager.message_recipients');
$recipients_plugin_id = $user_input['recipients']['plugin_id'] ?? $recipients_setting['plugin_id'] ?? NULL;
$recipients_type_configuration = $user_input['recipients']['configuration'] ?? $recipients_setting['configuration'] ?? [];
$recipients_type_definitions = $recipients_plugin_manager->getDefinitions();
$form['recipients'] = [
'#type' => 'container',
'#tree' => TRUE,
'#attributes' => [
'id' => 'recipients-type-configuration',
],
];
$form['recipients_type'] = [ $form['recipients']['plugin_id'] = [
'#title' => t('Recipients type'), '#title' => t('Recipients type'),
'#description' => t('Allow to get recipients via specific logic.'), '#description' => t('Allow to get recipients via specific logic.'),
'#type' => 'select', '#type' => 'select',
'#options' => [NULL => t('- None -')] + array_map(fn($item) => $item['label'], $recipients_type_definitions), '#options' => [NULL => t('- None -')] + array_map(fn($item) => $item['label'], $recipients_type_definitions),
'#default_value' => $entity->getThirdPartySetting('message_plus', 'recipients_type') ?? [], '#default_value' => $recipients_plugin_id,
'#ajax' => [
'callback' => '_message_plus_recipients_type_callback',
'wrapper' => 'recipients-type-configuration',
'event' => 'change',
],
]; ];
/** @var \Drupal\message_plus\MessageRecipientsPluginBase $recipients_plugin */
if (
$recipients_plugin_id &&
$recipients_plugin = $recipients_plugin_manager
->createInstance($recipients_plugin_id, $recipients_type_configuration)
) {
$config_form = $recipients_plugin->buildConfigurationForm([], $form_state);
if (!empty($config_form)) {
$form['recipients']['configuration'] = [
'#title' => t('Recipients configurations'),
'#type' => 'details',
];
$form['recipients']['configuration'] += $config_form;
}
}
$form['is_queue_enabled'] = [ $form['is_queue_enabled'] = [
'#title' => t('Is queue enabled?'), '#title' => t('Is queue enabled?'),
'#description' => t('If enabled then all messages will processed via queue.'), '#description' => t('If enabled then all messages will processed via queue.'),
...@@ -53,6 +89,22 @@ function message_plus_form_message_template_form_alter(array &$form, FormStateIn ...@@ -53,6 +89,22 @@ function message_plus_form_message_template_form_alter(array &$form, FormStateIn
array_unshift($form['actions']['submit']['#submit'], '_message_plus_submit_message_template'); array_unshift($form['actions']['submit']['#submit'], '_message_plus_submit_message_template');
} }
/**
* Ajax callback.
*
* @param array $form
* Form associated array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state.
*
* @return array|mixed
* Recipient form array.
*/
function _message_plus_recipients_type_callback(array $form, FormStateInterface $form_state) {
$form_state->setRebuild();
return $form['recipients'] ?? [];
}
/** /**
* Submit handler. * Submit handler.
*/ */
...@@ -61,7 +113,7 @@ function _message_plus_submit_message_template(array $form, FormStateInterface $ ...@@ -61,7 +113,7 @@ function _message_plus_submit_message_template(array $form, FormStateInterface $
$entity = $form_state->getFormObject()->getEntity(); $entity = $form_state->getFormObject()->getEntity();
$entity->setThirdPartySetting('message_plus', 'queue', $form_state->getValue('is_queue_enabled')); $entity->setThirdPartySetting('message_plus', 'queue', $form_state->getValue('is_queue_enabled'));
$entity->setThirdPartySetting('message_plus', 'sender_type', array_filter($form_state->getValue('sender_type', []))); $entity->setThirdPartySetting('message_plus', 'sender_type', array_filter($form_state->getValue('sender_type', [])));
$entity->setThirdPartySetting('message_plus', 'recipients_type', $form_state->getValue('recipients_type')); $entity->setThirdPartySetting('message_plus', 'recipients', $form_state->getValue('recipients'));
} }
/** /**
......
...@@ -8,6 +8,7 @@ services: ...@@ -8,6 +8,7 @@ services:
- '@logger.factory' - '@logger.factory'
- '@queue' - '@queue'
- '@config.factory' - '@config.factory'
- '@plugin.manager.message_recipients'
plugin.manager.message_recipients: plugin.manager.message_recipients:
class: Drupal\message_plus\MessageRecipientsPluginManager class: Drupal\message_plus\MessageRecipientsPluginManager
......
...@@ -5,6 +5,7 @@ declare(strict_types = 1); ...@@ -5,6 +5,7 @@ declare(strict_types = 1);
namespace Drupal\message_plus; namespace Drupal\message_plus;
use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Form\FormStateInterface;
/** /**
* Interface for message_recipients plugins. * Interface for message_recipients plugins.
...@@ -27,4 +28,25 @@ interface MessageRecipientsInterface { ...@@ -27,4 +28,25 @@ interface MessageRecipientsInterface {
*/ */
public function getRecipients(ContentEntityInterface $entity): array; public function getRecipients(ContentEntityInterface $entity): array;
/**
* Get default configurations.
*
* @return array
* List of configurations.
*/
public function defaultConfigurations(): array;
/**
* Build configuration form.
*
* @param array $form
* Form associated array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state.
*
* @return array
* Recipient form array.
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state): array;
} }
...@@ -5,6 +5,7 @@ declare(strict_types = 1); ...@@ -5,6 +5,7 @@ declare(strict_types = 1);
namespace Drupal\message_plus; namespace Drupal\message_plus;
use Drupal\Component\Plugin\PluginBase; use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Form\FormStateInterface;
/** /**
* Base class for message_recipients plugins. * Base class for message_recipients plugins.
...@@ -19,4 +20,18 @@ abstract class MessageRecipientsPluginBase extends PluginBase implements Message ...@@ -19,4 +20,18 @@ abstract class MessageRecipientsPluginBase extends PluginBase implements Message
return (string) $this->pluginDefinition['label']; return (string) $this->pluginDefinition['label'];
} }
/**
* {@inheritdoc}
*/
public function defaultConfigurations(): array {
return [];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
return $form;
}
} }
...@@ -18,7 +18,7 @@ use Drupal\message\Entity\MessageTemplate; ...@@ -18,7 +18,7 @@ use Drupal\message\Entity\MessageTemplate;
use Drupal\message\MessageInterface; use Drupal\message\MessageInterface;
use Drupal\message_notify\MessageNotifier; use Drupal\message_notify\MessageNotifier;
use Drupal\message_plus\Form\EntityTemplateSettingsForm; use Drupal\message_plus\Form\EntityTemplateSettingsForm;
use Drupal\user\EntityOwnerInterface; use Drupal\message_plus\MessageRecipientsPluginManager;
/** /**
* Message helper. * Message helper.
...@@ -68,6 +68,8 @@ class MessageProvider { ...@@ -68,6 +68,8 @@ class MessageProvider {
* Queue factory. * Queue factory.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The config factory. * The config factory.
* @param \Drupal\message_plus\MessageRecipientsPluginManager $messageRecipientsManager
* The recipients message plugin manager.
*/ */
public function __construct( public function __construct(
protected readonly EntityTypeManagerInterface $entityTypeManager, protected readonly EntityTypeManagerInterface $entityTypeManager,
...@@ -75,7 +77,8 @@ class MessageProvider { ...@@ -75,7 +77,8 @@ class MessageProvider {
protected readonly AccountInterface $currentUser, protected readonly AccountInterface $currentUser,
LoggerChannelFactory $logger_channel_factory, LoggerChannelFactory $logger_channel_factory,
QueueFactory $queue_factory, QueueFactory $queue_factory,
protected readonly ConfigFactoryInterface $configFactory protected readonly ConfigFactoryInterface $configFactory,
protected readonly MessageRecipientsPluginManager $messageRecipientsManager
) { ) {
$this->queue = $queue_factory->get(static::MESSAGE_QUEUE_WORKER); $this->queue = $queue_factory->get(static::MESSAGE_QUEUE_WORKER);
$this->logger = $logger_channel_factory->get('message_plus'); $this->logger = $logger_channel_factory->get('message_plus');
...@@ -272,10 +275,17 @@ class MessageProvider { ...@@ -272,10 +275,17 @@ class MessageProvider {
*/ */
protected function getDefaultRecipients(MessageInterface $message, ContentEntityInterface $entity) { protected function getDefaultRecipients(MessageInterface $message, ContentEntityInterface $entity) {
$recipients = []; $recipients = [];
// @todo Move functionality to plugins. $template_recipients = $message->getTemplate()
$recipients[] = $entity instanceof EntityOwnerInterface ->getThirdPartySetting('message_plus', 'recipients', []);
? $entity->getOwner()
: $message->getOwner(); if (
!empty($template_recipients['plugin_id']) &&
!empty($template_recipients['configuration']) &&
$recipients_plugin = $this->messageRecipientsManager
->createInstance($template_recipients['plugin_id'], $template_recipients['configuration'])
) {
$recipients = $recipients_plugin->getRecipients($entity);
}
return $recipients; return $recipients;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment