Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
37 changes: 37 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

use Craft;
use craft\base\Model;
use craft\console\controllers\EntrifyController;
use craft\events\EntrifyCategoriesEvent;
use craft\events\EntrifyGlobalSetEvent;
use craft\events\EntrifyTagsEvent;
use craft\events\RegisterUrlRulesEvent;
use craft\feedme\base\PluginTrait;
use craft\feedme\models\Settings;
Expand Down Expand Up @@ -86,6 +90,7 @@ public function init(): void
$this->_registerCpRoutes();
$this->_registerTwigExtensions();
$this->_registerVariables();
$this->_listenToEvents();
}

/**
Expand Down Expand Up @@ -169,4 +174,36 @@ private function _registerVariables(): void
$event->sender->set('feedme', FeedMeVariable::class);
});
}

/**
* Listen to events
*
* @return void
*/
private function _listenToEvents(): void
{
Event::on(
EntrifyController::class,
EntrifyController::EVENT_ENTRIFY_CATEGORIES,
function(EntrifyCategoriesEvent $event) {
self::$plugin->feeds->entrifyCategoryFeeds($event);
}
);

Event::on(
EntrifyController::class,
EntrifyController::EVENT_ENTRIFY_TAGS,
function(EntrifyTagsEvent $event) {
self::$plugin->feeds->entrifyTagFeeds($event);
}
);

Event::on(
EntrifyController::class,
EntrifyController::EVENT_ENTRIFY_GLOBAL_SET,
function(EntrifyGlobalSetEvent $event) {
self::$plugin->feeds->entrifyGlobalSetFeeds($event);
}
);
}
}
83 changes: 83 additions & 0 deletions src/services/Feeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@
use craft\base\Component;
use craft\db\ActiveQuery;
use craft\db\Query;
use craft\elements\Category;
use craft\elements\Entry;
use craft\elements\GlobalSet;
use craft\elements\Tag;
use craft\events\EntrifyCategoriesEvent;
use craft\events\EntrifyGlobalSetEvent;
use craft\events\EntrifyTagsEvent;
use craft\feedme\errors\FeedException;
use craft\feedme\events\FeedEvent;
use craft\feedme\models\FeedModel;
use craft\feedme\records\FeedRecord;
use craft\helpers\Json;
use craft\models\EntryType;
use craft\models\Section;
use Exception;
use Throwable;

Expand Down Expand Up @@ -237,6 +246,39 @@ public function reorderFeeds(array $feedIds): bool
return true;
}

/**
* Adjust Category Feeds after entrification
*
* @param EntrifyCategoriesEvent $event
* @return void
*/
public function entrifyCategoryFeeds(EntrifyCategoriesEvent $event): void
{
$this->_entrifyFeeds(Category::class, $event->section, $event->entryType, $event->categoryGroup->id);
}

/**
* Adjust Tag Feeds after entrification
*
* @param EntrifyTagsEvent $event
* @return void
*/
public function entrifyTagFeeds(EntrifyTagsEvent $event): void
{
$this->_entrifyFeeds(Tag::class, $event->section, $event->entryType, $event->tagGroup->id);
}

/**
* Adjust Global Set Feeds after entrification
*
* @param EntrifyGlobalSetEvent $event
* @return void
*/
public function entrifyGlobalSetFeeds(EntrifyGlobalSetEvent $event): void
{
$this->_entrifyFeeds(GlobalSet::class, $event->section, $event->entryType, ['globalSet' => $event->globalSet->id]);
}


// Private Methods
// =========================================================================
Expand Down Expand Up @@ -320,4 +362,45 @@ private function _getFeedRecordById(int $feedId = null): FeedRecord

return $feedRecord;
}

/**
* Adjust the feeds after entrification.
*
* @param string $entrifiedElementClass class name of the entrified elements
* @param Section $section section to entrify to
* @param EntryType $entryType element type to entrify to
* @param int|array $entrifiedModelId id of the entrified model (category group, tag group or global set)
* @return void
*/
private function _entrifyFeeds(
string $entrifiedElementClass,
Section $section,
EntryType $entryType,
int|array $entrifiedModelId
): void
{
$allFeeds = $this->getFeeds();

foreach ($allFeeds as $feed) {
if ($feed->elementType === $entrifiedElementClass) {
// if, in the elementGroup, the key matches $elementType
// and the value for that key matches $elementGroup[from]
// (in case of GlobalSet, the value would be ['globalSet' => <globalSetId>])
if (isset($feed->elementGroup[$entrifiedElementClass]) &&
$feed->elementGroup[$entrifiedElementClass] == $entrifiedModelId) {
// change the elementType
$feed->elementType = Entry::class;
// in the elementGroup change the value for the $elementType key to an empty string
$feed->elementGroup[$entrifiedElementClass] = "";
// in the elementGroup change the value for the craft\elements\Entry key
$feed->elementGroup[Entry::class] = [
'section' => $section->id,
'entryType' => $entryType->id,
];

$this->saveFeed($feed);
}
}
}
}
}