You are here

public function ChartsBlock::blockForm in Charts 8.3

Same name and namespace in other branches
  1. 8.4 modules/charts_blocks/src/Plugin/Block/ChartsBlock.php \Drupal\charts_blocks\Plugin\Block\ChartsBlock::blockForm()
  2. 5.0.x modules/charts_blocks/src/Plugin/Block/ChartsBlock.php \Drupal\charts_blocks\Plugin\Block\ChartsBlock::blockForm()

Overrides BlockPluginTrait::blockForm

File

modules/charts_blocks/src/Plugin/Block/ChartsBlock.php, line 79

Class

ChartsBlock
Provides a 'ChartsBlock' block.

Namespace

Drupal\charts_blocks\Plugin\Block

Code

public function blockForm($form, FormStateInterface $form_state) {
  parent::blockForm($form, $form_state);
  if (!empty($this->chartsDefaultSettings)) {

    // Get the charts default settings.
    $default_options = $this->chartsDefaultSettings;

    // Merge the charts default settings with this block's configuration.
    $defaults = array_merge($default_options, $this->configuration);
  }
  else {
    $defaults = $this->configuration;
  }
  $form = $this->chartsBaseSettingsForm
    ->getChartsBaseSettingsForm($form, $defaults, [], [], 'block');

  /*
   * @todo figure out why the block label field does not respect weight.
   */

  // Reposition the block form fields to the top.
  $form['label']['#weight'] = '-26';
  $form['label_display']['#weight'] = '-25';

  // Check if chart will be a series.
  $form['series'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Series'),
    '#description' => $this
      ->t('Does this chart graph more than a single series?'),
    '#default_value' => !empty($this->configuration['series']) ? $this->configuration['series'] : 0,
    '#weight' => '-22',
  ];

  // If a single series.
  $form['data'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Data (single series)'),
    '#description' => $this
      ->t('Enter the data for your chart, separated by comma: 1,3,5,7'),
    '#default_value' => !empty($this->configuration['data']) ? $this->configuration['data'] : '',
    '#weight' => '-19',
    '#states' => [
      'invisible' => [
        ':input[name="settings[series]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['series_label'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Series label (single series)'),
    '#description' => $this
      ->t('Provide a label for your legend'),
    '#default_value' => !empty($this->configuration['series_label']) ? $this->configuration['series_label'] : '',
    '#weight' => '-18',
    '#states' => [
      'invisible' => [
        ':input[name="settings[series]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['color'] = [
    '#title' => $this
      ->t('Color (single series)'),
    '#type' => 'markup',
    '#theme_wrappers' => [
      'form_element',
    ],
    '#prefix' => '<div class="chart-colors">',
    '#suffix' => '</div>',
    '#weight' => '-17',
    '#states' => [
      'invisible' => [
        ':input[name="settings[series]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['color'][0] = [
    '#type' => 'textfield',
    '#attributes' => [
      'TYPE' => 'color',
    ],
    '#size' => 10,
    '#maxlength' => 7,
    '#theme_wrappers' => [],
    '#suffix' => ' ',
    '#default_value' => !empty($this->configuration['color']) ? $this->configuration['color'] : '#000000',
    '#states' => [
      'invisible' => [
        ':input[name="settings[series]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];

  // If making a series chart, the API requires this format.
  $form['data_series'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Data (multiple series)'),
    '#description' => $this
      ->t('Enter the data for your chart using this format (must be valid JSON): {"name":"Number of players","color":"#0d233a","data":[50,60,100,132,133,234]},{"name":"Number of coaches","color":"#ff0000","data":[50,80,100,32,133,234]}'),
    '#default_value' => !empty($this->configuration['data_series']) ? $this->configuration['data_series'] : '',
    '#weight' => '-17',
    '#states' => [
      'invisible' => [
        ':input[name="settings[series]"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
    '#placeholder' => 'Check the instructions below for formatting syntax.',
  ];
  $form['categories'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Categories'),
    '#description' => $this
      ->t('List categories. You should have as many as you have points of data in a series. They should be comma-separated: One,Two,Three,Four'),
    '#default_value' => !empty($this->configuration['categories']) ? $this->configuration['categories'] : '',
    '#weight' => '-16',
  ];

  // Enable stacking.
  unset($form['grouping']['#parents']);
  $form['grouping'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Stacking'),
    '#description' => $this
      ->t('Enable stacking'),
    '#default_value' => !empty($this->configuration['grouping']) ? $this->configuration['grouping'] : 0,
    '#weight' => '-15',
  ];

  /*
   * Unset the #parents element from default form, then set the
   * default value.
   */
  unset($form['library']['#parents']);
  $form['library']['#default_value'] = !empty($this->configuration['library']) ? $this->configuration['library'] : $defaults['library'];
  $form['library']['#weight'] = '-23';
  unset($form['type']['#parents']);
  $form['type']['#default_value'] = !empty($this->configuration['type']) ? $this->configuration['type'] : $defaults['type'];
  $form['type']['#weight'] = '-24';
  unset($form['display']['title']['#parents']);
  $form['display']['title']['#default_value'] = !empty($this->configuration['title']) ? $this->configuration['title'] : '';
  unset($form['display']['title_position']['#parents']);
  $form['display']['title_position']['#default_value'] = !empty($this->configuration['title_position']) ? $this->configuration['title_position'] : $defaults['title_position'];
  unset($form['display']['data_labels']['#parents']);
  $form['display']['data_labels']['#default_value'] = !empty($this->configuration['data_labels']) ? $this->configuration['data_labels'] : $defaults['data_labels'];
  unset($form['display']['data_markers']['#parents']);
  $form['display']['data_markers']['#default_value'] = !empty($this->configuration['data_markers']) ? $this->configuration['data_markers'] : $defaults['data_markers'];
  unset($form['display']['background']['#parents']);
  $form['display']['background']['#default_value'] = !empty($this->configuration['background']) ? $this->configuration['background'] : $defaults['background'];
  unset($form['display']['three_dimensional']['#parents']);
  $form['display']['three_dimensional']['#default_value'] = !empty($this->configuration['three_dimensional']) ? $this->configuration['three_dimensional'] : $defaults['three_dimensional'];
  unset($form['display']['polar']['#parents']);
  $form['display']['polar']['#default_value'] = !empty($this->configuration['polar']) ? $this->configuration['polar'] : $defaults['polar'];
  unset($form['display']['legend_position']['#parents']);
  $form['display']['legend_position']['#default_value'] = !empty($this->configuration['legend_position']) ? $this->configuration['legend_position'] : $defaults['legend_position'];
  unset($form['display']['tooltips']['#parents']);
  $form['display']['tooltips']['#default_value'] = !empty($this->configuration['tooltips']) ? $this->configuration['tooltips'] : $defaults['tooltips'];
  unset($form['display']['dimensions']['height']['#parents']);
  $form['display']['dimensions']['height']['#default_value'] = !empty($this->configuration['height']) ? $this->configuration['height'] : $defaults['height'];
  unset($form['display']['dimensions']['width']['#parents']);
  $form['display']['dimensions']['width']['#default_value'] = !empty($this->configuration['width']) ? $this->configuration['width'] : $defaults['width'];
  unset($form['display']['dimensions']['height_units']['#parents']);
  $form['display']['dimensions']['height_units']['#default_value'] = !empty($this->configuration['height_units']) ? $this->configuration['height_units'] : $defaults['height_units'];
  unset($form['display']['dimensions']['width_units']['#parents']);
  $form['display']['dimensions']['width_units']['#default_value'] = !empty($this->configuration['width_units']) ? $this->configuration['width_units'] : $defaults['width_units'];
  unset($form['display']['gauge']['green_to']['#parents']);
  $form['display']['gauge']['green_to']['#default_value'] = !empty($this->configuration['green_to']) ? $this->configuration['green_to'] : $defaults['green_to'];
  unset($form['display']['gauge']['green_from']['#parents']);
  $form['display']['gauge']['green_from']['#default_value'] = !empty($this->configuration['green_from']) ? $this->configuration['green_from'] : $defaults['green_from'];
  unset($form['display']['gauge']['yellow_to']['#parents']);
  $form['display']['gauge']['yellow_to']['#default_value'] = !empty($this->configuration['yellow_to']) ? $this->configuration['yellow_to'] : $defaults['yellow_to'];
  unset($form['display']['gauge']['yellow_from']['#parents']);
  $form['display']['gauge']['yellow_from']['#default_value'] = !empty($this->configuration['yellow_from']) ? $this->configuration['yellow_from'] : $defaults['yellow_from'];
  unset($form['display']['gauge']['red_to']['#parents']);
  $form['display']['gauge']['red_to']['#default_value'] = !empty($this->configuration['red_to']) ? $this->configuration['red_to'] : $defaults['red_to'];
  unset($form['display']['gauge']['red_from']['#parents']);
  $form['display']['gauge']['red_from']['#default_value'] = !empty($this->configuration['red_from']) ? $this->configuration['red_from'] : $defaults['red_from'];
  unset($form['display']['gauge']['max']['#parents']);
  $form['display']['gauge']['max']['#default_value'] = !empty($this->configuration['max']) ? $this->configuration['max'] : $defaults['max'];
  unset($form['display']['gauge']['min']['#parents']);
  $form['display']['gauge']['min']['#default_value'] = !empty($this->configuration['min']) ? $this->configuration['min'] : $defaults['min'];
  unset($form['xaxis']['xaxis_title']['#parents']);
  $form['xaxis']['xaxis_title']['#default_value'] = !empty($this->configuration['xaxis_title']) ? $this->configuration['xaxis_title'] : $defaults['xaxis_title'];
  unset($form['xaxis']['xaxis_labels_rotation']['#parents']);
  $form['xaxis']['xaxis_labels_rotation']['#default_value'] = !empty($this->configuration['xaxis_labels_rotation']) ? $this->configuration['xaxis_labels_rotation'] : $defaults['xaxis_labels_rotation'];
  unset($form['yaxis']['title']['#parents']);
  $form['yaxis']['title']['#default_value'] = !empty($this->configuration['yaxis_title']) ? $this->configuration['yaxis_title'] : $defaults['yaxis_title'];
  unset($form['yaxis']['yaxis_min']['#parents']);
  $form['yaxis']['yaxis_min']['#default_value'] = !empty($this->configuration['yaxis_min']) ? $this->configuration['yaxis_min'] : $defaults['yaxis_min'];
  unset($form['yaxis']['yaxis_max']['#parents']);
  $form['yaxis']['yaxis_max']['#default_value'] = !empty($this->configuration['yaxis_max']) ? $this->configuration['yaxis_max'] : $defaults['yaxis_max'];
  unset($form['yaxis']['yaxis_prefix']['#parents']);
  $form['yaxis']['yaxis_prefix']['#default_value'] = !empty($this->configuration['yaxis_prefix']) ? $this->configuration['yaxis_prefix'] : $defaults['yaxis_prefix'];
  unset($form['yaxis']['yaxis_suffix']['#parents']);
  $form['yaxis']['yaxis_suffix']['#default_value'] = !empty($this->configuration['yaxis_suffix']) ? $this->configuration['yaxis_suffix'] : $defaults['yaxis_suffix'];
  unset($form['yaxis']['yaxis_decimal_count']['#parents']);
  $form['yaxis']['yaxis_decimal_count']['#default_value'] = !empty($this->configuration['yaxis_decimal_count']) ? $this->configuration['yaxis_decimal_count'] : $defaults['yaxis_decimal_count'];
  unset($form['yaxis']['yaxis_labels_rotation']['#parents']);
  $form['yaxis']['yaxis_labels_rotation']['#default_value'] = !empty($this->configuration['yaxis_labels_rotation']) ? $this->configuration['yaxis_labels_rotation'] : $defaults['yaxis_labels_rotation'];
  $form['yaxis']['inherit_yaxis']['#default_value'] = !empty($this->configuration['inherit_yaxis']) ? $this->configuration['inherit_yaxis'] : '';

  // There are no parents for the secondary y axis.
  $form['yaxis']['secondary_yaxis']['title']['#default_value'] = !empty($this->configuration['secondary_yaxis_title']) ? $this->configuration['secondary_yaxis_title'] : $defaults['yaxis_title'];
  $form['yaxis']['secondary_yaxis']['minmax']['min']['#default_value'] = !empty($this->configuration['secondary_yaxis_min']) ? $this->configuration['secondary_yaxis_min'] : $defaults['yaxis_min'];
  $form['yaxis']['secondary_yaxis']['minmax']['max']['#default_value'] = !empty($this->configuration['secondary_yaxis_max']) ? $this->configuration['secondary_yaxis_max'] : $defaults['yaxis_max'];
  $form['yaxis']['secondary_yaxis']['prefix']['#default_value'] = !empty($this->configuration['secondary_yaxis_prefix']) ? $this->configuration['secondary_yaxis_prefix'] : $defaults['yaxis_prefix'];
  $form['yaxis']['secondary_yaxis']['suffix']['#default_value'] = !empty($this->configuration['secondary_yaxis_suffix']) ? $this->configuration['secondary_yaxis_suffix'] : $defaults['yaxis_suffix'];
  $form['yaxis']['secondary_yaxis']['decimal_count']['#default_value'] = !empty($this->configuration['secondary_yaxis_decimal_count']) ? $this->configuration['secondary_yaxis_decimal_count'] : $defaults['yaxis_decimal_count'];
  $form['yaxis']['secondary_yaxis']['labels_rotation']['#default_value'] = !empty($this->configuration['secondary_yaxis_labels_rotation']) ? $this->configuration['secondary_yaxis_labels_rotation'] : $defaults['yaxis_labels_rotation'];
  return $form;
}