Skip to content

Commit

Permalink
Address code climate objections
Browse files Browse the repository at this point in the history
  • Loading branch information
dafeder committed Dec 2, 2024
1 parent d8e7b69 commit f01cc3a
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions modules/json_form_widget/src/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,43 @@ public function arrayActionButtonCallback(array &$form, FormStateInterface $form
*/
public function handleArrayElement(array $definition, ?array $data, FormStateInterface $form_state, array $context): array {
// Extract field name from field definition and min items from field schema.
$field_name = $definition['name'];
$min_items = $definition['schema']->minItems ?? 0;

$context_name = self::buildContextName($context);
$item_count = $this->getItemCount($context_name, count($data ?? []), $min_items, $form_state);
$is_required = in_array($field_name, $this->builder->getSchema()->required ?? []);
$is_required = in_array($definition['name'], $this->builder->getSchema()->required ?? []);

// Build the parent fieldset.
$element = $this->buildArrayParentElement($definition, $is_required, $context_name);

// Build the specified number of field item elements.
$items = [];
for ($i = 0; $i < $item_count; $i++) {
$property_required = $is_required && ($i < $min_items);
$items[] = $this->buildArrayItemElement($definition, $data[$i] ?? NULL, $form_state, array_merge($context, [$i]), $property_required);
$item = $this->buildArrayItemElement($definition, $data[$i] ?? NULL, $form_state, array_merge($context, [$i]));
$item['#required'] = $is_required && ($i < $min_items);
$items[] = $item;
}
$element[$definition['name']] = $items;
return $element;
}

// Build field element.
/**
* Build the parent fieldset for an array.
*
* @param array $definition
* Field definition.
* @param bool $is_required
* Whether the field is required.
* @param string $context_name
* Field context name.
*
* @return array
* Render array for the array parent element.
*/
protected function buildArrayParentElement($definition, $is_required, $context_name) {
$element = [
'#type' => 'fieldset',
'#title' => ($definition['schema']->title ?? $field_name),
'#title' => ($definition['schema']->title ?? $definition['name']),
'#description' => ($definition['schema']->description ?? ''),
'#description_display' => 'before',
'#prefix' => '<div id="' . self::buildWrapperIdentifier($context_name) . '">',
Expand All @@ -123,10 +142,9 @@ public function handleArrayElement(array $definition, ?array $data, FormStateInt
'actions' => [
'#type' => 'actions',
'actions' => [
'add' => $this->buildAction($this->t('Add one'), 'addOne', $field_name, $context_name),
'add' => $this->buildAction($this->t('Add one'), 'addOne', $definition['name'], $context_name),
],
],
$field_name => $items,
];
return $element;
}
Expand All @@ -142,22 +160,18 @@ public function handleArrayElement(array $definition, ?array $data, FormStateInt
* Form state.
* @param string[] $context
* Field context.
* @param bool $required
* Whether the field is required.
*
* @return array
* Render array for the array element.
*/
protected function buildArrayItemElement(array $definition, $data, FormStateInterface $form_state, array $context, bool $required): array {
protected function buildArrayItemElement(array $definition, $data, FormStateInterface $form_state, array $context): array {
// Use the simple or complex method depending on whether items are objects.
if (isset($definition['schema']->items->properties)) {
$element = $this->buildComplexArrayElement($definition, $data, $form_state, $context);
}
else {
$element = $this->buildSimpleArrayElement($definition, $data, $context);
}
// If we show the element on the form, it's required.
$element['#required'] = $required;
return $element;
}

Expand Down Expand Up @@ -335,10 +349,10 @@ protected function buildAction(string $title, string $method, string $parent, st
* @param string $context_name
* Data context.
*
* @return array{#type: string, remove: array}
* @return array
* Actions render array.
*/
protected function buildElementActions(string $parent, string $context_name) {
protected function buildElementActions(string $parent, string $context_name):array {
return [
'#type' => 'actions',
'remove' => $this->buildAction($this->t('Remove'), 'remove', $parent, $context_name),
Expand Down

0 comments on commit f01cc3a

Please sign in to comment.