Skip to content

Commit

Permalink
Repeater support
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Nov 10, 2024
1 parent 11d52ca commit 62558ef
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ ddev craft wp-import --help
- Radio Button
- Range
- Relationship
- Repeater
- Select
- Tab
- Taxonomy
Expand Down
26 changes: 12 additions & 14 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,15 @@ public function acfLayoutTabsForEntity(string $type, string $name, FieldLayout $
return $tabs;
}

public function fieldsForEntity(string $type, string $name): array
{
$acfFields = [];
foreach ($this->fieldGroupsForEntity($type, $name) as $groupData) {
$acfFields = array_merge($acfFields, $groupData['fields']);
}
return $acfFields;
}

private function fieldGroupsForEntity(string $type, string $name): Generator
{
foreach ($this->wpInfo['field_groups'] as $groupData) {
Expand Down Expand Up @@ -427,7 +436,7 @@ private function locationMatchesRules(string $type, string $name, array $rules):
return false;
}

private function acfFieldElements(array $fields): array
public function acfFieldElements(array $fields): array
{
return Collection::make($fields)
->map(fn(array $fieldData) => match($fieldData['type']) {
Expand Down Expand Up @@ -501,18 +510,7 @@ private function acfAdapter(array $data): BaseAcfAdapter
return $this->acfAdapters[$data['type']];
}

public function prepareAcfFieldValues(string $type, string $slug, array $acfValues): array
{
// get all the fields from all matching field groups
$acfFields = [];
foreach ($this->fieldGroupsForEntity($type, $slug) as $groupData) {
$acfFields = array_merge($acfFields, $groupData['fields']);
}

return $this->prepareAcfFieldValuesInternal($acfFields, $acfValues);
}

private function prepareAcfFieldValuesInternal(array $acfFields, array $acfValues): array
public function prepareAcfFieldValues(array $acfFields, array $acfValues): array
{
$fieldValues = [];

Expand All @@ -529,7 +527,7 @@ private function prepareAcfFieldValuesInternal(array $acfFields, array $acfValue
if ($fieldData['type'] === 'group') {
$fieldValues = array_merge(
$fieldValues,
$this->prepareAcfFieldValuesInternal($fieldData['sub_fields'], is_array($fieldValue) ? $fieldValue : []),
$this->prepareAcfFieldValues($fieldData['sub_fields'], is_array($fieldValue) ? $fieldValue : []),
);
} else {
$handle = $this->normalizeAcfFieldHandle($fieldName);
Expand Down
87 changes: 87 additions & 0 deletions src/acfadapters/Repeater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license MIT
*/

namespace craft\wpimport\acfadapters;

use Craft;
use craft\base\FieldInterface;
use craft\fields\Matrix;
use craft\helpers\StringHelper;
use craft\models\EntryType;
use craft\models\FieldLayout;
use craft\models\FieldLayoutTab;
use craft\wpimport\BaseAcfAdapter;
use yii\console\Exception;

/**
* @author Pixel & Tonic, Inc. <[email protected]>
*/
class Repeater extends BaseAcfAdapter
{
public static function type(): string
{
return 'repeater';
}

public function create(array $data): FieldInterface
{
$entryType = $this->entryType($data);
$field = new Matrix();
$field->setEntryTypes([$entryType]);
$field->viewMode = $data['pagination'] ? Matrix::VIEW_MODE_INDEX : Matrix::VIEW_MODE_BLOCKS;
if ($data['min']) {
$field->minEntries = $data['min'];
}
if ($data['max']) {
$field->maxEntries = $data['max'];
}
if ($data['button_label'] && $data['button_label'] !== 'Add Row') {
$field->createButtonLabel = $data['button_label'];
}
return $field;
}

private function entryType(array $data): EntryType
{
$entryTypeHandle = sprintf('acf_%s_%s', StringHelper::toHandle($data['name']), $data['ID']);
$entryType = Craft::$app->entries->getEntryTypeByHandle($entryTypeHandle);
if ($entryType) {
return $entryType;
}

$entryType = new EntryType();
$entryType->name = $data['label'] ?: "Untitled ACF Field {$data['ID']}";
$entryType->handle = $entryTypeHandle;

$fieldLayout = new FieldLayout();
$fieldLayout->setTabs([
new FieldLayoutTab([
'layout' => $fieldLayout,
'name' => 'Content',
'elements' => $this->command->acfFieldElements($data['sub_fields']),
]),
]);
$entryType->setFieldLayout($fieldLayout);

$this->command->do("Creating `$entryType->name` entry type", function() use ($entryType) {
if (!Craft::$app->entries->saveEntryType($entryType)) {
throw new Exception(implode(', ', $entryType->getFirstErrors()));
}
});

return $entryType;
}

public function normalizeValue(mixed $value, array $data): mixed
{
$entryType = $this->entryType($data);
return array_map(fn(array $row) => [
'type' => $entryType->handle,
'fields' => $this->command->prepareAcfFieldValues($data['sub_fields'], $row),
], $value);
}
}
8 changes: 4 additions & 4 deletions src/importers/PostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ public function populate(ElementInterface $element, array $data): void
}

if (!empty($data['acf'])) {
$fieldValues = array_merge(
$fieldValues,
$this->command->prepareAcfFieldValues('post_type', $this->slug(), $data['acf']),
);
$fieldValues = array_merge($fieldValues, $this->command->prepareAcfFieldValues(
$this->command->fieldsForEntity('post_type', $this->slug()),
$data['acf'],
));
}

foreach ($fieldValues as $handle => $value) {
Expand Down
8 changes: 4 additions & 4 deletions src/importers/Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ public function populate(ElementInterface $element, array $data): void
];

if (!empty($data['acf'])) {
$fieldValues = array_merge(
$fieldValues,
$this->command->prepareAcfFieldValues('taxonomy', $this->slug(), $data['acf']),
);
$fieldValues = array_merge($fieldValues, $this->command->prepareAcfFieldValues(
$this->command->fieldsForEntity('taxonomy', $this->slug()),
$data['acf'],
));
}

foreach ($fieldValues as $handle => $value) {
Expand Down

0 comments on commit 62558ef

Please sign in to comment.