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 Builder implements NamedBuilder |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private $config = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $layouts = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Field Name |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $name; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $name |
30
|
|
|
* @param array $args field configuration |
31
|
|
|
*/ |
32
|
|
|
public function __construct($name, $args = []) |
33
|
|
|
{ |
34
|
|
|
$this->name = $name; |
35
|
|
|
$this->config = array_merge( |
36
|
|
|
[ |
37
|
|
|
'key' => $name, |
38
|
|
|
'name' => $name, |
39
|
|
|
'label' => $this->generateLabel($name), |
40
|
|
|
'type' => 'flexible_content', |
41
|
|
|
], |
42
|
|
|
$args |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
if (!isset($this->config['button'])) { |
46
|
|
|
$this->config['button'] = 'Add '.rtrim($this->config['label'], 's'); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Return a configuration array |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function build() |
55
|
|
|
{ |
56
|
|
|
return array_merge($this->config, [ |
57
|
|
|
'layouts' => $this->buildLayouts(), |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Return a configuration array for each layout |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
private function buildLayouts() |
66
|
|
|
{ |
67
|
|
|
return array_map(function ($layout) { |
68
|
|
|
$layout = ($layout instanceof Builder) ? $layout->build() : $layout; |
69
|
|
|
return $this->transformLayout($layout); |
70
|
|
|
}, $this->getLayouts()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Apply transformations to a layout |
75
|
|
|
* @param array $layout Layout configuration array |
76
|
|
|
* @return array Transformed layout configuration array |
77
|
|
|
*/ |
78
|
|
|
private function transformLayout($layout) |
79
|
|
|
{ |
80
|
|
|
$layoutTransform = new Transform\FlexibleContentLayout($this); |
81
|
|
|
$namespaceTransform = new Transform\NamespaceFieldKey($this); |
82
|
|
|
|
83
|
|
|
return |
84
|
|
|
$namespaceTransform->transform( |
85
|
|
|
$layoutTransform->transform($layout) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string Flexible Content field name |
91
|
|
|
*/ |
92
|
|
|
public function getName() |
93
|
|
|
{ |
94
|
|
|
return $this->name; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Add a layout, which is a FieldsBuilder. `addLayout` can be chained to add |
99
|
|
|
* multiple layouts to the Flexible Content field. |
100
|
|
|
* @param string|FieldsBuilder $layout layout name. |
101
|
|
|
* Alternatively supply a FieldsBuilder to reuse existing fields. The name |
102
|
|
|
* will be inferred from the FieldsBuilder's name. |
103
|
|
|
* @param array $args filed configuration |
104
|
|
|
* @return FieldsBuilder |
105
|
|
|
*/ |
106
|
|
|
public function addLayout($layout, $args = []) |
107
|
|
|
{ |
108
|
|
|
if ($layout instanceof FieldsBuilder) { |
109
|
|
|
$layout = clone $layout; |
110
|
|
|
} else { |
111
|
|
|
$layout = new FieldsBuilder($layout, $args); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$layout = $this->configureLayout($layout, $args); |
115
|
|
|
$this->pushLayout($layout); |
116
|
|
|
|
117
|
|
|
return $layout; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Configures the layout FieldsBuilder |
122
|
|
|
* @param FieldsBuilder $layout |
123
|
|
|
* @param array $args FieldGroup Configuration |
124
|
|
|
* @return FieldsBuilder Configured Layout |
125
|
|
|
*/ |
126
|
|
|
protected function configureLayout(FieldsBuilder $layout, $args = []) |
127
|
|
|
{ |
128
|
|
|
$layout->setGroupConfig('name', $layout->getName()); |
129
|
|
|
$layout->setGroupConfig('display', 'block'); |
130
|
|
|
|
131
|
|
|
foreach ($args as $key => $value) { |
132
|
|
|
$layout->setGroupConfig($key, $value); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$layout->setParentContext($this); |
136
|
|
|
|
137
|
|
|
return $layout; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* End the current Flexible Content field, return to parent context |
142
|
|
|
* @return Builder |
143
|
|
|
*/ |
144
|
|
|
public function endFlexibleContent() |
145
|
|
|
{ |
146
|
|
|
return $this->getParentContext(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Add layout to internal array |
151
|
|
|
* @param FieldsBuilder $layout |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
|
|
protected function pushLayout($layout) |
155
|
|
|
{ |
156
|
|
|
$this->layouts[] = $layout; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return array[FieldsBuilder] |
|
|
|
|
161
|
|
|
*/ |
162
|
|
|
public function getLayouts() |
163
|
|
|
{ |
164
|
|
|
return $this->layouts; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Generate a label based on the name. Title case. |
169
|
|
|
* @param string $name |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
protected function generateLabel($name) |
173
|
|
|
{ |
174
|
|
|
return ucwords(str_replace("_", " ", $name)); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
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.