From 715e4deddfa284ff33d274f4255d669c4a5258d3 Mon Sep 17 00:00:00 2001 From: Iwona Just Date: Thu, 6 Jul 2023 14:05:39 +0100 Subject: [PATCH 1/3] added entrify event --- src/console/controllers/EntrifyController.php | 69 +++++++++++++++++++ src/events/EntrifyEvent.php | 34 +++++++++ 2 files changed, 103 insertions(+) create mode 100644 src/events/EntrifyEvent.php diff --git a/src/console/controllers/EntrifyController.php b/src/console/controllers/EntrifyController.php index 2da2d474d53..f999aaba039 100644 --- a/src/console/controllers/EntrifyController.php +++ b/src/console/controllers/EntrifyController.php @@ -14,8 +14,10 @@ use craft\db\Table; use craft\elements\Category; use craft\elements\Entry; +use craft\elements\GlobalSet; use craft\elements\Tag; use craft\elements\User; +use craft\events\EntrifyEvent; use craft\events\SectionEvent; use craft\fields\Categories; use craft\fields\Entries; @@ -39,6 +41,10 @@ */ class EntrifyController extends Controller { + // Events + // ------------------------------------------------------------------------- + public const EVENT_AFTER_ENTRIFY = 'onAfterEntrify'; + /** * @var string|null The section handle that entries should be saved in */ @@ -244,6 +250,7 @@ public function actionCategories(string $categoryGroup): int "deletePeerCategoryDrafts:$categoryGroup->uid" => "deletePeerEntryDrafts:$section->uid", ], $sectionCreated); + $fieldsConverted = false; if (!$projectConfigService->readOnly) { if (!$categoryGroup->dateDeleted && $this->confirm("Delete the “{$categoryGroup}” category group?", true)) { $this->do('Deleting category group', function() use ($categoryGroup) { @@ -278,6 +285,7 @@ public function actionCategories(string $categoryGroup): int $this->success(sprintf('Categories %s converted.', $total === 1 ? 'field' : 'fields')); $projectConfigChanged = true; + $fieldsConverted = true; } } } @@ -286,6 +294,27 @@ public function actionCategories(string $categoryGroup): int $this->_deployTip('categories', $categoryGroup->handle); } + // Fire a 'onAfterEntrify' event + if ($this->hasEventHandlers(self::EVENT_AFTER_ENTRIFY)) { + $event = new EntrifyEvent([ + 'elementType' => Category::class, + 'elementGroup' => [ + 'from' => $categoryGroup->id, + 'to' => [ + 'sectionId' => $section->id, + 'typeId' => $entryType->id, + ], + ], + 'fields' => [ + 'type' => Categories::class, + 'converted' => $fieldsConverted, + 'fields' => $fields ?? null, + ], + ]); + + $this->trigger(self::EVENT_AFTER_ENTRIFY, $event); + } + return ExitCode::OK; } @@ -384,6 +413,7 @@ public function actionTags(string $tagGroup): int $this->success('Tags converted.'); + $fieldsConverted = false; if (!$projectConfigService->readOnly) { if (!$tagGroup->dateDeleted && $this->confirm("Delete the “{$tagGroup}” tag group?", true)) { $this->do('Deleting tag group', function() use ($tagGroup) { @@ -417,6 +447,7 @@ public function actionTags(string $tagGroup): int $this->success(sprintf('Tags %s converted.', $total === 1 ? 'field' : 'fields')); $projectConfigChanged = true; + $fieldsConverted = true; } } } @@ -425,6 +456,27 @@ public function actionTags(string $tagGroup): int $this->_deployTip('tags', $tagGroup->handle); } + // Fire a 'onAfterEntrify' event + if ($this->hasEventHandlers(self::EVENT_AFTER_ENTRIFY)) { + $event = new EntrifyEvent([ + 'elementType' => Tag::class, + 'elementGroup' => [ + 'from' => $tagGroup->id, + 'to' => [ + 'sectionId' => $section->id, + 'typeId' => $entryType->id, + ], + ], + 'fields' => [ + 'type' => Tags::class, + 'converted' => $fieldsConverted, + 'fields' => $fields ?? null, + ], + ]); + + $this->trigger(self::EVENT_AFTER_ENTRIFY, $event); + } + return ExitCode::OK; } @@ -539,6 +591,23 @@ public function actionGlobalSet(string $globalSet): int $this->_deployTip('global-set', $globalSet->handle); } + // Fire a 'onAfterEntrify' event + if ($this->hasEventHandlers(self::EVENT_AFTER_ENTRIFY)) { + $event = new EntrifyEvent([ + 'elementType' => GlobalSet::class, + 'elementGroup' => [ + 'from' => $globalSet->id, + 'to' => [ + 'sectionId' => $section->id, + 'typeId' => $entryType->id, + ], + ], + 'fields' => [], + ]); + + $this->trigger(self::EVENT_AFTER_ENTRIFY, $event); + } + return ExitCode::OK; } diff --git a/src/events/EntrifyEvent.php b/src/events/EntrifyEvent.php new file mode 100644 index 00000000000..a56c4d810b0 --- /dev/null +++ b/src/events/EntrifyEvent.php @@ -0,0 +1,34 @@ + + * @since 4.4.16 + */ +class EntrifyEvent extends Event +{ + /** + * @var string Element type being entrified + */ + public string $elementType; + + /** + * @var array Array of from and to element identifiers + */ + public array $elementGroup = []; + + /** + * @var array The array of fields that were entrified + */ + public array $fields = []; +} From feb3e94e60f91198a3e9e3291752b754285d91fd Mon Sep 17 00:00:00 2001 From: Iwona Just Date: Mon, 10 Jul 2023 14:38:05 +0100 Subject: [PATCH 2/3] adjust entrify event data --- src/console/controllers/EntrifyController.php | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/console/controllers/EntrifyController.php b/src/console/controllers/EntrifyController.php index f999aaba039..abd28f2c821 100644 --- a/src/console/controllers/EntrifyController.php +++ b/src/console/controllers/EntrifyController.php @@ -299,10 +299,13 @@ public function actionCategories(string $categoryGroup): int $event = new EntrifyEvent([ 'elementType' => Category::class, 'elementGroup' => [ - 'from' => $categoryGroup->id, + 'from' => [ + 'id' => $categoryGroup->id, + 'uid' => $categoryGroup->uid, + ], 'to' => [ - 'sectionId' => $section->id, - 'typeId' => $entryType->id, + 'section' => $section->uid, + 'entryType' => $entryType->uid, ], ], 'fields' => [ @@ -461,10 +464,13 @@ public function actionTags(string $tagGroup): int $event = new EntrifyEvent([ 'elementType' => Tag::class, 'elementGroup' => [ - 'from' => $tagGroup->id, + 'from' => [ + 'id' => $tagGroup->id, + 'uid' => $tagGroup->uid, + ], 'to' => [ - 'sectionId' => $section->id, - 'typeId' => $entryType->id, + 'section' => $section->uid, + 'entryType' => $entryType->uid, ], ], 'fields' => [ @@ -596,10 +602,13 @@ public function actionGlobalSet(string $globalSet): int $event = new EntrifyEvent([ 'elementType' => GlobalSet::class, 'elementGroup' => [ - 'from' => $globalSet->id, + 'from' => [ + 'id' => $globalSet->id, + 'uid' => $globalSet->uid, + ], 'to' => [ - 'sectionId' => $section->id, - 'typeId' => $entryType->id, + 'section' => $section->uid, + 'entryType' => $entryType->uid, ], ], 'fields' => [], From acd4a1888eb138d22ab9cbfff332fe03c775c564 Mon Sep 17 00:00:00 2001 From: Iwona Just Date: Mon, 17 Jul 2023 10:09:57 +0100 Subject: [PATCH 3/3] separate events for entrification --- src/console/controllers/EntrifyController.php | 90 +++++++------------ src/events/EntrifyCategoriesEvent.php | 47 ++++++++++ src/events/EntrifyEvent.php | 34 ------- src/events/EntrifyGlobalSetEvent.php | 37 ++++++++ src/events/EntrifyTagsEvent.php | 47 ++++++++++ 5 files changed, 162 insertions(+), 93 deletions(-) create mode 100644 src/events/EntrifyCategoriesEvent.php delete mode 100644 src/events/EntrifyEvent.php create mode 100644 src/events/EntrifyGlobalSetEvent.php create mode 100644 src/events/EntrifyTagsEvent.php diff --git a/src/console/controllers/EntrifyController.php b/src/console/controllers/EntrifyController.php index abd28f2c821..e0ef71a3a66 100644 --- a/src/console/controllers/EntrifyController.php +++ b/src/console/controllers/EntrifyController.php @@ -14,10 +14,11 @@ use craft\db\Table; use craft\elements\Category; use craft\elements\Entry; -use craft\elements\GlobalSet; use craft\elements\Tag; use craft\elements\User; -use craft\events\EntrifyEvent; +use craft\events\EntrifyCategoriesEvent; +use craft\events\EntrifyGlobalSetEvent; +use craft\events\EntrifyTagsEvent; use craft\events\SectionEvent; use craft\fields\Categories; use craft\fields\Entries; @@ -43,7 +44,9 @@ class EntrifyController extends Controller { // Events // ------------------------------------------------------------------------- - public const EVENT_AFTER_ENTRIFY = 'onAfterEntrify'; + public const EVENT_ENTRIFY_CATEGORIES = 'onEntrifyCategories'; + public const EVENT_ENTRIFY_TAGS = 'onEntrifyTags'; + public const EVENT_ENTRIFY_GLOBAL_SET = 'onEntrifyGlobalSet'; /** * @var string|null The section handle that entries should be saved in @@ -294,28 +297,17 @@ public function actionCategories(string $categoryGroup): int $this->_deployTip('categories', $categoryGroup->handle); } - // Fire a 'onAfterEntrify' event - if ($this->hasEventHandlers(self::EVENT_AFTER_ENTRIFY)) { - $event = new EntrifyEvent([ - 'elementType' => Category::class, - 'elementGroup' => [ - 'from' => [ - 'id' => $categoryGroup->id, - 'uid' => $categoryGroup->uid, - ], - 'to' => [ - 'section' => $section->uid, - 'entryType' => $entryType->uid, - ], - ], - 'fields' => [ - 'type' => Categories::class, - 'converted' => $fieldsConverted, - 'fields' => $fields ?? null, - ], + // Fire a 'onEntrifyCategories' event + if ($this->hasEventHandlers(self::EVENT_ENTRIFY_CATEGORIES)) { + $event = new EntrifyCategoriesEvent([ + 'categoryGroup' => $categoryGroup, + 'section' => $section, + 'entryType' => $entryType, + 'fieldsConverted' => $fieldsConverted, + 'fields' => $fields ?? null, ]); - $this->trigger(self::EVENT_AFTER_ENTRIFY, $event); + $this->trigger(self::EVENT_ENTRIFY_CATEGORIES, $event); } return ExitCode::OK; @@ -459,28 +451,17 @@ public function actionTags(string $tagGroup): int $this->_deployTip('tags', $tagGroup->handle); } - // Fire a 'onAfterEntrify' event - if ($this->hasEventHandlers(self::EVENT_AFTER_ENTRIFY)) { - $event = new EntrifyEvent([ - 'elementType' => Tag::class, - 'elementGroup' => [ - 'from' => [ - 'id' => $tagGroup->id, - 'uid' => $tagGroup->uid, - ], - 'to' => [ - 'section' => $section->uid, - 'entryType' => $entryType->uid, - ], - ], - 'fields' => [ - 'type' => Tags::class, - 'converted' => $fieldsConverted, - 'fields' => $fields ?? null, - ], + // Fire a 'onEntrifyTags' event + if ($this->hasEventHandlers(self::EVENT_ENTRIFY_TAGS)) { + $event = new EntrifyTagsEvent([ + 'tagGroup' => $tagGroup, + 'section' => $section, + 'entryType' => $entryType, + 'fieldsConverted' => $fieldsConverted, + 'fields' => $fields ?? null, ]); - $this->trigger(self::EVENT_AFTER_ENTRIFY, $event); + $this->trigger(self::EVENT_ENTRIFY_TAGS, $event); } return ExitCode::OK; @@ -597,24 +578,15 @@ public function actionGlobalSet(string $globalSet): int $this->_deployTip('global-set', $globalSet->handle); } - // Fire a 'onAfterEntrify' event - if ($this->hasEventHandlers(self::EVENT_AFTER_ENTRIFY)) { - $event = new EntrifyEvent([ - 'elementType' => GlobalSet::class, - 'elementGroup' => [ - 'from' => [ - 'id' => $globalSet->id, - 'uid' => $globalSet->uid, - ], - 'to' => [ - 'section' => $section->uid, - 'entryType' => $entryType->uid, - ], - ], - 'fields' => [], + // Fire a 'onEntrifyTags' event + if ($this->hasEventHandlers(self::EVENT_ENTRIFY_GLOBAL_SET)) { + $event = new EntrifyGlobalSetEvent([ + 'globalSet' => $globalSet, + 'section' => $section, + 'entryType' => $entryType, ]); - $this->trigger(self::EVENT_AFTER_ENTRIFY, $event); + $this->trigger(self::EVENT_ENTRIFY_GLOBAL_SET, $event); } return ExitCode::OK; diff --git a/src/events/EntrifyCategoriesEvent.php b/src/events/EntrifyCategoriesEvent.php new file mode 100644 index 00000000000..f59229041a3 --- /dev/null +++ b/src/events/EntrifyCategoriesEvent.php @@ -0,0 +1,47 @@ + + * @since 4.4.16 + */ +class EntrifyCategoriesEvent extends Event +{ + /** + * @var CategoryGroup Category group being entrified + */ + public CategoryGroup $categoryGroup; + + /** + * @var Section Section used for entrification + */ + public Section $section; + + /** + * @var EntryType Entry type used for entrification + */ + public EntryType $entryType; + + /** + * @var bool Whether fields were entrified + */ + public bool $fieldsConverted; + + /** + * @var array The array of fields that were entrified + */ + public array $fields = []; +} diff --git a/src/events/EntrifyEvent.php b/src/events/EntrifyEvent.php deleted file mode 100644 index a56c4d810b0..00000000000 --- a/src/events/EntrifyEvent.php +++ /dev/null @@ -1,34 +0,0 @@ - - * @since 4.4.16 - */ -class EntrifyEvent extends Event -{ - /** - * @var string Element type being entrified - */ - public string $elementType; - - /** - * @var array Array of from and to element identifiers - */ - public array $elementGroup = []; - - /** - * @var array The array of fields that were entrified - */ - public array $fields = []; -} diff --git a/src/events/EntrifyGlobalSetEvent.php b/src/events/EntrifyGlobalSetEvent.php new file mode 100644 index 00000000000..a4fa8004f0b --- /dev/null +++ b/src/events/EntrifyGlobalSetEvent.php @@ -0,0 +1,37 @@ + + * @since 4.4.16 + */ +class EntrifyGlobalSetEvent extends Event +{ + /** + * @var GlobalSet Global set being entrified + */ + public GlobalSet $globalSet; + + /** + * @var Section Section used for entrification + */ + public Section $section; + + /** + * @var EntryType Entry type used for entrification + */ + public EntryType $entryType; +} diff --git a/src/events/EntrifyTagsEvent.php b/src/events/EntrifyTagsEvent.php new file mode 100644 index 00000000000..82e8017cdda --- /dev/null +++ b/src/events/EntrifyTagsEvent.php @@ -0,0 +1,47 @@ + + * @since 4.4.16 + */ +class EntrifyTagsEvent extends Event +{ + /** + * @var TagGroup Tag group being entrified + */ + public TagGroup $tagGroup; + + /** + * @var Section Section used for entrification + */ + public Section $section; + + /** + * @var EntryType Entry type used for entrification + */ + public EntryType $entryType; + + /** + * @var bool Whether fields were entrified + */ + public bool $fieldsConverted; + + /** + * @var array The array of fields that were entrified + */ + public array $fields = []; +}