Skip to content

Commit

Permalink
Merge pull request #171 from nerds-and-company/feature/delete-empty-g…
Browse files Browse the repository at this point in the history
…roups-force

Feature/delete empty groups force
  • Loading branch information
bvangennep authored Nov 19, 2018
2 parents fa30b95 + 68c2942 commit 467c9d5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
### 4.0.18
### Added
- 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

### 4.0.17 - 2018-09-24
### Fixed
Expand Down
16 changes: 16 additions & 0 deletions src/Converters/Models/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public function deleteRecord(Model $record): bool
public function getGroupIdByName($name)
{
if (!isset($this->groups)) {
$this->resetCraftSitesServiceGroupsCache();

$this->groups = [];
foreach (Craft::$app->sites->getAllGroups() as $group) {
$this->groups[$group->name] = $group->id;
Expand All @@ -88,4 +90,18 @@ public function getGroupIdByName($name)

return $this->groups[$name];
}

/**
* Reset craft site service groups cache using reflection.
*/
private function resetCraftSitesServiceGroupsCache()
{
$obj = Craft::$app->sites;
$refObject = new \ReflectionObject($obj);
if ($refObject->hasProperty('_fetchedAllGroups')) {
$refProperty = $refObject->getProperty('_fetchedAllGroups');
$refProperty->setAccessible(true);
$refProperty->setValue($obj, false);
}
}
}
15 changes: 15 additions & 0 deletions src/DataTypes/FieldDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,20 @@ public function getRecords(): array
public function afterImport()
{
Craft::$app->fields->updateFieldVersion();
if (Schematic::$force) {
$this->clearEmptyGroups();
}
}

/**
* Clear empty field groups
*/
private function clearEmptyGroups()
{
foreach (Craft::$app->fields->getAllGroups() as $group) {
if (count($group->getFields()) == 0) {
Craft::$app->fields->deleteGroup($group);
}
}
}
}
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 467c9d5

Please sign in to comment.