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

Issue #3526573 by sanduhrs: Auto create Webhook types after Webhook config creation

parent 27c14645
Branches 4.0.x
No related tags found
2 merge requests!34Issue #3302284 by sanduhrs, ant1, datawench, vrs11: Add possibility to set up...,!33Issue #3526573 by sanduhrs: Auto creats webhook types after Webhook config creation
Pipeline #506655 passed
create_webhook_types: true
webhook.settings:
type: config_object
label: 'Webhooks settings'
mapping:
create_webhook_types:
type: boolean
label: 'Create Webhook types'
webhook.mappings.*:
type: config_object
label: 'Field payload mapping'
......
......@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Drupal\webhook\Hook;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\Url;
use Drupal\webhook\Entity\WebhookType;
......@@ -30,4 +31,47 @@ class WebhookHooks {
return $operations;
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
#[Hook('webhook_config_insert')]
public function webhookConfigInsert(EntityInterface $entity): void {
$create_webhook_types = \Drupal::service('config.factory')->get('webhooks.settings')
->get('create_webhook_types');
if ($create_webhook_types && $entity->get('type') === 'incoming') {
WebhookType::create([
'id' => $entity->id(),
'label' => $entity->label(),
])->save();
\Drupal::messenger()->addMessage(t('A Webhook Type has been created for storage. You may now <a href=":fields">add fields</a> and then <a href=":mapping">map the Webhook header and payload values</a> to the fields.', [
':fields' => Url::fromRoute('entity.webhook.field_ui_fields', ['webhook_type' => $entity->id()])->toString(),
':mapping' => Url::fromRoute('webhook.webhook_type.field_mapping', ['webhook_type' => $entity->id()])->toString(),
]));
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
#[Hook('form_webhooks_settings_alter')]
public function formWebhooksSettingsAlter(&$form, FormStateInterface $form_state, $form_id) : void {
$form['create_webhook_types'] = [
'#type' => 'checkbox',
'#title' => t('Create Webhook types'),
'#description' => t('Create Webhook types after Webhook config creation for dedicated incoming webhook storage.'),
'#default_value' => \Drupal::service('config.factory')->get('webhooks.settings')->get('create_webhook_types'),
'#disabled' => !\Drupal::service('module_handler')->moduleExists('webhook'),
];
$form['actions']['submit']['#submit'][] = '\Drupal\webhook\Hook\WebhookHooks::formWebhooksSettingsSubmit';
}
/**
* Implements form_webhooks_settings_alter submit handler.
*/
public static function formWebhooksSettingsSubmit(array $form, FormStateInterface $form_state) {
\Drupal::service('config.factory')->getEditable('webhooks.settings')
->set('create_webhook_types', $form_state->getValue('create_webhook_types'))
->save();
}
}
......@@ -20,6 +20,11 @@ parameters:
count: 1
path: modules/webhook/src/Form/MappingForm.php
-
message: "#^\\\\Drupal calls should be avoided in classes, use dependency injection instead$#"
count: 4
path: modules/webhook/src/Hook/WebhookHooks.php
-
message: "#^\\\\Drupal calls should be avoided in classes, use dependency injection instead$#"
count: 21
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment