Skip to content
Snippets Groups Projects
Commit 2d7afe8f authored by Jürgen Haas's avatar Jürgen Haas
Browse files

Issue #3378397 by jurgenhaas: Fix notify action to specify the recipient

parent 980a6164
Branches 2.2.x
Tags
No related merge requests found
......@@ -8,10 +8,9 @@ use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\node\Entity\Node;
use Drupal\push_framework\ChannelPluginManager;
use Drupal\Core\Plugin\ContextAwarePluginManagerInterface;
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -19,7 +18,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*
* @Action(
* id = "push_framework_notify",
* label = @Translation("Push a notification to a channel.")
* label = @Translation("Push a notification to a channel."),
* type = "user"
* )
*/
class Notify extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
......@@ -38,16 +38,6 @@ class Notify extends ConfigurableActionBase implements ContainerFactoryPluginInt
*/
protected EntityTypeBundleInfoInterface $entityTypeBundleInfo;
/**
* The current user account.
*
* @var \Drupal\Core\Session\AccountProxyInterface
* Represents the currently authenticated user session within Drupal.
* Provides methods to retrieve information about the user,
* such as their ID, username, roles, and permissions.
*/
protected AccountProxyInterface $currentUser;
/**
* Constructs a new instance of the class.
*
......@@ -57,21 +47,15 @@ class Notify extends ConfigurableActionBase implements ContainerFactoryPluginInt
* The plugin ID.
* @param mixed $plugin_definition
* The plugin definition.
* @param \Drupal\Core\Plugin\ContextAwarePluginManagerInterface $channelPluginManager
* @param \Drupal\push_framework\ChannelPluginManager $channelPluginManager
* The channel plugin manager.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entityTypeBundleInfo
* The entity type bundle info service.
* @param \Drupal\Core\Session\AccountProxyInterface $currentUser
* The current user account.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* The entity type and bundle info service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ContextAwarePluginManagerInterface $channelPluginManager, EntityTypeBundleInfoInterface $entityTypeBundleInfo, AccountProxyInterface $currentUser, EntityTypeManagerInterface $entity_type_manager) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, ChannelPluginManager $channelPluginManager, EntityTypeBundleInfoInterface $entityTypeBundleInfo) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->channelPluginManager = $channelPluginManager;
$this->entityTypeBundleInfo = $entityTypeBundleInfo;
$this->currentUser = $currentUser;
$this->userStorage = $entity_type_manager->getStorage('user');
}
/**
......@@ -86,7 +70,7 @@ class Notify extends ConfigurableActionBase implements ContainerFactoryPluginInt
* @param mixed $plugin_definition
* The plugin definition.
*
* @return \Drupal\your_module\Notify
* @return \Drupal\push_framework\Plugin\Action\Notify
* The new instance of the Notify class.
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): Notify {
......@@ -95,9 +79,7 @@ class Notify extends ConfigurableActionBase implements ContainerFactoryPluginInt
$plugin_id,
$plugin_definition,
$container->get('push_framework.channel.plugin.manager'),
$container->get('entity_type.bundle.info'),
$container->get('current_user'),
$container->get('entity_type.manager')
$container->get('entity_type.bundle.info')
);
}
......@@ -105,15 +87,17 @@ class Notify extends ConfigurableActionBase implements ContainerFactoryPluginInt
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
$result = AccessResult::allowed();
$result = AccessResult::allowedIf($object instanceof UserInterface);
try {
$channelPlugin = $this->channelPluginManager->createInstance($this->configuration['channel']);
if ($channelPlugin === NULL) {
throw new \RuntimeException('The given channel does not exist.');
$result = AccessResult::forbidden('The given channel does not exist.');
}
$bundles = $this->entityTypeBundleInfo->getBundleInfo('node');
if (!isset($bundles[$this->configuration['node_type']])) {
throw new \RuntimeException('The given node type does not exist.');
else {
$bundles = $this->entityTypeBundleInfo->getBundleInfo('node');
if (!isset($bundles[$this->configuration['node_type']])) {
$result = AccessResult::forbidden('The given node type does not exist.');
}
}
}
catch (\Exception $e) {
......@@ -125,10 +109,11 @@ class Notify extends ConfigurableActionBase implements ContainerFactoryPluginInt
/**
* {@inheritdoc}
*/
public function execute(): void {
public function execute($object = NULL): void {
/** @var \Drupal\push_framework\ChannelPluginInterface $channelPlugin */
$channelPlugin = $this->channelPluginManager->createInstance($this->configuration['channel']);
$user = $this->userStorage->load($this->currentUser->id());
/** @var \Drupal\user\UserInterface $user */
$user = $object;
/** @var \Drupal\node\Entity\Node $node */
$node = Node::create([
'type' => $this->configuration['node_type'],
......@@ -150,7 +135,7 @@ class Notify extends ConfigurableActionBase implements ContainerFactoryPluginInt
'body_field' => 'body',
'subject' => '',
'body' => '',
];
] + parent::defaultConfiguration();
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment