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

Bugfix/1340 update feeds after entrification #1347

Draft
wants to merge 5 commits into
base: 5.x
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
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 @@
$this->_registerCpRoutes();
$this->_registerTwigExtensions();
$this->_registerVariables();
$this->_listenToEvents();
}

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

/**
* Listen to events
*
* @return void
*/
private function _listenToEvents(): void
{
Event::on(
EntrifyController::class,
EntrifyController::EVENT_ENTRIFY_CATEGORIES,

Check failure on line 187 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Access to undefined constant craft\console\controllers\EntrifyController::EVENT_ENTRIFY_CATEGORIES.
function(EntrifyCategoriesEvent $event) {

Check failure on line 188 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Parameter $event of anonymous function has invalid type craft\events\EntrifyCategoriesEvent.
self::$plugin->feeds->entrifyCategoryFeeds($event);
}
);

Event::on(
EntrifyController::class,
EntrifyController::EVENT_ENTRIFY_TAGS,

Check failure on line 195 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Access to undefined constant craft\console\controllers\EntrifyController::EVENT_ENTRIFY_TAGS.
function(EntrifyTagsEvent $event) {

Check failure on line 196 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Parameter $event of anonymous function has invalid type craft\events\EntrifyTagsEvent.
self::$plugin->feeds->entrifyTagFeeds($event);
}
);

Event::on(
EntrifyController::class,
EntrifyController::EVENT_ENTRIFY_GLOBAL_SET,

Check failure on line 203 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Access to undefined constant craft\console\controllers\EntrifyController::EVENT_ENTRIFY_GLOBAL_SET.
function(EntrifyGlobalSetEvent $event) {

Check failure on line 204 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Parameter $event of anonymous function has invalid type craft\events\EntrifyGlobalSetEvent.
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 @@
return true;
}

/**
* Adjust Category Feeds after entrification
*
* @param EntrifyCategoriesEvent $event
* @return void
*/
public function entrifyCategoryFeeds(EntrifyCategoriesEvent $event): void

Check failure on line 255 in src/services/Feeds.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Parameter $event of method craft\feedme\services\Feeds::entrifyCategoryFeeds() has invalid type craft\events\EntrifyCategoriesEvent.
{
$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

Check failure on line 266 in src/services/Feeds.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Parameter $event of method craft\feedme\services\Feeds::entrifyTagFeeds() has invalid type craft\events\EntrifyTagsEvent.
{
$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

Check failure on line 277 in src/services/Feeds.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Parameter $event of method craft\feedme\services\Feeds::entrifyGlobalSetFeeds() has invalid type craft\events\EntrifyGlobalSetEvent.
{
$this->_entrifyFeeds(GlobalSet::class, $event->section, $event->entryType, ['globalSet' => $event->globalSet->id]);
}


// Private Methods
// =========================================================================
Expand Down Expand Up @@ -320,4 +362,45 @@

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);
}
}
}
}
}