You are here

public static function YamlFormMultiple::buildElementRow in YAML Form 8

Build a single element row.

Parameters

string $table_id: The element's table id.

int $row_index: The row index.

array $element: The element.

string $default_value: The default value.

int $weight: The weight.

array $ajax_settings: An array containing AJAX callback settings.

Return value

array A render array containing inputs for an element's value and weight.

1 call to YamlFormMultiple::buildElementRow()
YamlFormMultiple::processYamlFormMultiple in src/Element/YamlFormMultiple.php
Process items and build multiple elements widget.

File

src/Element/YamlFormMultiple.php, line 224

Class

YamlFormMultiple
Provides a form element to assist in creation of multiple elements.

Namespace

Drupal\yamlform\Element

Code

public static function buildElementRow($table_id, $row_index, array $element, $default_value, $weight, array $ajax_settings) {
  if ($element['#child_keys']) {
    foreach ($element['#child_keys'] as $child_key) {
      if (isset($default_value[$child_key])) {
        $element['#element'][$child_key]['#default_value'] = $default_value[$child_key];
      }
    }
  }
  else {
    $element['#element']['#default_value'] = $default_value;
  }
  $row = [];
  $row['_handle_'] = [];
  if ($element['#child_keys'] && !empty($element['#header'])) {
    foreach ($element['#child_keys'] as $child_key) {
      $row[$child_key] = $element['#element'][$child_key];
    }
  }
  else {
    $row['_item_'] = $element['#element'];
  }
  $row['weight'] = [
    '#type' => 'weight',
    '#delta' => 1000,
    '#title' => t('Item weight'),
    '#title_display' => 'invisible',
    '#attributes' => [
      'class' => [
        'yamlform-multiple-sort-weight',
      ],
    ],
    '#default_value' => $weight,
  ];

  // Allow users to add & remove rows if cardinality is not set.
  if (empty($element['#cardinality'])) {
    $row['_operations_'] = [];
    $row['_operations_']['add'] = [
      '#type' => 'image_button',
      '#src' => drupal_get_path('module', 'yamlform') . '/images/icons/plus.svg',
      '#limit_validation_errors' => [],
      '#submit' => [
        [
          get_called_class(),
          'addItemSubmit',
        ],
      ],
      '#ajax' => $ajax_settings,
      // Issue #1342066 Document that buttons with the same #value need a unique
      // #name for the form API to distinguish them, or change the form API to
      // assign unique #names automatically.
      '#row_index' => $row_index,
      '#name' => $table_id . '_add_' . $row_index,
    ];
    $row['_operations_']['remove'] = [
      '#type' => 'image_button',
      '#src' => drupal_get_path('module', 'yamlform') . '/images/icons/ex.svg',
      '#limit_validation_errors' => [],
      '#submit' => [
        [
          get_called_class(),
          'removeItemSubmit',
        ],
      ],
      '#ajax' => $ajax_settings,
      // Issue #1342066 Document that buttons with the same #value need a unique
      // #name for the form API to distinguish them, or change the form API to
      // assign unique #names automatically.
      '#row_index' => $row_index,
      '#name' => $table_id . '_remove_' . $row_index,
    ];
  }
  $row['#weight'] = $weight;
  $row['#attributes']['class'][] = 'draggable';
  return $row;
}