From 3489dc4e1225aa5754be8928bba7082a137f71d6 Mon Sep 17 00:00:00 2001 From: Bart van Gennep Date: Sun, 18 Nov 2018 23:06:25 +0100 Subject: [PATCH 1/3] Delete empty field groups on import with force --- CHANGELOG.md | 2 ++ src/DataTypes/FieldDataType.php | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fae367b..afb3a1bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,6 @@ ### 4.0.18 +### Added +- Delete empty field groups on import with force ### Fixed - Fixed user field sources not being exported diff --git a/src/DataTypes/FieldDataType.php b/src/DataTypes/FieldDataType.php index 9490c533..7f4ee4b3 100644 --- a/src/DataTypes/FieldDataType.php +++ b/src/DataTypes/FieldDataType.php @@ -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); + } + } } } From 1a4150ee6d8bf54311a3c1ed60cc66f8dde741c3 Mon Sep 17 00:00:00 2001 From: Bart van Gennep Date: Sun, 18 Nov 2018 23:11:07 +0100 Subject: [PATCH 2/3] Fixed import of multiple sites in same sitegroup by clearing sitegroup cache. --- CHANGELOG.md | 1 + src/Converters/Models/Site.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index afb3a1bd..08bb0398 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Delete empty field 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 diff --git a/src/Converters/Models/Site.php b/src/Converters/Models/Site.php index 0044cad3..6e0354ed 100644 --- a/src/Converters/Models/Site.php +++ b/src/Converters/Models/Site.php @@ -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; @@ -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); + } + } } From 68c2942aa2a050d23d5fb26ce96dc274389b7e54 Mon Sep 17 00:00:00 2001 From: Bart van Gennep Date: Sun, 18 Nov 2018 23:15:53 +0100 Subject: [PATCH 3/3] Delete empty site groups on import with force --- CHANGELOG.md | 2 +- src/DataTypes/SiteDataType.php | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08bb0398..eb1831bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/DataTypes/SiteDataType.php b/src/DataTypes/SiteDataType.php index f733c962..953b67b5 100644 --- a/src/DataTypes/SiteDataType.php +++ b/src/DataTypes/SiteDataType.php @@ -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); @@ -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); + } + } + } }