Skip to content

Commit

Permalink
Cloned Matrix fields now have their entry types cloned, but fields on…
Browse files Browse the repository at this point in the history
… the entry type are reused and not cloned
  • Loading branch information
engram-design committed Jun 22, 2024
1 parent ef5fb33 commit cb4d73e
Showing 1 changed file with 29 additions and 44 deletions.
73 changes: 29 additions & 44 deletions src/services/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@
use craft\db\Query;
use craft\helpers\ArrayHelper;
use craft\helpers\Json;
use craft\helpers\StringHelper;
use craft\fields\Matrix;
use craft\models\EntryType;
use craft\models\FieldGroup;
use craft\models\FieldLayout;
use craft\models\FieldLayoutTab;

use yii\base\Component;

use Exception;

use benf\neo\Field as NeoField;
use benf\neo\elements\Block;

Expand Down Expand Up @@ -119,55 +123,36 @@ public function getUnusedFields(): array

public function processCloneMatrix(FieldInterface $originField): array
{
$blockTypes = [];
$entryTypes = [];

foreach ($originField->blockTypes as $i => $blockType) {
$fields = [];
$blockKey = 'new' . ($i + 1);
foreach ($originField->entryTypes as $i => $blockType) {
$entryType = new EntryType([
'name' => $blockType->name . ' ' . StringHelper::randomString(6),
'handle' => StringHelper::appendRandomString($blockType->handle, 6),
]);

foreach ($blockType->getCustomFields() as $j => $blockField) {
$fieldKey = 'new' . ($j + 1);
$width = 100;
// Clone the field layout
$fieldLayoutConfig = $blockType->getFieldLayout()->getConfig()['tabs'] ?? [];

$fieldLayout = $blockType->getFieldLayout();
$fieldLayoutElements = $fieldLayout->getTabs()[0]->elements ?? [];
foreach ($fieldLayoutConfig as $tabKey => $tab) {
$fieldLayoutConfig[$tabKey]['uid'] = StringHelper::UUID();

if ($fieldLayoutElements) {
$fieldLayoutElement = ArrayHelper::firstWhere($fieldLayoutElements, 'field.uid', $blockField->uid);
$width = (int)($fieldLayoutElement->width ?? 0) ?: 100;
foreach (($tab['elements'] ?? []) as $layoutElementKey => $layoutElement) {
$fieldLayoutConfig[$tabKey]['elements'][$layoutElementKey]['uid'] = StringHelper::UUID();
}
}

if ($blockField::class == 'verbb\supertable\fields\SuperTableField') {
$blockField->contentTable = $blockField->contentTable ?? '';
}
$fieldLayout = FieldLayout::createFromConfig(['tabs' => $fieldLayoutConfig]);
$entryType->setFieldLayout($fieldLayout);

$fields[$fieldKey] = [
'type' => $blockField::class,
'name' => $blockField['name'],
'handle' => $blockField['handle'],
'instructions' => $blockField['instructions'],
'required' => (bool)$blockField['required'],
'searchable' => (bool)$blockField['searchable'],
'translationMethod' => $blockField['translationMethod'],
'translationKeyFormat' => $blockField['translationKeyFormat'],
'typesettings' => Json::decode(Json::encode($blockField['settings'])),
'width' => $width,
];

if ($blockField::class == 'verbb\supertable\fields\SuperTableField') {
$fields['new' . ($j + 1)]['typesettings']['blockTypes'] = $this->processCloneSuperTable($blockField);
}
if (Craft::$app->getEntries()->saveEntryType($entryType)) {
$entryTypes[] = $entryType->id;
} else {
throw new Exception(Json::encode($entryType->getErrors()));
}

$blockTypes[$blockKey] = [
'name' => $blockType->name,
'handle' => $blockType->handle,
'sortOrder' => $blockType->sortOrder,
'fields' => $fields,
];
}

return $blockTypes;
return $entryTypes;
}

public function processCloneNeo(FieldInterface $originField): array
Expand Down Expand Up @@ -234,9 +219,9 @@ public function processCloneNeoGroups(FieldInterface $originField): array

public function processCloneSuperTable(FieldInterface $originField): array
{
$blockTypes = [];
$entryTypes = [];

foreach ($originField->blockTypes as $i => $blockType) {
foreach ($originField->entryTypes as $i => $blockType) {
$fields = [];

foreach ($blockType->getCustomFields() as $j => $blockField) {
Expand All @@ -257,16 +242,16 @@ public function processCloneSuperTable(FieldInterface $originField): array
];

if ($blockField::class == Matrix::class) {
$fields['new' . $j]['typesettings']['blockTypes'] = $this->processCloneMatrix($blockField);
$fields['new' . $j]['typesettings']['entryTypes'] = $this->processCloneMatrix($blockField);
}
}

$blockTypes['new' . $i] = [
$entryTypes['new' . $i] = [
'fields' => $fields,
];
}

return $blockTypes;
return $entryTypes;
}

public function createFieldLayoutFromConfig(array $config): FieldLayout
Expand Down

0 comments on commit cb4d73e

Please sign in to comment.