Skip to content

Commit 03a2152

Browse files
committed
Ajax-based component class discovery
Fixes #8
1 parent f05f2eb commit 03a2152

File tree

5 files changed

+49
-18
lines changed

5 files changed

+49
-18
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+
### Fixed
6+
- Fixed a bug where it wasn’t possible to create or edit webhooks if a plugin contained an invalid class. ([#8](https://github.com/craftcms/webhooks/issues/8))
7+
38
## 2.0.0 - 2019-03-19
49

510
### Added

src/WebhookHelper.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class WebhookHelper
3535
*/
3636
public static function classSuggestions(): array
3737
{
38-
$data = Craft::$app->getCache()->getOrSet('webhooks.classSuggestions', function() {
38+
return Craft::$app->getCache()->getOrSet('webhooks.classSuggestions', function() {
3939
$classes = [];
4040
foreach (self::_findClasses() as $class) {
4141
$classes[] = [
@@ -47,12 +47,6 @@ public static function classSuggestions(): array
4747
}, null, new FileDependency([
4848
'fileName' => Craft::$app->getPath()->getVendorPath() . '/composer/autoload_real.php',
4949
]));
50-
51-
return [
52-
[
53-
'data' => $data,
54-
],
55-
];
5650
}
5751

5852
/**

src/controllers/WebhooksController.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,13 @@ public function actionEdit(int $id = null, int $groupId = null, Webhook $webhook
7979
}
8080
}
8181

82-
// Autosuggest classes
83-
$classSuggestions = WebhookHelper::classSuggestions();
84-
8582
Craft::$app->getView()->registerAssetBundle(EditAsset::class);
8683

8784
return $this->renderTemplate('webhooks/_manage/edit', compact(
8885
'groupOptions',
8986
'webhook',
9087
'title',
91-
'crumbs',
92-
'classSuggestions'
88+
'crumbs'
9389
));
9490
}
9591

@@ -169,6 +165,16 @@ public function actionDelete(): Response
169165
return $this->redirectToPostedUrl();
170166
}
171167

168+
/**
169+
* Returns the available sender classes.
170+
*/
171+
public function actionClassSuggestions(): Response
172+
{
173+
return $this->asJson([
174+
'classes' => WebhookHelper::classSuggestions(),
175+
]);
176+
}
177+
172178
/**
173179
* Returns the available events for a component class.
174180
*/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{% extends '_includes/forms/autosuggest' %}
2+
3+
{% block data %}
4+
{{ parent() }}
5+
this.updateSuggestions()
6+
{#data.inputProps.onInputChange = this.myOnInputChange;#}
7+
8+
{% endblock %}
9+
10+
11+
{% block methods %}
12+
{{ parent() }}
13+
updateSuggestions() {
14+
Craft.postActionRequest('webhooks/webhooks/class-suggestions', {}, $.proxy(function(response, textStatus) {
15+
if (textStatus === 'success') {
16+
this.suggestions = [
17+
{
18+
data: response.classes
19+
}
20+
];
21+
}
22+
}, this));
23+
},
24+
{% endblock %}

src/templates/_manage/edit.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,20 @@
4141
errors: webhook.getErrors('name')
4242
}) }}
4343

44-
{{ forms.autosuggestField({
45-
label: 'Sender Class'|t('webhooks'),
46-
required: true,
47-
instructions: 'The class name the sender must be an instance of.'|t('webhooks'),
44+
{% set classInput = include('webhooks/_manage/class-autosuggest', {
4845
id: 'class',
4946
name: 'class',
5047
class: 'code ltr',
51-
suggestions: classSuggestions,
5248
placeholder: 'craft\\elements\\Entry',
5349
value: webhook.class,
5450
errors: webhook.getErrors('class')
55-
}) }}
51+
}, with_context = false) %}
52+
{{ forms.field({
53+
label: 'Sender Class'|t('webhooks'),
54+
required: true,
55+
instructions: 'The class name the sender must be an instance of.'|t('webhooks'),
56+
errors: webhook.getErrors('class')
57+
}, classInput) }}
5658

5759
{% set eventInput = include('webhooks/_manage/event-autosuggest', {
5860
id: 'event',

0 commit comments

Comments
 (0)