Skip to content

Commit

Permalink
Adds Redirect Element migration
Browse files Browse the repository at this point in the history
  • Loading branch information
BenParizek committed May 27, 2024
1 parent 1270c47 commit f8fbcb1
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/redirects/migrations/m240526_000000_content_refactor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace BarrelStrength\Sprout\redirects\migrations;

use BarrelStrength\Sprout\redirects\components\elements\RedirectElement;
use BarrelStrength\Sprout\redirects\RedirectsModule;
use Craft;
use craft\db\Query;
use craft\migrations\BaseContentRefactorMigration;
use craft\models\FieldLayout;

class m240526_000000_content_refactor extends BaseContentRefactorMigration
{
public const REDIRECTS_TABLE = '{{%sprout_redirects}}';
public const REDIRECT_ELEMENT_TYPE = 'BarrelStrength\Sprout\redirects\components\elements\RedirectElement';

public function safeUp(): void
{
$fieldLayouts = Craft::$app->projectConfig->get('sprout.sprout-module-redirects.fieldLayouts');

if ($fieldLayout = reset($fieldLayouts)) {
$layout = FieldLayout::createFromConfig($fieldLayout);
} else {
$layout = new FieldLayout([
'type' => self::REDIRECT_ELEMENT_TYPE,
]);
}

// Update Redirect Elements
$this->updateElements(
(new Query())->from(self::REDIRECTS_TABLE),
$layout,
);
}

public function safeDown(): bool
{
echo self::class . " cannot be reverted.\n";

return false;
}
}

0 comments on commit f8fbcb1

Please sign in to comment.