View source
<?php
namespace Drupal\formatter_suite\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Html;
use Drupal\formatter_suite\Utilities;
class GeneralNumberWithBarIndicatorFormatter extends GeneralNumberFormatter {
protected static function getValueLocations() {
return [
'none' => t('No value'),
'left' => t('Before bar'),
'right' => t('After bar'),
];
}
public static function defaultSettings() {
return array_merge([
'barLength' => '200',
'barWidth' => '15',
'barColor' => '#000000',
'backgroundColor' => '#ffffff',
'valueLocation' => 'right',
], parent::defaultSettings());
}
public function settingsSummary() {
$this
->sanitizeSettings();
$barLength = $this
->getSetting('barLength');
$barWidth = $this
->getSetting('barWidth');
$valueLocation = $this
->getSetting('valueLocation');
$fieldSettings = $this
->getFieldSettings();
$min = $fieldSettings['min'];
$max = $fieldSettings['max'];
$valueLocations = $this
->getValueLocations();
if (empty($valueLocation) === TRUE || isset($valueLocations[$valueLocation]) === FALSE) {
$valueLocation = 'none';
}
$disabledByMinMax = FALSE;
$disabledByLength = FALSE;
if (isset($min) === FALSE || isset($max) === FALSE) {
$disabledByMinMax = TRUE;
}
elseif ($barLength <= 0 || $barWidth <= 0) {
$disabledByLength = TRUE;
}
else {
$barLength .= 'px';
$barWidth .= 'px';
}
$summary = [];
if ($disabledByMinMax === TRUE) {
$summary[] = $this
->t('Disabled color bar, field min/max need to be set.');
$summary = array_merge($summary, parent::settingsSummary());
}
elseif ($disabledByLength === TRUE) {
$summary[] = $this
->t('Disabled color bar, bar size needs to be set.');
$summary = array_merge($summary, parent::settingsSummary());
}
else {
$summary[] = $this
->t('Colored bar @barLength long, @barWidth wide.', [
'@barLength' => $barLength,
'@barWidth' => $barWidth,
]);
if ($valueLocation === 'none') {
$summary[] = $this
->t('No value shown.');
}
else {
$summary[] = $this
->t('Value @location.', [
'@location' => $valueLocations[$valueLocation],
]);
$summary = array_merge($summary, parent::settingsSummary());
}
}
return $summary;
}
protected function getDescription() {
return $this
->t('Draw a horizontal bar with a length based on the field value.');
}
public function settingsForm(array $form, FormStateInterface $formState) {
$elements = parent::settingsForm($form, $formState);
$fieldSettings = $this
->getFieldSettings();
$min = $fieldSettings['min'];
$max = $fieldSettings['max'];
$disabled = FALSE;
if (isset($min) === FALSE || isset($max) === FALSE) {
$disabled = TRUE;
$elements['warning'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $this
->t("To enable horizontal bar display, first set the minimum and maximum in the field's definition."),
'#weight' => -999,
'#attributes' => [
'class' => [
'formatter_suite-settings-warning',
],
],
];
}
$weight = 100;
$elements['sectionBreak'] = [
'#markup' => '<div class="formatter_suite-section-break"></div>',
'#weight' => $weight++,
];
$elements['barLength'] = [
'#title' => $this
->t('Max bar length'),
'#type' => 'number',
'#min' => 1,
'#max' => 5000,
'#size' => 5,
'#default_value' => $this
->getSetting('barLength'),
'#disabled' => $disabled,
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-number-with-bar-indicator-bar-length',
],
],
];
$elements['barWidth'] = [
'#title' => $this
->t('Bar width'),
'#type' => 'number',
'#min' => 1,
'#max' => 5000,
'#size' => 5,
'#default_value' => $this
->getSetting('barWidth'),
'#description' => $this
->t('Bar length and width in pixels.'),
'#disabled' => $disabled,
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-number-with-bar-indicator-bar-width',
],
],
];
$elements['barColor'] = [
'#title' => $this
->t('Bar color'),
'#type' => 'textfield',
'#size' => 7,
'#default_value' => $this
->getSetting('barColor'),
'#disabled' => $disabled,
'#weight' => $weight++,
'#attributes' => [
'autocomplete' => 'off',
'autocapitalize' => 'none',
'spellcheck' => 'false',
'autocorrect' => 'off',
],
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-number-with-bar-indicator-bar-color',
],
],
];
$elements['backgroundColor'] = [
'#title' => $this
->t('Background color'),
'#type' => 'textfield',
'#size' => 7,
'#default_value' => $this
->getSetting('backgroundColor'),
'#description' => $this
->t("Colors use CSS syntax (e.g. '#ff0000'). Empty background uses page's background."),
'#disabled' => $disabled,
'#weight' => $weight++,
'#attributes' => [
'autocomplete' => 'off',
'autocapitalize' => 'none',
'spellcheck' => 'false',
'autocorrect' => 'off',
],
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-number-with-bar-indicator-background-color',
],
],
];
$elements['valueLocation'] = [
'#title' => $this
->t('Value location'),
'#type' => 'select',
'#options' => $this
->getValueLocations(),
'#default_value' => $this
->getSetting('valueLocation'),
'#disabled' => $disabled,
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-number-with-bar-indicator-value-location',
],
],
];
return $elements;
}
protected function sanitizeSettings() {
$barLength = $this
->getSetting('barLength');
$barWidth = $this
->getSetting('barWidth');
$barColor = $this
->getSetting('barColor');
$backgroundColor = $this
->getSetting('backgroundColor');
$valueLocation = $this
->getSetting('valueLocation');
$defaults = $this
->defaultSettings();
parent::sanitizeSettings();
$valueLocations = $this
->getValueLocations();
if (empty($valueLocation) === TRUE || isset($valueLocations[$valueLocation]) === FALSE) {
$valueLocation = $defaults['valueLocation'];
}
if (empty($barLength) === TRUE) {
$barLength = intval($defaults['barLength']);
}
else {
$barLength = intval($barLength);
if ($barLength < 0) {
$barLength = intval($defaults['barLength']);
}
}
if (empty($barWidth) === TRUE) {
$barWidth = intval($defaults['barWidth']);
}
else {
$barWidth = intval($barWidth);
if ($barWidth < 0) {
$barWidth = intval($defaults['barWidth']);
}
}
if (empty($barColor) === TRUE || $barColor[0] !== '#') {
$barColor = $defaults['barColor'];
}
else {
$barColor = Html::escape($barColor);
}
if (empty($backgroundColor) === TRUE || $backgroundColor[0] !== '#') {
$backgroundColor = $defaults['backgroundColor'];
}
else {
$backgroundColor = Html::escape($backgroundColor);
}
$this
->setSetting('barLength', $barLength);
$this
->setSetting('barWidth', $barWidth);
$this
->setSetting('barColor', $barColor);
$this
->setSetting('backgroundColor', $backgroundColor);
$this
->setSetting('valueLocation', $valueLocation);
}
public function viewElements(FieldItemListInterface $items, $langCode) {
if ($items
->isEmpty() === TRUE) {
return [];
}
$build = parent::viewElements($items, $langCode);
if (empty($build) === TRUE) {
return [];
}
$barColor = $this
->getSetting('barColor');
$barLength = $this
->getSetting('barLength');
$barWidth = $this
->getSetting('barWidth');
$backgroundColor = $this
->getSetting('backgroundColor');
$valueLocation = $this
->getSetting('valueLocation');
$fieldSettings = $this
->getFieldSettings();
$min = $fieldSettings['min'];
$max = $fieldSettings['max'];
if (empty($barLength) === TRUE || empty($barWidth) === TRUE || isset($min) === FALSE || isset($max) === FALSE) {
return $build;
}
$valueRange = (double) $max - (double) $min;
foreach ($items as $delta => $item) {
$percent = ((double) $item->value - $min) / $valueRange;
if ($percent < 0.0) {
$percent = 0.0;
}
elseif ($percent > 1.0) {
$percent = 1.0;
}
$valueLabel = '';
if ($valueLocation !== 'none') {
$valueLabel = $build[$delta]['#markup'];
}
unset($build[$delta]['#markup']);
$build[$delta] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'formatter_suite-general-number-with-bar-indicator-wrapper',
],
],
'#attached' => [
'library' => [
'formatter_suite/formatter_suite.usage',
],
],
];
if ($valueLocation === 'left') {
$build[$delta]['value'] = [
'#markup' => $valueLabel,
];
}
$backgroundStyle = "width: {$barLength}px; height: {$barWidth}px;";
if (empty($backgroundColor) === FALSE) {
$backgroundStyle .= "background-color: {$backgroundColor};";
}
$build[$delta]['barouter'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'formatter_suite-general-number-with-bar-indicator-outer',
'formatter_suite-general-number-with-bar-indicator-' . $valueLocation,
],
'style' => $backgroundStyle,
],
'bar' => [
'#type' => 'html_tag',
'#tag' => 'img',
'#attributes' => [
'class' => [
'formatter_suite-general-number-with-bar-indicator',
],
'src' => Utilities::createImage($barColor, $percent * $barLength, $barWidth),
],
],
];
if ($valueLocation === 'right') {
$build[$delta]['value'] = [
'#markup' => $valueLabel,
];
}
}
return $build;
}
}