1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace StoutLogic\AcfBuilder; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Create a configuration array for an ACF Flexible Content field. |
7
|
|
|
* A flexible content field can have many different `layouts` which are |
8
|
|
|
* groups of fields. |
9
|
|
|
*/ |
10
|
|
|
class FlexibleContentBuilder extends FieldBuilder |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private $layouts = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param string $name Field name |
19
|
|
|
* @param string $type Field name |
20
|
|
|
* @param array $config Field configuration |
21
|
|
|
*/ |
22
|
|
|
public function __construct($name, $type = 'flexible_content', $config = []) |
23
|
|
|
{ |
24
|
|
|
parent::__construct($name, $type, $config); |
25
|
|
|
|
26
|
|
|
if (!isset($config['button'])) { |
27
|
|
|
$this->setConfig('button', $this->getDefaultButtonLabel()); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Return a configuration array |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public function build() |
36
|
|
|
{ |
37
|
|
|
return array_merge(parent::build(), [ |
38
|
|
|
'layouts' => $this->buildLayouts(), |
39
|
|
|
]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Return a configuration array for each layout |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
private function buildLayouts() |
47
|
|
|
{ |
48
|
|
|
return array_map(function ($layout) { |
49
|
|
|
$layout = ($layout instanceof Builder) ? $layout->build() : $layout; |
50
|
|
|
return $this->transformLayout($layout); |
51
|
|
|
}, $this->getLayouts()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Apply transformations to a layout |
56
|
|
|
* @param array $layout Layout configuration array |
57
|
|
|
* @return array Transformed layout configuration array |
58
|
|
|
*/ |
59
|
|
|
private function transformLayout($layout) |
60
|
|
|
{ |
61
|
|
|
$layoutTransform = new Transform\FlexibleContentLayout($this); |
62
|
|
|
$namespaceTransform = new Transform\NamespaceFieldKey($this); |
63
|
|
|
|
64
|
|
|
return |
65
|
|
|
$namespaceTransform->transform( |
66
|
|
|
$layoutTransform->transform($layout) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Add a layout, which is a FieldsBuilder. `addLayout` can be chained to add |
72
|
|
|
* multiple layouts to the Flexible Content field. |
73
|
|
|
* @param string|FieldsBuilder $layout layout name. |
74
|
|
|
* Alternatively supply a FieldsBuilder to reuse existing fields. The name |
75
|
|
|
* will be inferred from the FieldsBuilder's name. |
76
|
|
|
* @param array $args filed configuration |
77
|
|
|
* @return FieldsBuilder |
78
|
|
|
*/ |
79
|
|
|
public function addLayout($layout, $args = []) |
80
|
|
|
{ |
81
|
|
|
if ($layout instanceof FieldsBuilder) { |
82
|
|
|
$layout = clone $layout; |
83
|
|
|
} else { |
84
|
|
|
$layout = new FieldsBuilder($layout, $args); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$layout = $this->initializeLayout($layout, $args); |
88
|
|
|
$this->pushLayout($layout); |
89
|
|
|
|
90
|
|
|
return $layout; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Configures the layout FieldsBuilder |
95
|
|
|
* @param FieldsBuilder $layout |
96
|
|
|
* @param array $args FieldGroup Configuration |
97
|
|
|
* @return FieldsBuilder Configured Layout |
98
|
|
|
*/ |
99
|
|
|
protected function initializeLayout(FieldsBuilder $layout, $args = []) |
100
|
|
|
{ |
101
|
|
|
$layout->setGroupConfig('name', $layout->getName()); |
102
|
|
|
$layout->setGroupConfig('display', 'block'); |
103
|
|
|
|
104
|
|
|
foreach ($args as $key => $value) { |
105
|
|
|
$layout->setGroupConfig($key, $value); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$layout->setParentContext($this); |
109
|
|
|
|
110
|
|
|
return $layout; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* End the current Flexible Content field, return to parent context |
115
|
|
|
* @return Builder |
116
|
|
|
*/ |
117
|
|
|
public function endFlexibleContent() |
118
|
|
|
{ |
119
|
|
|
return $this->getParentContext(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Add layout to internal array |
124
|
|
|
* @param FieldsBuilder $layout |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
protected function pushLayout($layout) |
128
|
|
|
{ |
129
|
|
|
$this->layouts[] = $layout; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return array[FieldsBuilder] |
|
|
|
|
134
|
|
|
*/ |
135
|
|
|
public function getLayouts() |
136
|
|
|
{ |
137
|
|
|
return $this->layouts; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
private function getDefaultButtonLabel() |
141
|
|
|
{ |
142
|
|
|
return 'Add '.rtrim($this->getLabel(), 's'); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.