You are here

public static function ChartDataCollectorTable::validateDataCollectorTable in Charts 5.0.x

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

Validates the data collected.

Parameters

array $element: The form element.

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

File

src/Element/ChartDataCollectorTable.php, line 346

Class

ChartDataCollectorTable
Provides a chart data collector table form element.

Namespace

Drupal\charts\Element

Code

public static function validateDataCollectorTable(array $element, FormStateInterface $form_state) {
  $parents = $element['#parents'];
  $value = $form_state
    ->getValue($parents);

  // Remove empty rows and unneeded keys.
  foreach ($value['data_collector_table'] as $row_key => $row) {
    if (!is_numeric($row_key)) {
      unset($value['data_collector_table'][$row_key]);
      continue;
    }
    foreach ($row as $column_key => $column) {
      if (!is_numeric($column_key)) {
        unset($value['data_collector_table'][$row_key][$column_key]);
      }
    }
  }
  unset($value['import']);
  $form_state
    ->setValue($parents, $value);
  if ($element['#required'] && empty($value['table_categories_identifier'])) {
    $form_state
      ->setError($element['table_categories_identifier'], t('Please select how categories should be identiefied.'));
  }
}