Skip to content
Snippets Groups Projects
Commit 411b5392 authored by Stefan Auditor's avatar Stefan Auditor
Browse files

Issue #3526214 by sanduhrs: Add bundles, fields and mapping interface to...

Issue #3526214 by sanduhrs: Add bundles, fields and mapping interface to webhook entity: adds mapping logic
parent 8ef97156
No related branches found
No related tags found
1 merge request!34Issue #3302284 by sanduhrs, ant1, datawench, vrs11: Add possibility to set up...
Pipeline #506456 passed with warnings
......@@ -2,6 +2,9 @@
namespace Drupal\webhook\EventSubscriber;
use Adbar\Dot;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Entity\EntityFieldManager;
use Drupal\Core\Entity\EntityTypeBundleInfo;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\webhook\Entity\Webhook;
......@@ -22,16 +25,40 @@ class WebhookSubscriber implements EventSubscriberInterface {
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfo
*/
protected $entityTypeBundleInfo;
protected EntityTypeBundleInfo $entityTypeBundleInfo;
/**
* The entity field manager service.
*
* @var \Drupal\Core\Entity\EntityFieldManager
*/
protected EntityFieldManager $entityFieldManager;
/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
private ConfigFactory $configFactory;
/**
* Constructs event subscriber.
*
* @param \Drupal\Core\Entity\EntityTypeBundleInfo $entity_type_bundle_info
* @param EntityTypeBundleInfo $entity_type_bundle_info
* The entity type bundle info service.
* @param EntityFieldManager $entity_field_manager
* The entity field manager service.
* @param ConfigFactory $config_factory
* The config factory service.
*/
public function __construct(EntityTypeBundleInfo $entity_type_bundle_info) {
public function __construct(
EntityTypeBundleInfo $entity_type_bundle_info,
EntityFieldManager $entity_field_manager,
ConfigFactory $config_factory,
) {
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->entityFieldManager = $entity_field_manager;
$this->configFactory = $config_factory;
}
/**
......@@ -53,14 +80,27 @@ class WebhookSubscriber implements EventSubscriberInterface {
*/
public function onWebhookReceive(ReceiveEvent $event) {
$bundle_info = $this->entityTypeBundleInfo->getBundleInfo('webhook');
$machine_name = $event->getWebhookConfig()->id();
$webhook = Webhook::create([
'bundle' => isset($bundle_info[$machine_name]) ? $machine_name : 'webhook',
$webhook_type = isset($bundle_info[$event->getWebhookConfig()->id()]) ? $event->getWebhookConfig()->id() : 'webhook';
$dots = new Dot([
'headers' => $event->getWebhook()->getHeaders(),
'payload' => $event->getWebhook()->getPayload(),
]);
$mapping = $this->configFactory->get('webhook.mappings.' . $webhook_type)->get('mapping');
$fields = $this->entityFieldManager->getFieldDefinitions('webhook', $webhook_type);
$field_data = [];
foreach ($mapping as $field_name => $identifier) {
if (isset($fields[$field_name]) && $dots->has($identifier)) {
$field_data[$field_name] = $dots->get($identifier);
}
}
$values = $field_data + [
'bundle' => $webhook_type,
'label' => $this->t('Webhook @uuid', ['@uuid' => $event->getWebhook()->getUuid()]),
'headers' => json_encode($event->getWebhook()->getHeaders()),
'payload' => json_encode($event->getWebhook()->getPayload()),
'created' => time(),
]);
];
$webhook = Webhook::create($values);
$webhook->save();
}
......
......@@ -6,6 +6,7 @@ namespace Drupal\webhook;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Link;
/**
* Provides a list controller for the webhook entity type.
......@@ -18,6 +19,7 @@ final class WebhookListBuilder extends EntityListBuilder {
public function buildHeader(): array {
$header['id'] = $this->t('ID');
$header['label'] = $this->t('Label');
$header['type'] = $this->t('Type');
$header['created'] = $this->t('Created');
return $header + parent::buildHeader();
}
......@@ -29,6 +31,7 @@ final class WebhookListBuilder extends EntityListBuilder {
/** @var \Drupal\webhook\WebhookInterface $entity */
$row['id'] = $entity->id();
$row['label'] = $entity->toLink();
$row['type'] = $entity->bundle();
$row['created']['data'] = $entity->get('created')->view(['label' => 'hidden']);
return $row + parent::buildRow($entity);
}
......
services:
webhook.event_subscriber:
class: Drupal\webhook\EventSubscriber\WebhookSubscriber
arguments: ['@entity_type.bundle.info']
arguments: ['@entity_type.bundle.info', '@entity_field.manager', '@config.factory']
tags:
- { name: event_subscriber }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment