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

Issue #3328999 by jurgenhaas: Add support for generic events and notifications

parent 78a7802b
No related branches found
No related tags found
No related merge requests found
name: DANSE Generic
type: module
description: Provides generic DANSE events to be created for arbitrary scenarios.
core_version_requirement: ^9.5 || ^10
package: DANSE
dependencies:
- danse:danse
services:
danse_generic:
class: Drupal\danse_generic\Service
arguments:
- '@plugin.manager.danse.plugin'
<?php
namespace Drupal\danse_generic;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\danse\Entity\EventInterface;
use Drupal\danse\PayloadBase;
use Drupal\danse\PayloadInterface;
use Drupal\node\Entity\Node;
use Drupal\user\Entity\User;
/**
* The generic payload class.
*
* @package Drupal\danse_generic
*/
final class Payload extends PayloadBase {
/**
* The label.
*
* @var string
*/
protected string $label;
/**
* The arbitrary data object.
*
* @var mixed
*/
protected mixed $data;
/**
* Webhook payload constructor.
*
* @param string $label
* The label.
* @param mixed $data
* The arbitrary data object.
*/
public function __construct(string $label, mixed $data) {
$this->label = $label;
$this->data = $data;
}
/**
* {@inheritdoc}
*/
public function label(EventInterface $event): string {
return $this->label;
}
/**
* {@inheritdoc}
*/
public function getEventReference(): string {
if ($this->data instanceof ContentEntityInterface) {
$id = [$this->data->getEntityTypeId(), $this->data->id()];
}
// @todo Add support for other data types.
else {
$id = hash('md5', serialize($this->data));
}
array_unshift($id, 'generic');
return implode('-', $id);
}
/**
* {@inheritdoc}
*/
public function getSubscriptionReferences(EventInterface $event): array {
return [];
}
/**
* {@inheritdoc}
*/
public function prepareArray(): array {
return [
'label' => $this->label,
'data' => $this->data,
];
}
/**
* {@inheritdoc}
*/
public static function createFromArray(array $payload): ?PayloadInterface {
return new Payload($payload['label'], $payload['data']);
}
/**
* {@inheritdoc}
*/
public function getEntity(): ContentEntityInterface {
if ($this->data instanceof ContentEntityInterface) {
$entity = $this->data;
}
// @todo Add support for other data types.
else {
/**
* @var \Drupal\node\NodeInterface $entity
*/
$entity = Node::create([
'title' => $this->label,
'body' => '',
]);
}
return $entity;
}
/**
* {@inheritdoc}
*/
public function hasAccess(int $uid): bool {
if ($this->data instanceof ContentEntityInterface) {
if ($account = User::load($uid)) {
return $this->data->access('view', $account, TRUE);
}
return FALSE;
}
// @todo Add support for other data types.
return TRUE;
}
}
<?php
namespace Drupal\danse_generic\Plugin\Danse;
use Drupal\danse\Entity\EventInterface;
use Drupal\danse\PayloadInterface;
use Drupal\danse\PluginBase;
use Drupal\danse_generic\Payload;
/**
* Plugin implementation of DANSE.
*
* @Danse(
* id = "generic",
* label = @Translation("Generic"),
* description = @Translation("Provides generic DANSE events.")
* )
*/
class Generic extends PluginBase {
/**
* {@inheritdoc}
*/
public function assertPayload(PayloadInterface $payload): bool {
return $payload instanceof Payload;
}
/**
* Creates a new generic event.
*
* @param string $topicId
* The identifier for the topic this event belongs to.
* @param string $label
* The label.
* @param \Drupal\danse_generic\Payload $payload
* The payload.
*
* @return \Drupal\danse\Entity\EventInterface|null
* The generic event if it was possible to create it, NULL otherwise.
*/
public function createGenericEvent(string $topicId, string $label, Payload $payload): ?EventInterface {
return $this->createEvent($topicId, $label, $payload);
}
}
<?php
namespace Drupal\danse_generic;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\danse\Entity\EventInterface;
use Drupal\danse\PluginManager;
use Drupal\danse_generic\Plugin\Danse\Generic;
/**
* Provides services for the DANSE generic sub-module.
*/
class Service {
/**
* @var \Drupal\danse_generic\Plugin\Danse\Generic
*/
protected Generic $plugin;
/**
* Constructor for the generic DANSE event.
*
* @param \Drupal\danse\PluginManager $pluginManager
* The DANSE plugin manager.
*/
public function __construct(PluginManager $pluginManager) {
try {
$plugin = $pluginManager->createInstance('generic');
if ($plugin instanceof Generic) {
$this->plugin = $plugin;
}
}
catch (PluginException $e) {
// Can be ignored.
}
}
/**
* Create a new generic DNASE event.
*
* @param string $topicId
* The topic ID.
* @param string $label
* The label.
* @param array $data
* The arbitrary data.
*
* @return \Drupal\danse\Entity\EventInterface|null
*/
public function createDanseEvent(string $topicId, string $label, array $data): ?EventInterface {
return $this->plugin->createGenericEvent($topicId, $label, new Payload($label, $data));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment