protected function parseFieldsAndGroups(array $fields, array $generalConfig)
{
$acceptableFileTypes = $generalConfig['accept_file_types'];
$currentGroup = 'ungrouped';
$groups = [];
$hasGroups = false;
foreach ($fields as $key => $field) {
unset($fields[$key]);
$key = str_replace('-', '_', strtolower(Str::makeSafe($key, true)));
if (!isset($field['type']) || empty($field['type'])) {
$error = sprintf('Field "%s" has no "type" set.', $key);
throw new InvalidArgumentException($error);
}
// If field is a "file" type, make sure the 'extensions' are set, and it's an array.
if ($field['type'] == 'file' || $field['type'] == 'filelist') {
if (empty($field['extensions'])) {
$field['extensions'] = $acceptableFileTypes;
}
$field['extensions'] = (array) $field['extensions'];
}
// If field is an "image" type, make sure the 'extensions' are set, and it's an array.
if ($field['type'] == 'image' || $field['type'] == 'imagelist') {
if (empty($field['extensions'])) {
$field['extensions'] = array_intersect(['gif', 'jpg', 'jpeg', 'png'], $acceptableFileTypes);
}
$field['extensions'] = (array) $field['extensions'];
}
// Make indexed arrays into associative for select fields
// e.g.: [ 'yes', 'no' ] => { 'yes': 'yes', 'no': 'no' }
if ($field['type'] === 'select' && isset($field['values']) && Arr::isIndexed($field['values'])) {
$field['values'] = array_combine($field['values'], $field['values']);
}
if (!empty($field['group'])) {
$hasGroups = true;
}
// Make sure we have these keys and every field has a group set.
$field = array_replace(['class' => '', 'default' => '', 'group' => $currentGroup, 'label' => '', 'variant' => ''], $field);
// Collect group data for rendering.
// Make sure that once you started with group all following have that group, too.
$currentGroup = $field['group'];
$groups[$currentGroup] = 1;
$fields[$key] = $field;
// Repeating fields checks
if ($field['type'] === 'repeater') {
$fields[$key] = $this->parseFieldRepeaters($fields, $key);
if ($fields[$key] === null) {
unset($fields[$key]);
}
}
}
// Make sure the 'uses' of the slug is an array.
if (isset($fields['slug']) && isset($fields['slug']['uses'])) {
$fields['slug']['uses'] = (array) $fields['slug']['uses'];
}
return [$fields, $hasGroups ? array_keys($groups) : []];
}