Skip to content

Commit

Permalink
Content element migration for non primary sites
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed May 2, 2024
1 parent 2e96ed5 commit 813051b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public static function editions(): array
/**
* @inheritdoc
*/
public string $schemaVersion = '3.0.0.3';
public string $schemaVersion = '3.1.2';

/**
* @inheritdoc
Expand Down
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;
}
}

0 comments on commit 813051b

Please sign in to comment.