You are here

private static function BaseSettings::processSeriesForm in Charts 8.4

Same name and namespace in other branches
  1. 5.0.x src/Element/BaseSettings.php \Drupal\charts\Element\BaseSettings::processSeriesForm()

Process series form.

Parameters

array $element: The current element.

array $options: The options.

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

Return value

array The element.

Throws

\Exception

1 call to BaseSettings::processSeriesForm()
BaseSettings::processSettings in src/Element/BaseSettings.php
Processes the settings element.

File

src/Element/BaseSettings.php, line 987

Class

BaseSettings
Provides a form element for setting a chart.

Namespace

Drupal\charts\Element

Code

private static function processSeriesForm(array $element, array $options, FormStateInterface $form_state) {

  // Chart preview.
  $parents = $element['#parents'];
  $id_prefix = implode('-', $parents);
  $element_state = ChartDataCollectorTable::getElementState($parents, $form_state);
  if (!$element_state) {
    $element_state = $options;

    // Closing preview here cause this is probably initial form load.
    $open_preview = FALSE;
    $element_state[$id_prefix . '__open_preview'] = $open_preview;
    ChartDataCollectorTable::setElementState($parents, $form_state, $element_state);
  }
  else {
    $open_preview = $element_state[$id_prefix . '__open_preview'];
  }
  $wrapper_id = $element['#wrapper_id'] . '--preview';
  $element['preview'] = [
    '#type' => 'details',
    '#title' => new TranslatableMarkup('Preview'),
    '#weight' => -99,
    '#open' => $open_preview,
    '#prefix' => '<div id="' . $wrapper_id . '">',
    '#suffix' => '</div>',
  ];
  $element['preview']['submit'] = [
    '#type' => 'submit',
    '#value' => new TranslatableMarkup('Update Preview'),
    '#name' => $id_prefix . '--preview-submit',
    '#attributes' => [
      'class' => [
        Html::cleanCssIdentifier($id_prefix . '--preview-submit'),
      ],
    ],
    '#submit' => [
      [
        get_called_class(),
        'chartPreviewSubmit',
      ],
    ],
    '#limit_validation_errors' => [
      $parents,
    ],
    '#ajax' => [
      'callback' => [
        get_called_class(),
        'ajaxRefreshPreview',
      ],
      'progress' => [
        'type' => 'throbber',
      ],
      'wrapper' => $wrapper_id,
      'effect' => 'fade',
    ],
    '#operation' => 'preview',
  ];
  $preview_content = new TranslatableMarkup('<p>Please fill up the required value below then update the preview.</p>');
  if (!empty($element_state['library']) && !empty($element_state['series'])) {

    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');
    $chart_build = Chart::buildElement($options, $wrapper_id);

    // @todo check if this would work with various hooks.
    $chart_build['#id'] = $wrapper_id;
    $chart_build['#chart_id'] = $id_prefix;
    $preview_content = $renderer
      ->render($chart_build);
  }
  $element['preview']['content'] = [
    '#markup' => $preview_content,
  ];
  $element['series'] = [
    '#type' => 'chart_data_collector_table',
    '#initial_rows' => $element['#table_initial_rows'] ?? 5,
    '#initial_columns' => $element['#table_initial_columns'] ?? 2,
    '#table_drag' => FALSE,
    '#default_value' => $options['series'] ?? [],
  ];
  return $element;
}