-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlexibleContent.php
99 lines (89 loc) · 3.22 KB
/
FlexibleContent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?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\ArrayHelper;
use craft\helpers\StringHelper;
use craft\models\EntryType;
use craft\models\FieldLayout;
use craft\models\FieldLayoutTab;
use craft\wpimport\BaseAcfAdapter;
use Illuminate\Support\Arr;
use yii\console\Exception;
/**
* @author Pixel & Tonic, Inc. <[email protected]>
*/
class FlexibleContent extends BaseAcfAdapter
{
public static function type(): string
{
return 'flexible_content';
}
public function create(array $data): FieldInterface
{
$entryTypes = array_map(fn(array $layoutData) => $this->entryType($data, $layoutData), $data['layouts']);
$field = new Matrix();
$field->setEntryTypes($entryTypes);
$field->viewMode = ($data['pagination'] ?? false) ? Matrix::VIEW_MODE_INDEX : Matrix::VIEW_MODE_BLOCKS;
if ($data['min'] ?? false) {
$field->minEntries = $data['min'];
}
if ($data['max'] ?? false) {
$field->maxEntries = $data['max'];
}
if (($data['button_label'] ?? false) && $data['button_label'] !== 'Add Row') {
$field->createButtonLabel = $data['button_label'];
}
return $field;
}
private function entryType(array $fieldData, array $layoutData): EntryType
{
$entryTypeHandle = sprintf(
'acf_%s_%s_%s',
StringHelper::toHandle($fieldData['name']), $fieldData['ID'], $layoutData['name'],
);
$entryType = Craft::$app->entries->getEntryTypeByHandle($entryTypeHandle);
if ($entryType) {
return $entryType;
}
$entryType = new EntryType();
$entryType->name = $layoutData['label'] ?: "Untitled ACF Field {$layoutData['ID']}";
$entryType->handle = $entryTypeHandle;
$fieldLayout = new FieldLayout();
$fieldLayout->setTabs([
new FieldLayoutTab([
'layout' => $fieldLayout,
'name' => 'Content',
'elements' => $this->command->acfFieldElements($layoutData['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
{
return array_map(function(array $row) use ($data) {
$layoutName = ArrayHelper::remove($row, 'acf_fc_layout');
$layoutData = Arr::first(
$data['layouts'],
fn(array $layoutData) => $layoutData['name'] === $layoutName,
);
$entryType = $this->entryType($data, $layoutData);
return [
'type' => $entryType->handle,
'fields' => $this->command->prepareAcfFieldValues($layoutData['sub_fields'], $row),
];
}, $value);
}
}