From 3c2e00f2daa528b3c31130979139748c79efad0f Mon Sep 17 00:00:00 2001 From: artem_sylchuk <id.jk@2511902.no-reply.drupal.org> Date: Sat, 19 Feb 2022 14:21:37 +0000 Subject: [PATCH] Issue #3082478 by artem_sylchuk, wombatbuddy, Arno2Mars, muaz91: Rules integration for the Private Message module --- src/Plugin/RulesAction/SendPrivateMessage.php | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/Plugin/RulesAction/SendPrivateMessage.php diff --git a/src/Plugin/RulesAction/SendPrivateMessage.php b/src/Plugin/RulesAction/SendPrivateMessage.php new file mode 100644 index 00000000..43a87bda --- /dev/null +++ b/src/Plugin/RulesAction/SendPrivateMessage.php @@ -0,0 +1,74 @@ +<?php + +namespace Drupal\private_message\Plugin\RulesAction; + +use Drupal\rules\Core\RulesActionBase; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\user\UserInterface; +use Drupal\private_message\Entity\PrivateMessage; + +/** + * Provides "Send private message" rules action. + * + * @RulesAction( + * id = "private_message_send_message", + * label = @Translation("Send private message"), + * category = @Translation("System"), + * context_definitions = { + * "author" = @ContextDefinition("entity:user", + * label = @Translation("From"), + * description = @Translation("The author of the message.") + * ), + * "recipient" = @ContextDefinition("entity:user", + * label = @Translation("To"), + * description = @Translation("The recipient of the message.") + * ), + * "message" = @ContextDefinition("string", + * label = @Translation("Message"), + * description = @Translation("The message.") + * ), + * } + * ) + */ +class SendPrivateMessage extends RulesActionBase implements ContainerFactoryPluginInterface { + + /** + * The Private Message service. + * + * @var \Drupal\private_message\Service\PrivateMessageService + */ + protected $privateMessageService; + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + $instance = new static($configuration, $plugin_id, $plugin_definition); + $instance->privateMessageService = $container->get('private_message.service'); + return $instance; + } + + /** + * Send a private message. + * + * @param \Drupal\user\UserInterface $author + * The author of the message. + * @param \Drupal\user\UserInterface $recipient + * The recipient of the message. + * @param string $message + * The text of the message. + */ + protected function doExecute(UserInterface $author, UserInterface $recipient, $message) { + $members = [$author, $recipient]; + // Create a thread if one does not exist. + $private_message_thread = $this->privateMessageService->getThreadForMembers($members); + // Add a Message to the thread. + $private_message = PrivateMessage::create(); + $private_message->set('owner', $author); + $private_message->set('message', $message); + $private_message->save(); + $private_message_thread->addMessage($private_message)->save(); + } + +} -- GitLab