Skip to content

Commit

Permalink
Fixed missing onAdminSave and onAdminAfterSave events when using …
Browse files Browse the repository at this point in the history
…`Flex Pages` and `Flex Users`
  • Loading branch information
mahagr committed Jul 1, 2020
1 parent 291ed38 commit 3a05dcf
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@
* Fixed plugin initialization in CLI
* Fixed broken logic in `Page::topParent()` when dealing with first-level pages
* Fixed broken `Flex Page` authorization for groups
* Fixed missing `onAdminSave` and `onAdminAfterSave` events when using `Flex Pages` and `Flex Users` [flex-objects#58](https://github.com/trilbymedia/grav-plugin-flex-objects/issues/58)

# v1.7.0-rc.12
## 06/08/2020
Expand Down
17 changes: 17 additions & 0 deletions system/blueprints/flex/configure/compat.yaml
@@ -0,0 +1,17 @@
form:
compatibility:
type: tab
title: Compatibility
fields:
object.compat.events:
type: toggle
toggleable: true
label: Admin event compatibility
help: Enables onAdminSave and onAdminAfterSave events for plugins
highlight: 1
default: 1
options:
1: PLUGIN_ADMIN.ENABLED
0: PLUGIN_ADMIN.DISABLED
validate:
type: bool
7 changes: 7 additions & 0 deletions system/blueprints/flex/pages.yaml
Expand Up @@ -185,6 +185,13 @@ config:
- title
- name

blueprints:
configure:
fields:
import@:
type: configure/compat
context: blueprints://flex

# Regular form definition
form:
fields:
Expand Down
7 changes: 7 additions & 0 deletions system/blueprints/flex/user-accounts.yaml
Expand Up @@ -121,6 +121,13 @@ config:
- key
- email

blueprints:
configure:
fields:
import@:
type: configure/compat
context: blueprints://flex

# Regular form definition
form:
fields:
Expand Down
7 changes: 7 additions & 0 deletions system/blueprints/flex/user-groups.yaml
Expand Up @@ -113,3 +113,10 @@ config:
- key
- groupname
- description

blueprints:
configure:
fields:
import@:
type: configure/compat
context: blueprints://flex
13 changes: 12 additions & 1 deletion system/src/Grav/Common/Flex/Traits/FlexObjectTrait.php
Expand Up @@ -28,14 +28,25 @@ trait FlexObjectTrait
*/
public function triggerEvent(string $name, $event = null)
{
$events = [
'onRender' => 'onFlexObjectRender',
'onBeforeSave' => 'onFlexObjectBeforeSave',
'onAfterSave' => 'onFlexObjectAfterSave',
'onBeforeDelete' => 'onFlexObjectBeforeDelete',
'onAfterDelete' => 'onFlexObjectAfterDelete'
];

if (null === $event) {
$event = new Event([
'type' => 'flex',
'directory' => $this->getFlexDirectory(),
'object' => $this
]);
}
if (strpos($name, 'onFlexObject') !== 0 && strpos($name, 'on') === 0) {

if (isset($events['name'])) {
$name = $events['name'];
} elseif (strpos($name, 'onFlexObject') !== 0 && strpos($name, 'on') === 0) {
$name = 'onFlexObject' . substr($name, 2);
}

Expand Down
16 changes: 16 additions & 0 deletions system/src/Grav/Common/Flex/Types/Pages/PageObject.php
Expand Up @@ -235,12 +235,28 @@ public function save($reorder = true)
{
$variables = $this->onBeforeSave(func_get_args());

// Backwards compatibility with older plugins.
$fireEvents = $reorder && $this->isAdminSite() && $this->getFlexDirectory()->getConfig('object.compat.events', true);
if ($fireEvents) {
$grav = $this->getContainer();
$self = $this;
$grav->fireEvent('onAdminSave', new Event(['type' => 'flex', 'directory' => $this->getFlexDirectory(), 'page' => &$self]));
if ($self !== $this) {
throw new \RuntimeException('Switching Flex Page object during onAdminSave event is not supported! Please update plugin.');
}
}

/** @var static $instance */
$instance = parent::save();
$variables = $this->onSave($variables);

$this->onAfterSave($variables);

// Backwards compatibility with older plugins.
if ($fireEvents) {
$grav->fireEvent('onAdminAfterSave', new Event(['type' => 'flex', 'directory' => $this->getFlexDirectory(), 'page' => $this]));
}

return $instance;
}

Expand Down
21 changes: 20 additions & 1 deletion system/src/Grav/Common/Flex/Types/Users/UserObject.php
Expand Up @@ -36,6 +36,7 @@
use Grav\Framework\Flex\Traits\FlexMediaTrait;
use Grav\Framework\Form\FormFlashFile;
use Psr\Http\Message\UploadedFileInterface;
use RocketTheme\Toolbox\Event\Event;
use RocketTheme\Toolbox\File\FileInterface;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;

Expand Down Expand Up @@ -550,7 +551,25 @@ public function save()
$this->setProperty('hashed_password', Authentication::create($password));
}

return parent::save();
// Backwards compatibility with older plugins.
$fireEvents = $this->isAdminSite() && $this->getFlexDirectory()->getConfig('object.compat.events', true);
if ($fireEvents) {
$grav = $this->getContainer();
$self = $this;
$grav->fireEvent('onAdminSave', new Event(['type' => 'flex', 'directory' => $this->getFlexDirectory(), 'object' => &$self]));
if ($self !== $this) {
throw new \RuntimeException('Switching Flex User object during onAdminSave event is not supported! Please update plugin.');
}
}

$instance = parent::save();

// Backwards compatibility with older plugins.
if ($fireEvents) {
$grav->fireEvent('onAdminAfterSave', new Event(['type' => 'flex', 'directory' => $this->getFlexDirectory(), 'object' => $this]));
}

return $instance;
}

/**
Expand Down

0 comments on commit 3a05dcf

Please sign in to comment.