FlotBlock.php in Flot 8
File
src/Plugin/Block/FlotBlock.php
View source
<?php
namespace Drupal\flot\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
class FlotBlock extends BlockBase {
public function build() {
$settings = $this
->getConfiguration();
$data = $settings['flot_block_settings']['data'];
$series = [];
foreach ($data as $series_data) {
$series[] = [
'data' => json_decode($series_data),
];
}
$options = [];
if ($settings['flot_block_type'] == 1) {
$options = [
'bars' => [
'show' => TRUE,
],
];
}
$output['flot'] = [
'#type' => 'flot',
'#data' => $series,
'#options' => $options,
];
return $output;
}
public function blockForm($form, FormStateInterface $form_state) {
$config = $this
->getConfiguration();
$data = isset($config['flot_block_settings']['data']) ? $config['flot_block_settings']['data'] : [];
$text = "";
$first = true;
foreach ($data as $line) {
if (!$first) {
$text .= "\n";
}
else {
$first = false;
}
$text .= $line;
}
$form['flot_block_type'] = [
'#type' => 'select',
'#title' => 'Chart Type',
'#options' => [
'Lines',
'Bars',
'Pie',
],
'#default_value' => isset($config['flot_block_type']) ? $config['flot_block_type'] : 0,
];
$form['flot_block_settings'] = [
'#type' => 'textarea',
'#title' => $this
->t('Flot Data'),
'#default_value' => $text,
];
return $form;
}
public function blockValidate($form, FormStateInterface $form_state) {
}
public function blockSubmit($form, FormStateInterface $form_state) {
$textarea_value = $form_state
->getValue('flot_block_settings');
$settings = [];
$settings['label'] = $form_state
->getValue('label');
$settings['flot_block_type'] = $form_state
->getValue('flot_block_type');
$line_array = explode("\n", $textarea_value);
foreach ($line_array as $line) {
$settings['flot_block_settings']['data'][] = $line;
}
$this->configuration = $settings;
}
}