You are here

private static function BaseSettings::processViewForm in Charts 8.4

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

Process view 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.

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

File

src/Element/BaseSettings.php, line 804

Class

BaseSettings
Provides a form element for setting a chart.

Namespace

Drupal\charts\Element

Code

private static function processViewForm(array $element, array $options, FormStateInterface $form_state) {
  if (!is_array($element['#field_options'])) {
    throw new \InvalidArgumentException('The chart_base_settings element need valid field options when used as view form.');
  }
  $element['display']['#weight'] = 2;
  $element['xaxis']['#weight'] = 2;
  $element['yaxis']['#weight'] = 2;
  $element_name = $element['#name'];
  $field_options = $element['#field_options'];
  $first_field = $field_options ? key($field_options) : '';
  $element['fields'] = [
    '#title' => new TranslatableMarkup('Charts fields'),
    '#type' => 'fieldset',
    '#weight' => 1,
  ];

  // Add a views-specific chart option to allow advanced rendering.
  // $element['fields']['allow_advanced_rendering'] = [
  // '#type' => 'checkbox',
  // '#title' => new TranslatableMarkup('Allow advanced rendering'),
  // '#description' => new TranslatableMarkup('Allow views field rewriting.
  // etc. for label and data fields. This can break charts if you rewrite
  // the field to a value the charting library cannot handle
  // - e.g. passing a string value into a numeric data column.'),
  // '#default_value' => isset($options['fields'].
  // ['allow_advanced_rendering']) ? $options['fields']
  // ['allow_advanced_rendering'] : NULL,].
  $element['fields']['label'] = [
    '#type' => 'radios',
    '#title' => new TranslatableMarkup('Label field'),
    '#options' => $field_options + [
      '' => new TranslatableMarkup('No label field'),
    ],
    '#default_value' => isset($options['fields']['label']) ? $options['fields']['label'] : $first_field,
  ];

  // Enable stacking.
  $element['fields']['stacking'] = [
    '#type' => 'checkbox',
    '#title' => new TranslatableMarkup('Stacking'),
    '#description' => new TranslatableMarkup('Enable stacking for this chart. Will stack based on the selected label field.'),
    '#default_value' => !empty($options['fields']['stacking']) ? $options['fields']['stacking'] : FALSE,
  ];
  $element['fields']['data_providers'] = [
    '#type' => 'table',
    '#header' => [
      new TranslatableMarkup('Field Name'),
      new TranslatableMarkup('Provides Data'),
      new TranslatableMarkup('Color'),
      new TranslatableMarkup('Weight'),
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'view-chart-fields-data-providers-order-weight',
      ],
    ],
  ];

  // Make the weight list always reflect the current number of values.
  // Taken from WidgetBase::formMultipleElements().
  $max_weight = count($field_options);
  foreach ($field_options as $field_name => $field_label) {
    $field_option_element =& $element['fields']['data_providers'][$field_name];
    $default_value = $options['fields']['data_providers'][$field_name] ?? [];
    $default_weight = $default_value['weight'] ?? $max_weight;
    $field_option_element['#attributes']['class'][] = 'draggable';

    // Field option label.
    $field_option_element['label'] = [
      '#markup' => new TranslatableMarkup('@label', [
        '@label' => $field_label,
      ]),
    ];
    $field_option_element['enabled'] = [
      '#type' => 'checkbox',
      '#title' => new TranslatableMarkup('Provides data'),
      '#title_display' => 'invisible',
      '#default_value' => !empty($default_value['enabled']),
      '#states' => [
        'disabled' => [
          ':input[name="' . $element_name . '[fields][label]"]' => [
            'value' => $field_name,
          ],
        ],
      ],
    ];
    $field_option_element['color'] = [
      '#type' => 'textfield',
      '#title' => new TranslatableMarkup('Color'),
      '#attributes' => [
        'TYPE' => 'color',
      ],
      '#title_display' => 'invisible',
      '#size' => 10,
      '#maxlength' => 7,
      '#default_value' => $default_value['color'] ?? '#000000',
    ];
    $field_option_element['weight'] = [
      '#type' => 'weight',
      '#title' => new TranslatableMarkup('Weight'),
      '#title_display' => 'invisible',
      '#delta' => $max_weight,
      '#default_value' => $default_weight,
      '#attributes' => [
        'class' => [
          'view-chart-fields-data-providers-order-weight',
        ],
      ],
    ];
    $field_option_element['#weight'] = $default_weight;
  }
  return $element;
}