Skip to content

Commit ec337fb

Browse files
committed
Allow webhook URLs to be set to environment variables or Twig code
Resolves #18
1 parent 5726284 commit ec337fb

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Release Notes for Webhooks for Craft CMS
22

3+
## Unreleased
4+
5+
### Added
6+
- Webhook URLs can now be set to environment variables or Twig code. ([#18](https://github.com/craftcms/webhooks/issues/18))
7+
38
## 2.2.0 - 2019-07-29
49

510
### Added

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ Webhooks listen to [events](https://www.yiiframework.com/doc/guide/2.0/en/concep
7070

7171
The Sender Class can be a subclass of the class that triggers the event. For example, all elements fire an [afterSave](https://docs.craftcms.com/api/v3/craft-base-element.html#event-after-save) event after they’ve been saved, courtesy of their base class, [craft\base\Element](https://docs.craftcms.com/api/v3/craft-base-element.html). However if you’re only interested in sending a webhook when an _entry_ gets saved, you can set the Sender Class to [craft\elements\Entry](https://docs.craftcms.com/api/v3/craft-elements-entry.html).
7272

73+
Webhook URLs can be set to an environment variable (`$VARIABLE_NAME`) or Twig code. If you set it to Twig code, you can reference the triggered event via an `event` variable.
74+
7375
See [Integrating with Task Automation Tools](#integrating-with-task-automation-tools) for examples on how to get a Webhook URL from various task automation tools.
7476

7577
![Screenshot of the Edit Webhook page](./images/edit-webhook.png)

src/Plugin.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@ public function init()
180180
}
181181

182182
// Queue the send request up
183-
$this->request($webhook->method, $webhook->url, $headers, $body, $webhook->id);
183+
$url = Craft::parseEnv($webhook->url);
184+
$url = $view->renderString($url, [
185+
'event' => $e,
186+
]);
187+
$this->request($webhook->method, $url, $headers, $body, $webhook->id);
184188
});
185189
}
186190

src/templates/_manage/edit.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
{{ forms.field({
127127
label: 'Request Method & URL'|t('webhooks'),
128128
required: true,
129-
instructions: 'The request method and URL that the webhook should send. (The URL will be provided by your [task automation tool][1].)'|t('webhooks') ~ '\n\n[1]: https://github.com/craftcms/webhooks#integration-with-task-automation-tools',
129+
instructions: 'The request method and URL that the webhook should send.'|t('webhooks') ~ ' ' ~ 'Can be set to an environment variable (`$VARIABLE_NAME`) or Twig code.'|t('webhooks'),
130130
id: 'url',
131131
errors: webhook.getErrors('url')
132132
}, urlInput) }}

0 commit comments

Comments
 (0)