Skip to content

Commit

Permalink
Delete empty site groups on import with force
Browse files Browse the repository at this point in the history
  • Loading branch information
bvangennep committed Nov 18, 2018
1 parent 1a4150e commit 68c2942
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### 4.0.18
### Added
- Delete empty field groups on import with force
- Delete empty field and site groups on import with force
### Fixed
- Fixed user field sources not being exported
- Fixed import of multiple sites in same sitegroup
Expand Down
25 changes: 24 additions & 1 deletion src/DataTypes/SiteDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,20 @@ public function getRecords(): array
}

/**
* Reset craft site service sites cache using reflection.
* {@inheritdoc}
*/
public function afterImport()
{
$this->clearSiteCaches();
if (Schematic::$force) {
$this->clearEmptyGroups();
}
}

/**
* Reset craft site service sites cache using reflection.
*/
private function clearSiteCaches()
{
$obj = Craft::$app->sites;
$refObject = new \ReflectionObject($obj);
Expand All @@ -53,4 +64,16 @@ public function afterImport()
}
$obj->init(); // reload sites
}

/**
* Clear empty sute groups
*/
private function clearEmptyGroups()
{
foreach (Craft::$app->sites->getAllGroups() as $group) {
if (count($group->getSites()) == 0) {
Craft::$app->sites->deleteGroup($group);
}
}
}
}

0 comments on commit 68c2942

Please sign in to comment.