Skip to content

Commit

Permalink
Merge branch 'release/v1.0.9'
Browse files Browse the repository at this point in the history
  • Loading branch information
gregory-claeyssens committed Aug 26, 2021
2 parents 433a9e1 + b7b4c15 commit 05b0f60
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .craftplugin
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"pluginName":"Storychief v3","pluginDescription":"Craft CMS plugin to use with Storychief","pluginVersion":"1.0.6","pluginAuthorName":"Storychief","pluginVendorName":"storychief","pluginAuthorUrl":"https://github.com/Story-Chief/","pluginAuthorGithub":"","codeComments":"","pluginComponents":"","consolecommandName":"","controllerName":"","cpsectionName":"","elementName":"","fieldName":"","modelName":"","purchasableName":"","recordName":"","serviceName":"","taskName":"","utilityName":"","widgetName":"","apiVersion":"api_version_3_0"}
{"pluginName":"Storychief v3","pluginDescription":"Craft CMS plugin to use with Storychief","pluginVersion":"1.0.9","pluginAuthorName":"Storychief","pluginVendorName":"storychief","pluginAuthorUrl":"https://github.com/Story-Chief/","pluginAuthorGithub":"","codeComments":"","pluginComponents":"","consolecommandName":"","controllerName":"","cpsectionName":"","elementName":"","fieldName":"","modelName":"","purchasableName":"","recordName":"","serviceName":"","taskName":"","utilityName":"","widgetName":"","apiVersion":"api_version_3_0"}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 1.0.9 - 2021-08-26
### Added
- Added beforeEntryPublish and beforeEntryUpdate events which allow altering of the entry before it is saved.

## 1.0.6 - 2020-08-17
### Added
- Support for propagation settings: none, language, siteGroup and custom. Previously only all
Expand Down
48 changes: 45 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,56 @@ Finally, back to your StoryChief CRAFT CMS channel configuration, fill up your C
:)

## Events
(Note: this is mostly for developers that know basic PHP and Composer Packages)

`afterEntryPublish` Event, this allows developers to execute custom functionality after a new entry, pushed by Storychief, is saved in Craft.
Note: this is mostly for developers that know basic PHP and Composer Packages.

### `beforeEntryPublish` and `beforeEntryUpdate` event

This allows developers to execute custom functionality before saving a new or updating an entry, to modify data of a
new entry.

```php

use storychief\storychiefv3\events\EntryPublishEvent;

$this->on('beforeEntryPublish', function (EntryPublishEvent $event) {
$payload = $event->payload;
$settings = $event->settings;
$entry = $event->entry;

// Example 1:
$entry->sectionId = 2; // BLog
$entry->typeId = 2;

// Example 2:
if ($payload['data']['custom_fields']) {
foreach ($payload['data']['custom_fields'] as $customField) {
if ($customField['key'] === 'custom_field_name') {
$entry->sectionId = $customField['value'];
$entry->typeId = 2;
}
}
}
});

use storychief\storychiefv3\events\EntryUpdateEvent;

$this->on('beforeEntryUpdate', function (EntryUpdateEvent $event) {
// ...
});
```

### `afterEntryPublish` event

This allows developers to execute custom functionality after a new entry, pushed by Storychief, is saved in Craft.

### `afterEntryUpdate` event

`afterEntryUpdate` Event, this allows developers to execute custom functionality after an update to an entry, pushed by Storychief, is saved in Craft.
This allows developers to execute custom functionality after an update to an entry, pushed by Storychief, is saved in Craft.

Both events send out a `EntrySaveEvent` with the saved `Entry` object as its property.

---

Brought to you by [StoryChief](https://github.com/Story-Chief/)

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "storychief/craft-cms-v3-storychief",
"description": "Craft CMS plugin to use with Storychief",
"type": "craft-plugin",
"version": "1.0.8",
"version": "1.0.9",
"keywords": [
"craft",
"cms",
Expand Down
26 changes: 26 additions & 0 deletions src/controllers/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use craft\elements\Entry;
use craft\elements\User;
use storychief\storychiefv3\storychief\FieldTypes\StoryChiefFieldTypeInterface;
use storychief\storychiefv3\events\EntryPublishEvent;
use storychief\storychiefv3\events\EntryUpdateEvent;
use storychief\storychiefv3\events\EntrySaveEvent;

class WebhookController extends Controller
Expand Down Expand Up @@ -139,6 +141,18 @@ protected function handlePublishEventType()
// Map all other fields
$entry = $this->_map($entry);

// Trigger event to alter the entry before saving
$this->trigger(
'beforeEntryPublish',
new EntryPublishEvent(
[
'payload' => $this->payload,
'settings' => $this->settings,
'entry' => $entry,
]
)
);

Craft::$app->elements->saveElement($entry);

// Trigger after publish event.
Expand All @@ -161,6 +175,18 @@ protected function handleUpdateEventType()

$entry = $this->_map($entry);

// Trigger event to alter the entry before saving
$this->trigger(
'beforeEntryUpdate',
new EntryUpdateEvent(
[
'payload' => $this->payload,
'settings' => $this->settings,
'entry' => $entry,
]
)
);

Craft::$app->elements->saveElement($entry);

// Trigger after update event.
Expand Down
19 changes: 19 additions & 0 deletions src/events/EntryPublishEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace storychief\storychiefv3\events;

use craft\elements\Entry;
use storychief\storychiefv3\models\Settings;
use yii\base\Event;

class EntryPublishEvent extends Event
{
/** @var array */
public $payload;

/** @var Settings */
public $settings;

/** @var Entry */
public $entry;
}
19 changes: 19 additions & 0 deletions src/events/EntryUpdateEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace storychief\storychiefv3\events;

use craft\elements\Entry;
use storychief\storychiefv3\models\Settings;
use yii\base\Event;

class EntryUpdateEvent extends Event
{
/** @var array */
public $payload;

/** @var Settings */
public $settings;

/** @var Entry */
public $entry;
}

0 comments on commit 05b0f60

Please sign in to comment.