Skip to content

Commit

Permalink
Layout migration
Browse files Browse the repository at this point in the history
  • Loading branch information
engram-design committed Aug 12, 2020
1 parent 43ffb11 commit 7cc9045
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
19 changes: 14 additions & 5 deletions src/migrations/m200812_100000_layout_sortorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,23 @@ public function safeUp()
$this->addColumn('{{%cpnav_layout}}', 'sortOrder', $this->smallInteger()->unsigned()->after('permissions'));
}

$layouts = (new Query())
->from('{{%cpnav_layout}}')
->all();
$projectConfig = Craft::$app->getProjectConfig();

// Don't make the same config changes twice
$schemaVersion = $projectConfig->get('plugins.cp-nav.schemaVersion', true);
if (version_compare($schemaVersion, '2.0.7', '>=')) {
return;
}

// Populate `sortOrder`
$layouts = Craft::$app->getProjectConfig()->get(LayoutsService::CONFIG_LAYOUT_KEY);

$sortOrder = 0;

foreach ($layouts as $layout) {
$this->update('{{%cpnav_layout}}', ['sortOrder' => $sortOrder++], ['id' => $layout['id']]);
foreach ($layouts as $layoutUid => $layout) {
$layout['sortOrder'] = ++$sortOrder;

Craft::$app->getProjectConfig()->set(LayoutsService::CONFIG_LAYOUT_KEY . '.' . $layoutUid, $layout);
}

return true;
Expand Down
14 changes: 10 additions & 4 deletions src/services/LayoutsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,18 +281,24 @@ private function _getLayoutRecord(string $uid): LayoutRecord

private function _createLayoutQuery(): Query
{
return (new Query())
$query = (new Query())
->select([
'id',
'name',
'isDefault',
'permissions',
'sortOrder',
'dateUpdated',
'dateCreated',
'uid',
])
->from(['{{%cpnav_layout}}'])
->orderBy(['sortOrder' => SORT_ASC]);
->from(['{{%cpnav_layout}}']);

$schemaVersion = Craft::$app->getProjectConfig()->get('plugins.cp-nav.schemaVersion', true);
if (version_compare($schemaVersion, '2.0.7', '>=')) {
$query->addSelect(['sortOrder']);
$query->orderBy(['sortOrder' => SORT_ASC]);
}

return $query;
}
}

0 comments on commit 7cc9045

Please sign in to comment.