diff --git a/CHANGELOG.md b/CHANGELOG.md index a3dab3e223..864d274e32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/system/blueprints/flex/configure/compat.yaml b/system/blueprints/flex/configure/compat.yaml new file mode 100644 index 0000000000..08497d400b --- /dev/null +++ b/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 diff --git a/system/blueprints/flex/pages.yaml b/system/blueprints/flex/pages.yaml index f871f61bbe..43d0a6fad7 100644 --- a/system/blueprints/flex/pages.yaml +++ b/system/blueprints/flex/pages.yaml @@ -185,6 +185,13 @@ config: - title - name +blueprints: + configure: + fields: + import@: + type: configure/compat + context: blueprints://flex + # Regular form definition form: fields: diff --git a/system/blueprints/flex/user-accounts.yaml b/system/blueprints/flex/user-accounts.yaml index 56aad88bb6..5cc00f98cc 100644 --- a/system/blueprints/flex/user-accounts.yaml +++ b/system/blueprints/flex/user-accounts.yaml @@ -121,6 +121,13 @@ config: - key - email +blueprints: + configure: + fields: + import@: + type: configure/compat + context: blueprints://flex + # Regular form definition form: fields: diff --git a/system/blueprints/flex/user-groups.yaml b/system/blueprints/flex/user-groups.yaml index 840b1449ae..a09a821b59 100644 --- a/system/blueprints/flex/user-groups.yaml +++ b/system/blueprints/flex/user-groups.yaml @@ -113,3 +113,10 @@ config: - key - groupname - description + +blueprints: + configure: + fields: + import@: + type: configure/compat + context: blueprints://flex diff --git a/system/src/Grav/Common/Flex/Traits/FlexObjectTrait.php b/system/src/Grav/Common/Flex/Traits/FlexObjectTrait.php index a381574497..f52339007a 100644 --- a/system/src/Grav/Common/Flex/Traits/FlexObjectTrait.php +++ b/system/src/Grav/Common/Flex/Traits/FlexObjectTrait.php @@ -28,6 +28,14 @@ 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', @@ -35,7 +43,10 @@ public function triggerEvent(string $name, $event = null) '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); } diff --git a/system/src/Grav/Common/Flex/Types/Pages/PageObject.php b/system/src/Grav/Common/Flex/Types/Pages/PageObject.php index a7f003eb04..403bdc5a99 100644 --- a/system/src/Grav/Common/Flex/Types/Pages/PageObject.php +++ b/system/src/Grav/Common/Flex/Types/Pages/PageObject.php @@ -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; } diff --git a/system/src/Grav/Common/Flex/Types/Users/UserObject.php b/system/src/Grav/Common/Flex/Types/Users/UserObject.php index acade0de65..8b854df715 100644 --- a/system/src/Grav/Common/Flex/Types/Users/UserObject.php +++ b/system/src/Grav/Common/Flex/Types/Users/UserObject.php @@ -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; @@ -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; } /**