You are here

public static function BaseSettings::processSettings in Charts 8.4

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

Processes the settings element.

Parameters

array $element: The form element.

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

array $complete_form: The complete form.

Return value

array The element.

Throws

\Drupal\Component\Plugin\Exception\PluginException

File

src/Element/BaseSettings.php, line 82

Class

BaseSettings
Provides a form element for setting a chart.

Namespace

Drupal\charts\Element

Code

public static function processSettings(array &$element, FormStateInterface $form_state, array &$complete_form) {
  $supported_usage = [
    'basic_form',
    'config_form',
    'view_form',
  ];
  if (empty($element['#used_in']) || !in_array($element['#used_in'], $supported_usage)) {
    throw new \InvalidArgumentException('The chart_base_settings element can only be used in basic, config and view forms.');
  }
  if (!is_array($element['#value'])) {
    throw new \InvalidArgumentException('The chart_base_settings #default_value must be an array.');
  }
  $parents = $element['#parents'];
  $id_prefix = implode('-', $parents);
  $wrapper_id = Html::getUniqueId($id_prefix . '-ajax-wrapper');
  $value = $element['#value'];

  // Enforce tree.
  $element = [
    '#tree' => TRUE,
    '#prefix' => '<div id="' . $wrapper_id . '">',
    '#suffix' => '</div>',
    // Pass the id along to other methods.
    '#wrapper_id' => $wrapper_id,
  ] + $element;
  $used_in = $element['#used_in'] ?: '';
  $required = !empty($element['#required']) ? $element['#required'] : FALSE;
  $options = $value ?? [];
  $library_options = [];
  if ($used_in !== 'config_form') {
    $library_options['site_default'] = new TranslatableMarkup('Site Default');
  }
  $library_options += self::getLibraries();
  if (!empty($element['#library']) && isset($library_options[$element['#library']])) {
    $element['library'] = [
      '#type' => 'value',
      '#value' => $element['#library'],
    ];
  }
  else {
    $element['library'] = [
      '#title' => new TranslatableMarkup('Charting library'),
      '#type' => 'select',
      '#options' => $library_options,
      '#default_value' => $options['library'],
      '#required' => $required,
      '#access' => count($library_options) > 0,
      '#attributes' => [
        'class' => [
          'chart-library-select',
        ],
      ],
      '#ajax' => [
        'callback' => [
          get_called_class(),
          'ajaxRefresh',
        ],
        'wrapper' => $wrapper_id,
      ],
    ];
  }
  $element['type'] = [
    '#title' => new TranslatableMarkup('Chart type'),
    '#type' => 'radios',
    '#default_value' => $options['type'],
    '#options' => self::getChartTypes(),
    '#required' => $required,
    '#attributes' => [
      'class' => [
        'chart-type-radios',
        'container-inline',
      ],
    ],
  ];
  if (!empty($element['#series'])) {
    $element = self::processSeriesForm($element, $options, $form_state);
  }
  $element['display'] = [
    '#title' => new TranslatableMarkup('Display'),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  ];
  $element['display']['title'] = [
    '#title' => new TranslatableMarkup('Chart title'),
    '#type' => 'textfield',
    '#default_value' => $options['display']['title'],
  ];
  $element['xaxis'] = [
    '#title' => new TranslatableMarkup('Horizontal axis'),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#attributes' => [
      'class' => [
        'chart-xaxis',
      ],
    ],
  ];
  $element['yaxis'] = [
    '#title' => new TranslatableMarkup('Vertical axis'),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#attributes' => [
      'class' => [
        'chart-yaxis',
      ],
    ],
  ];
  if ($used_in === 'view_form') {
    $element = self::processViewForm($element, $options, $form_state);
  }
  elseif ($used_in === 'config_form') {
    $element = self::processConfigForm($element, $options);
  }
  $element['display']['title_position'] = [
    '#title' => new TranslatableMarkup('Title position'),
    '#type' => 'select',
    '#options' => [
      '' => new TranslatableMarkup('None'),
      'out' => new TranslatableMarkup('Outside'),
      'in' => new TranslatableMarkup('Inside'),
      'top' => new TranslatableMarkup('Top'),
      'right' => new TranslatableMarkup('Right'),
      'bottom' => new TranslatableMarkup('Bottom'),
      'left' => new TranslatableMarkup('Left'),
    ],
    '#description' => new TranslatableMarkup('Not all of these will apply to your selected library.'),
    '#default_value' => $options['display']['title_position'] ?? '',
  ];
  $element['display']['tooltips'] = [
    '#title' => new TranslatableMarkup('Enable tooltips'),
    '#type' => 'checkbox',
    '#description' => new TranslatableMarkup('Show data details on mouse over? Note: unavailable for print or on mobile devices.'),
    '#default_value' => !empty($options['display']['tooltips']),
  ];
  $element['display']['data_labels'] = [
    '#title' => new TranslatableMarkup('Enable data labels'),
    '#type' => 'checkbox',
    '#default_value' => !empty($options['display']['data_labels']),
    '#description' => new TranslatableMarkup('Show data details as labels on chart? Note: recommended for print or on mobile devices.'),
  ];
  $element['display']['data_markers'] = [
    '#title' => new TranslatableMarkup('Enable data markers'),
    '#type' => 'checkbox',
    '#default_value' => !empty($options['display']['data_markers']),
    '#description' => new TranslatableMarkup('Show data markers (points) on line charts?'),
  ];
  $element['display']['legend_position'] = [
    '#title' => new TranslatableMarkup('Legend position'),
    '#type' => 'select',
    '#options' => [
      '' => new TranslatableMarkup('None'),
      'top' => new TranslatableMarkup('Top'),
      'right' => new TranslatableMarkup('Right'),
      'bottom' => new TranslatableMarkup('Bottom'),
      'left' => new TranslatableMarkup('Left'),
    ],
    '#default_value' => $options['display']['legend_position'] ?? '',
  ];
  $element['display']['background'] = [
    '#title' => new TranslatableMarkup('Background color'),
    '#type' => 'textfield',
    '#size' => 10,
    '#maxlength' => 7,
    '#attributes' => [
      'placeholder' => new TranslatableMarkup('transparent'),
    ],
    '#description' => new TranslatableMarkup('Leave blank for a transparent background.'),
    '#default_value' => $options['display']['background'] ?? '',
  ];
  $element['display']['three_dimensional'] = [
    '#title' => new TranslatableMarkup('Make chart three-dimensional (3D)'),
    '#type' => 'checkbox',
    '#default_value' => $options['display']['three_dimensional'] ?? FALSE,
    '#attributes' => [
      'class' => [
        'chart-type-checkbox',
        'container-inline',
      ],
    ],
  ];
  $element['display']['polar'] = [
    '#title' => new TranslatableMarkup('Transform cartesian charts into the polar coordinate system'),
    '#type' => 'checkbox',
    '#default_value' => $options['display']['polar'] ?? FALSE,
    '#attributes' => [
      'class' => [
        'chart-type-checkbox',
        'container-inline',
      ],
    ],
  ];
  $element['display']['dimensions'] = [
    '#title' => new TranslatableMarkup('Dimensions'),
    '#theme_wrappers' => [
      'form_element',
    ],
    '#description' => new TranslatableMarkup('If dimensions are left empty, the chart will fill its containing element.'),
  ];
  $element['display']['dimensions']['width'] = [
    '#type' => 'number',
    '#attributes' => [
      'placeholder' => new TranslatableMarkup('auto'),
    ],
    '#min' => 0,
    '#max' => 9999,
    '#default_value' => $options['display']['dimensions']['width'] ?? '',
    '#size' => 8,
    '#theme_wrappers' => [],
  ];
  $element['display']['dimensions']['width_units'] = [
    '#type' => 'textfield',
    '#attributes' => [
      'placeholder' => new TranslatableMarkup('%'),
    ],
    '#default_value' => $options['display']['dimensions']['width_units'] ?? '',
    '#suffix' => ' x ',
    '#size' => 2,
    '#theme_wrappers' => [],
  ];
  $element['display']['dimensions']['height'] = [
    '#type' => 'number',
    '#attributes' => [
      'placeholder' => new TranslatableMarkup('auto'),
    ],
    '#min' => 0,
    '#max' => 9999,
    '#default_value' => $options['display']['dimensions']['height'] ?? '',
    '#size' => 8,
    '#theme_wrappers' => [],
  ];
  $element['display']['dimensions']['height_units'] = [
    '#type' => 'textfield',
    '#attributes' => [
      'placeholder' => new TranslatableMarkup('px'),
    ],
    '#default_value' => $options['display']['dimensions']['height_units'] ?? '',
    '#size' => 2,
    '#theme_wrappers' => [],
  ];
  $element['xaxis']['title'] = [
    '#title' => new TranslatableMarkup('Custom title'),
    '#type' => 'textfield',
    '#default_value' => $options['xaxis']['title'] ?? '',
  ];
  $element['xaxis']['labels_rotation'] = [
    '#title' => new TranslatableMarkup('Labels rotation'),
    '#type' => 'select',
    '#options' => [
      0 => new TranslatableMarkup('0°'),
      30 => new TranslatableMarkup('30°'),
      45 => new TranslatableMarkup('45°'),
      60 => new TranslatableMarkup('60°'),
      90 => new TranslatableMarkup('90°'),
    ],
    // This is only shown on non-inverted charts.
    '#attributes' => [
      'class' => [
        'axis-inverted-hide',
      ],
    ],
    '#default_value' => $options['xaxis']['labels_rotation'] ?? '',
  ];
  $element['yaxis']['title'] = [
    '#title' => new TranslatableMarkup('Custom title'),
    '#type' => 'textfield',
    '#default_value' => $options['yaxis']['title'] ?? '',
  ];
  $element['yaxis']['min_max_label'] = [
    '#type' => 'html_tag',
    '#tag' => 'label',
    '#value' => new TranslatableMarkup('Value range'),
  ];
  $element['yaxis']['min'] = [
    '#type' => 'number',
    '#title' => new TranslatableMarkup('Value range minimum'),
    '#title_display' => 'invisible',
    '#attributes' => [
      'placeholder' => new TranslatableMarkup('Minimum'),
    ],
    '#max' => 999999999,
    '#default_value' => $options['yaxis']['min'] ?? '',
    '#size' => 12,
    '#suffix' => ' ',
  ];
  $element['yaxis']['max'] = [
    '#type' => 'number',
    '#attributes' => [
      'placeholder' => new TranslatableMarkup('Maximum'),
    ],
    '#max' => 999999999,
    '#default_value' => $options['yaxis']['max'] ?? '',
    '#size' => 12,
  ];
  $element['yaxis']['prefix'] = [
    '#title' => new TranslatableMarkup('Value prefix'),
    '#type' => 'textfield',
    '#default_value' => $options['yaxis']['prefix'] ?? '',
    '#size' => 12,
  ];
  $element['yaxis']['suffix'] = [
    '#title' => new TranslatableMarkup('Value suffix'),
    '#type' => 'textfield',
    '#default_value' => $options['yaxis']['suffix'] ?? '',
    '#size' => 12,
  ];
  $element['yaxis']['decimal_count'] = [
    '#title' => new TranslatableMarkup('Decimal count'),
    '#type' => 'number',
    '#attributes' => [
      'placeholder' => new TranslatableMarkup('auto'),
    ],
    '#min' => 0,
    '#max' => 20,
    '#default_value' => $options['yaxis']['decimal_count'] ?? '',
    '#size' => 5,
    '#description' => new TranslatableMarkup('Enforce a certain number of decimal-place digits in displayed values.'),
  ];
  $element['yaxis']['labels_rotation'] = [
    '#title' => new TranslatableMarkup('Labels rotation'),
    '#type' => 'select',
    '#options' => [
      0 => new TranslatableMarkup('0°'),
      30 => new TranslatableMarkup('30°'),
      45 => new TranslatableMarkup('45°'),
      60 => new TranslatableMarkup('60°'),
      90 => new TranslatableMarkup('90°'),
    ],
    // This is only shown on inverted charts.
    '#attributes' => [
      'class' => [
        'axis-inverted-show',
      ],
    ],
    '#default_value' => $options['yaxis']['labels_rotation'] ?? '',
  ];

  // Adding basic form yaxis other fields.
  if ($used_in === 'basic_form') {
    $element = self::processBasicForm($element, $options);
  }

  // Settings for gauges.
  $element['display']['gauge'] = [
    '#title' => new TranslatableMarkup('Gauge settings'),
    '#type' => 'fieldset',
    '#collapsible' => FALSE,
    '#states' => [
      'visible' => [
        ':input[class*=chart-type-radios]' => [
          'value' => 'gauge',
        ],
      ],
    ],
    'max' => [
      '#title' => new TranslatableMarkup('Gauge maximum value'),
      '#type' => 'number',
      '#default_value' => $options['display']['gauge']['max'] ?? '',
    ],
    'min' => [
      '#title' => new TranslatableMarkup('Gauge minimum value'),
      '#type' => 'number',
      '#default_value' => $options['display']['gauge']['min'] ?? '',
    ],
    'green_from' => [
      '#title' => new TranslatableMarkup('Green minimum value'),
      '#type' => 'number',
      '#default_value' => $options['display']['gauge']['green_from'] ?? '',
    ],
    'green_to' => [
      '#title' => new TranslatableMarkup('Green maximum value'),
      '#type' => 'number',
      '#default_value' => $options['display']['gauge']['green_to'] ?? '',
    ],
    'yellow_from' => [
      '#title' => new TranslatableMarkup('Yellow minimum value'),
      '#type' => 'number',
      '#default_value' => $options['display']['gauge']['yellow_from'] ?? '',
    ],
    'yellow_to' => [
      '#title' => new TranslatableMarkup('Yellow maximum value'),
      '#type' => 'number',
      '#default_value' => $options['display']['gauge']['yellow_to'] ?? '',
    ],
    'red_from' => [
      '#title' => new TranslatableMarkup('Red minimum value'),
      '#type' => 'number',
      '#default_value' => $options['display']['gauge']['red_from'] ?? '',
    ],
    'red_to' => [
      '#title' => new TranslatableMarkup('Red maximum value'),
      '#type' => 'number',
      '#default_value' => $options['display']['gauge']['red_to'] ?? '',
    ],
  ];
  if ($used_in === 'config_form' && !empty($options['library'])) {
    $element = self::buildLibraryConfigurationForm($element, $form_state, $options['library']);
  }
  return $element;
}