From 813051b6096a12c6be8a1a56ef0b0a282e1bc0fb Mon Sep 17 00:00:00 2001 From: bencroker Date: Thu, 2 May 2024 08:17:31 -0600 Subject: [PATCH] Content element migration for non primary sites --- CHANGELOG.md | 1 + src/Campaign.php | 2 +- ...nt_refactor_elements_non_primary_sites.php | 72 +++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/migrations/m240502_120000_content_refactor_elements_non_primary_sites.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 3356375d..a4697af6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Fixed +- Fixed a bug in which the content column was not being populated for elements in non primary sites after upgrading from Campaign 2 ([#470](https://github.com/putyourlightson/craft-campaign/issues/470)). - Fixed the PHPDoc type for relation field values. ## 3.1.1 - 2024-04-16 diff --git a/src/Campaign.php b/src/Campaign.php index a14d897b..e1db6450 100644 --- a/src/Campaign.php +++ b/src/Campaign.php @@ -180,7 +180,7 @@ public static function editions(): array /** * @inheritdoc */ - public string $schemaVersion = '3.0.0.3'; + public string $schemaVersion = '3.1.2'; /** * @inheritdoc diff --git a/src/migrations/m240502_120000_content_refactor_elements_non_primary_sites.php b/src/migrations/m240502_120000_content_refactor_elements_non_primary_sites.php new file mode 100644 index 00000000..ffcc6e10 --- /dev/null +++ b/src/migrations/m240502_120000_content_refactor_elements_non_primary_sites.php @@ -0,0 +1,72 @@ +getSites()->getPrimarySite()->id; + + foreach (Campaign::$plugin->campaignTypes->getAllCampaignTypes() as $campaignType) { + if ($campaignType->siteId !== $primarySiteId) { + $this->updateElements( + CampaignElement::find() + ->campaignType($campaignType) + ->siteId($campaignType->siteId) + ->ids(), + $campaignType->getFieldLayout(), + ); + } + } + + foreach (Campaign::$plugin->mailingListTypes->getAllMailingListTypes() as $mailingListType) { + if ($mailingListType->siteId !== $primarySiteId) { + $this->updateElements( + MailingListElement::find() + ->mailingListType($mailingListType) + ->siteId($mailingListType->siteId) + ->ids(), + $mailingListType->getFieldLayout(), + ); + } + } + + $this->updateElements( + SegmentElement::find() + ->siteId(['not', $primarySiteId]) + ->ids(), + Craft::$app->getFields()->getLayoutByType(SegmentElement::class), + ); + + $this->updateElements( + SendoutElement::find() + ->siteId(['not', $primarySiteId]) + ->ids(), + Craft::$app->getFields()->getLayoutByType(SendoutElement::class), + ); + + return true; + } + + /** + * @inheritdoc + */ + public function safeDown(): bool + { + echo self::class . " cannot be reverted.\n"; + + return false; + } +}