Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added entrify event #13391

Open
wants to merge 4 commits into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/console/controllers/EntrifyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*/
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -278,6 +285,7 @@ public function actionCategories(string $categoryGroup): int

$this->success(sprintf('Categories %s converted.', $total === 1 ? 'field' : 'fields'));
$projectConfigChanged = true;
$fieldsConverted = true;
}
}
}
Expand All @@ -286,6 +294,30 @@ 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,
],
]);

$this->trigger(self::EVENT_AFTER_ENTRIFY, $event);
}

return ExitCode::OK;
}

Expand Down Expand Up @@ -384,6 +416,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) {
Expand Down Expand Up @@ -417,6 +450,7 @@ public function actionTags(string $tagGroup): int

$this->success(sprintf('Tags %s converted.', $total === 1 ? 'field' : 'fields'));
$projectConfigChanged = true;
$fieldsConverted = true;
}
}
}
Expand All @@ -425,6 +459,30 @@ 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,
],
]);

$this->trigger(self::EVENT_AFTER_ENTRIFY, $event);
}

return ExitCode::OK;
}

Expand Down Expand Up @@ -539,6 +597,26 @@ 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' => [],
]);

$this->trigger(self::EVENT_AFTER_ENTRIFY, $event);
}

return ExitCode::OK;
}

Expand Down
34 changes: 34 additions & 0 deletions src/events/EntrifyEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\events;

use yii\base\Event;

/**
* Entrify event class.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @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 = [];
}