diff --git a/message_plus.install b/message_plus.install index 327e2a27d7ba214f043dbfc94bbce6e9bc62b6fc..0d95452480ba8674873b111dfba62c74051f8736 100644 --- a/message_plus.install +++ b/message_plus.install @@ -30,3 +30,24 @@ function message_plus_update_9001() { $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(); + } + } +} diff --git a/message_plus.module b/message_plus.module index 619b41872dd804ae84c61b49040c22c2b18e658b..d11388796ef7dcd952e0a042bb64088145dae241 100644 --- a/message_plus.module +++ b/message_plus.module @@ -32,17 +32,53 @@ function message_plus_form_message_template_form_alter(array &$form, FormStateIn '#default_value' => $entity->getThirdPartySetting('message_plus', 'sender_type') ?? [], ]; - $recipients_type_definitions = \Drupal::service('plugin.manager.message_recipients') - ->getDefinitions(); + $user_input = $form_state->getUserInput(); + $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'), '#description' => t('Allow to get recipients via specific logic.'), '#type' => 'select', '#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'] = [ '#title' => t('Is queue enabled?'), '#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 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. */ @@ -61,7 +113,7 @@ function _message_plus_submit_message_template(array $form, FormStateInterface $ $entity = $form_state->getFormObject()->getEntity(); $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', 'recipients_type', $form_state->getValue('recipients_type')); + $entity->setThirdPartySetting('message_plus', 'recipients', $form_state->getValue('recipients')); } /** diff --git a/message_plus.services.yml b/message_plus.services.yml index 9b2120e0104512dcca29c737eaf98ffdd4186239..c76cb8f01c36f0dfca6108a040e25068fc93145e 100644 --- a/message_plus.services.yml +++ b/message_plus.services.yml @@ -8,6 +8,7 @@ services: - '@logger.factory' - '@queue' - '@config.factory' + - '@plugin.manager.message_recipients' plugin.manager.message_recipients: class: Drupal\message_plus\MessageRecipientsPluginManager diff --git a/src/MessageRecipientsInterface.php b/src/MessageRecipientsInterface.php index 7f3c8ff2ad18c611e51cc31a617b775c53ebe5af..3af7d94c7d6be5420fe03d9a3ec0284494f155c1 100644 --- a/src/MessageRecipientsInterface.php +++ b/src/MessageRecipientsInterface.php @@ -5,6 +5,7 @@ declare(strict_types = 1); namespace Drupal\message_plus; use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\Core\Form\FormStateInterface; /** * Interface for message_recipients plugins. @@ -27,4 +28,25 @@ interface MessageRecipientsInterface { */ 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; + } diff --git a/src/MessageRecipientsPluginBase.php b/src/MessageRecipientsPluginBase.php index 8627d83a4124747be1948bb351dca084ab70dc07..d82d81c40c629e2d478de95ce15ab2c8256ee900 100644 --- a/src/MessageRecipientsPluginBase.php +++ b/src/MessageRecipientsPluginBase.php @@ -5,6 +5,7 @@ declare(strict_types = 1); namespace Drupal\message_plus; use Drupal\Component\Plugin\PluginBase; +use Drupal\Core\Form\FormStateInterface; /** * Base class for message_recipients plugins. @@ -19,4 +20,18 @@ abstract class MessageRecipientsPluginBase extends PluginBase implements Message return (string) $this->pluginDefinition['label']; } + /** + * {@inheritdoc} + */ + public function defaultConfigurations(): array { + return []; + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state): array { + return $form; + } + } diff --git a/src/Service/MessageProvider.php b/src/Service/MessageProvider.php index 6917b8441fb701501715f09c4e751d6505f754db..1e5c6e1f7ec60668d6cc2217139b8a5bfffcc3c0 100755 --- a/src/Service/MessageProvider.php +++ b/src/Service/MessageProvider.php @@ -18,7 +18,7 @@ use Drupal\message\Entity\MessageTemplate; use Drupal\message\MessageInterface; use Drupal\message_notify\MessageNotifier; use Drupal\message_plus\Form\EntityTemplateSettingsForm; -use Drupal\user\EntityOwnerInterface; +use Drupal\message_plus\MessageRecipientsPluginManager; /** * Message helper. @@ -68,6 +68,8 @@ class MessageProvider { * Queue factory. * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory * The config factory. + * @param \Drupal\message_plus\MessageRecipientsPluginManager $messageRecipientsManager + * The recipients message plugin manager. */ public function __construct( protected readonly EntityTypeManagerInterface $entityTypeManager, @@ -75,7 +77,8 @@ class MessageProvider { protected readonly AccountInterface $currentUser, LoggerChannelFactory $logger_channel_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->logger = $logger_channel_factory->get('message_plus'); @@ -272,10 +275,17 @@ class MessageProvider { */ protected function getDefaultRecipients(MessageInterface $message, ContentEntityInterface $entity) { $recipients = []; - // @todo Move functionality to plugins. - $recipients[] = $entity instanceof EntityOwnerInterface - ? $entity->getOwner() - : $message->getOwner(); + $template_recipients = $message->getTemplate() + ->getThirdPartySetting('message_plus', 'recipients', []); + + 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; }