Skip to content

Commit

Permalink
Merge pull request #170 from nerds-and-company/feature/category-field…
Browse files Browse the repository at this point in the history
…-layout

Category field layout fixes
  • Loading branch information
bvangennep committed Nov 19, 2018
2 parents 467c9d5 + 74c5610 commit ff08c27
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
### 4.0.18
### 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
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
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
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 ff08c27

Please sign in to comment.