-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
11d52ca
commit 62558ef
Showing
5 changed files
with
108 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters