87 protected static function getFieldIndex(array $formSettings,
string $fieldName): ?array
97 foreach ($formSettings as $columnIndex => $column)
99 if (!static::isValidColumnConfig($column))
104 foreach ($column[
'elements'] as $listIndex => $list)
106 if (!static::isValidElementListConfig($list))
110 if ($list[
'type'] !==
'section')
115 foreach ($list[
'elements'] as $rowIndex => $row)
117 if (!static::isValidRowConfig($row))
122 if ($row[
'name'] === $fieldName)
125 'COLUMN' => $columnIndex,
126 'LIST' => $listIndex,
183 protected static function isRowExists(array $formSettings, array $index): bool
185 if (empty($formSettings) || !static::checkRowIndex($index))
190 $column = $index[
'COLUMN'];
191 $list = $index[
'LIST'];
192 $row = $index[
'ROW'];
194 if (!isset($formSettings[$column]))
198 if (!isset($formSettings[$column][
'elements'][$list]))
202 if (!isset($formSettings[$column][
'elements'][$list][
'elements'][$row]))
245 protected static function setField(array $formSettings, array $index, array $field,
string $mode): array
247 if (empty($formSettings) || !static::checkRowIndex($index))
249 return $formSettings;
252 $column = $index[
'COLUMN'];
253 $list = $index[
'LIST'];
254 $row = $index[
'ROW'];
256 if (!isset($formSettings[$column]))
258 return $formSettings;
260 if (!isset($formSettings[$column][
'elements'][$list]))
262 return $formSettings;
265 !isset($formSettings[$column][
'elements'][$list][
'elements'])
266 || !is_array($formSettings[$column][
'elements'][$list][
'elements'])
269 return $formSettings;
276 $formSettings[$column][
'elements'][$list][
'elements'],
281 $formSettings[$column][
'elements'][$list][
'elements'][] = $field;
284 if (static::isRowExists($formSettings, $index))
289 $formSettings[$column][
'elements'][$list][
'elements'],
295 $before = array_slice($formSettings[$column][
'elements'][$list][
'elements'], 0, $row);
297 $after = array_slice($formSettings[$column][
'elements'][$list][
'elements'], $row);
298 $formSettings[$column][
'elements'][$list][
'elements'] = array_merge(
302 unset($after, $before);
307 if (static::isRowExists($formSettings, $index))
309 if ($row === count($formSettings[$column][
'elements'][$list][
'elements']))
311 $formSettings[$column][
'elements'][$list][
'elements'][] = $field;
315 $before = array_slice($formSettings[$column][
'elements'][$list][
'elements'], 0, $row + 1);
317 $after = array_slice($formSettings[$column][
'elements'][$list][
'elements'], $row + 1);
318 $formSettings[$column][
'elements'][$list][
'elements'] = array_merge(
322 unset($after, $before);
328 return $formSettings;
336 public static function addFormField(array $field,
string $parentId): void
338 if (empty($field) || !isset($field[
'name']) || !is_string($field[
'name']))
342 if ($parentId ===
'')
347 $config = static::getConfiguration();
348 $formSettings = $config->get(self::PRODUCT_FORM_ID, EntityEditorConfigScope::COMMON);
350 if (empty($formSettings) || !is_array($formSettings))
355 if (static::getFieldIndex($formSettings, $field[
'name']) !==
null)
360 $listIndex = static::getListIndex($formSettings, $parentId);
361 if ($listIndex ===
null)
366 $formSettings = static::setField($formSettings, $listIndex, $field, self::SET_MODE_LAST);
368 self::PRODUCT_FORM_ID,
370 [
'scope' => EntityEditorConfigScope::COMMON]