-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Content element migration for non primary sites
- Loading branch information
Showing
3 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/migrations/m240502_120000_content_refactor_elements_non_primary_sites.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace putyourlightson\campaign\migrations; | ||
|
||
use Craft; | ||
use craft\migrations\BaseContentRefactorMigration; | ||
use putyourlightson\campaign\Campaign; | ||
use putyourlightson\campaign\elements\CampaignElement; | ||
use putyourlightson\campaign\elements\MailingListElement; | ||
use putyourlightson\campaign\elements\SegmentElement; | ||
use putyourlightson\campaign\elements\SendoutElement; | ||
|
||
class m240502_120000_content_refactor_elements_non_primary_sites extends BaseContentRefactorMigration | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function safeUp(): bool | ||
{ | ||
$primarySiteId = Craft::$app->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; | ||
} | ||
} |