Skip to content

Commit

Permalink
Merge branch 'master' into record_index_improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bvangennep authored Nov 19, 2018
2 parents 576e49a + ff08c27 commit 4b2ffc8
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 7 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
### 4.1.0 - 2018-11-06
### 4.1.0 - 2018-11-19
### Added
- Added more flexibility for getting a record's index

### 4.0.18 - 2018-11-19
### Added
- Delete empty field and site groups on import with force
### Fixed
- Fixed user field sources not being exported
- Fixed category field layout not properly importing for existing categories
- Fixed import of multiple sites in same sitegroup

### 4.0.17 - 2018-09-24
### Fixed
- Fixed backwards compatibility of element index mapper
Expand Down
17 changes: 14 additions & 3 deletions src/Behaviors/FieldLayoutBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,16 @@ public function getFieldLayoutDefinition(FieldLayout $fieldLayout): array
$tabDefinitions[$tab->name] = $this->getFieldLayoutFieldsDefinition($tab->getFields());
}

return ['tabs' => $tabDefinitions];
return [
'type' => $fieldLayout->type,
'tabs' => $tabDefinitions
];
}

return ['fields' => $this->getFieldLayoutFieldsDefinition($fieldLayout->getFields())];
return [
'type' => $fieldLayout->type,
'fields' => $this->getFieldLayoutFieldsDefinition($fieldLayout->getFields()),
];
}

/**
Expand Down Expand Up @@ -87,7 +93,12 @@ public function getFieldLayout(array $fieldLayoutDef): FieldLayout
}

$fieldLayout = Craft::$app->fields->assembleLayout($layoutFields, $requiredFields);
$fieldLayout->type = Entry::class;

if (array_key_exists('type', $fieldLayoutDef)) {
$fieldLayout->type = $fieldLayoutDef['type'];
} else {
$fieldLayout->type = Entry::class;
}

return $fieldLayout;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Behaviors/SourcesBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use yii\base\Behavior;
use craft\base\Model;
use craft\records\VolumeFolder;
use craft\fields\Users;
use NerdsAndCompany\Schematic\Schematic;
use NerdsAndCompany\Schematic\Events\SourceMappingEvent;

Expand Down Expand Up @@ -123,7 +124,7 @@ public function getSource(string $fieldType, string $source = null, string $inde
break;
case 'group':
case 'editCategories':
$service = 'Users' == $fieldType ? Craft::$app->userGroups : Craft::$app->categories;
$service = Users::class == $fieldType ? Craft::$app->userGroups : Craft::$app->categories;
$method = 'getGroupBy';
break;
case 'folder':
Expand Down
4 changes: 3 additions & 1 deletion src/Converters/Models/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ public function setRecordAttributes(Model &$record, array $definition, array $de

// Set field layout
if (array_key_exists('fieldLayout', $definition)) {
$record->setFieldLayout($this->getFieldLayout($definition['fieldLayout']));
$fieldLayout = $this->getFieldLayout($definition['fieldLayout']);
$fieldLayout->id = $record->fieldLayoutId;
$record->setFieldLayout($fieldLayout);
}

// Set site settings
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);
}
}
}
}
3 changes: 3 additions & 0 deletions tests/unit/Converters/Base/VolumeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Craft;
use craft\base\Field as FieldModel;
use craft\base\Volume as VolumeModel;
use craft\elements\Asset;
use craft\models\FieldLayout;
use craft\volumes\Local;
use Codeception\Test\Unit;
Expand Down Expand Up @@ -131,6 +132,7 @@ private function getMockVolume(int $volumeId)
$mockField->required = true;

$mockFieldLayout = $this->getMockBuilder(FieldLayout::class)->getMock();
$mockFieldLayout->type = Asset::class;

$mockFieldLayout->expects($this->any())
->method('getFields')
Expand Down Expand Up @@ -166,6 +168,7 @@ private function getMockVolumeDefinition(VolumeModel $mockVolume)
'sortOrder' => null,
],
'fieldLayout' => [
'type' => Asset::class,
'fields' => $fieldDefs,
],
];
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/Converters/Elements/GlobalSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ private function getMockGlobalSetDefinition(GlobalSetElement $mockGlobalSet)
'uri' => null,
],
'fieldLayout' => [
'type' => GlobalSetElement::class,
'fields' => $fieldDefs,
],
'site' => $mockGlobalSet->getSite()->handle,
Expand Down Expand Up @@ -209,6 +210,7 @@ private function getMockGlobalSet(int $setId, string $siteHandle = 'default')
->willReturn($mockField);

$mockFieldLayout = $this->getMockBuilder(FieldLayout::class)->getMock();
$mockFieldLayout->type = GlobalSetElement::class;

$mockFieldLayout->expects($this->any())
->method('getFields')
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/Converters/Models/CategoryGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace NerdsAndCompany\Schematic\Converters\Models;

use Craft;
use craft\elements\Category;
use craft\models\CategoryGroup as CategoryGroupModel;
use craft\models\CategoryGroup_SiteSettings;
use craft\models\FieldLayout;
Expand Down Expand Up @@ -155,6 +156,7 @@ private function getMockCategoryGroupDefinition(CategoryGroupModel $mockCategory
'maxLevels' => 3,
],
'fieldLayout' => [
'type' => Category::class,
'fields' => [],
],
'siteSettings' => [
Expand Down Expand Up @@ -189,6 +191,7 @@ private function getMockCategoryGroup(int $groupId)
]);

$mockFieldLayout = $this->getMockBuilder(FieldLayout::class)->getMock();
$mockFieldLayout->type = Category::class;

$mockGroup->expects($this->any())
->method('getFieldLayout')
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/Converters/Models/EntryTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Craft;
use craft\base\Field as FieldModel;
use craft\elements\Entry;
use craft\models\EntryType as EntryTypeModel;
use craft\models\FieldLayout;
use craft\models\FieldLayoutTab;
Expand Down Expand Up @@ -174,6 +175,7 @@ private function getMockFieldLayoutDefinition(FieldLayout $fieldLayout)
}

return [
'type' => Entry::class,
'tabs' => $tabsDef,
];
}
Expand All @@ -198,6 +200,8 @@ private function getMockEntryType(int $entryTypeId)
$mockField = $this->getMockField($entryTypeId);

$mockFieldLayout = $this->getMockBuilder(FieldLayout::class)->getMock();
$mockFieldLayout->type = Entry::class;

$mockFieldLayoutTab = $this->getMockBuilder(FieldLayoutTab::class)->getMock();
$mockFieldLayoutTab->name = 'Content';

Expand Down
2 changes: 2 additions & 0 deletions tests/unit/Mappers/UserSettingsMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ private function getUserSettingsDefinition()
'photoVolume' => 'volumeHandle',
],
'fieldLayout' => [
'type' => User::class,
'tabs' => [
'Content' => [
'fieldHandle1' => true,
Expand Down Expand Up @@ -169,6 +170,7 @@ private function setMockServicesForImport($saveLayout = true, $deleteLayoutsByTy
private function getMockFieldLayout()
{
$mockFieldLayout = $this->getMockBuilder(FieldLayout::class)->getMock();
$mockFieldLayout->type = User::class;
$mockFieldLayoutTab = $this->getMockBuilder(FieldLayoutTab::class)->getMock();
$mockFieldLayoutTab->name = 'Content';

Expand Down

0 comments on commit 4b2ffc8

Please sign in to comment.