You are here

private static function ChartDataCollectorTable::tableColumnOperation in Charts 5.0.x

Same name and namespace in other branches
  1. 8.4 src/Element/ChartDataCollectorTable.php \Drupal\charts\Element\ChartDataCollectorTable::tableColumnOperation()

Performs add or delete operation on the table column.

Parameters

array $element_state: The element state storage.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

string $op: The operation.

null|int $index: The column index.

array $element_parents: The element parents.

Return value

array The updated element state storage.

1 call to ChartDataCollectorTable::tableColumnOperation()
ChartDataCollectorTable::tableOperationSubmit in src/Element/ChartDataCollectorTable.php
Submit callback for table add and delete operations.

File

src/Element/ChartDataCollectorTable.php, line 635

Class

ChartDataCollectorTable
Provides a chart data collector table form element.

Namespace

Drupal\charts\Element

Code

private static function tableColumnOperation(array $element_state, FormStateInterface $form_state, $op, $index = NULL, array $element_parents) {
  if ($op === 'delete') {
    foreach ($element_state['data_collector_table'] as $row_key => $columns) {
      $row = $element_state['data_collector_table'][$row_key];
      if (count(self::excludeWeightColumnFromRow($row)) === 1) {
        $element_state['data_collector_table'][$row_key][$index]['data'] = '';
      }
      else {
        array_splice($element_state['data_collector_table'][$row_key], $index, 1);

        // Making sure that the user input is updated as well.
        $user_input = $form_state
          ->getUserInput();
        $values = NestedArray::getValue($form_state
          ->getUserInput(), $element_parents);
        if (!empty($values['data_collector_table'][$row_key][$index])) {
          array_splice($values['data_collector_table'][$row_key], $index, 1);
        }
        NestedArray::setValue($user_input, $element_parents, $values);
        $form_state
          ->setUserInput($user_input);
      }
    }
  }
  else {
    foreach ($element_state['data_collector_table'] as $row_key => $columns) {
      $element_state['data_collector_table'][$row_key][]['data'] = '';
    }
  }
  return $element_state;
}