ColorapiTextDisplayFormatter.php in Color API 8
File
src/Plugin/Field/FieldFormatter/ColorapiTextDisplayFormatter.php
View source
<?php
namespace Drupal\colorapi\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
class ColorapiTextDisplayFormatter extends ColorapiDisplayFormatterBase {
public static function defaultSettings() {
return [
'show_hash' => TRUE,
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$element['show_hash'] = [
'#type' => 'checkbox',
'#title' => t('Prefix color with hash (#) symbol'),
'#default_value' => $this
->getSetting('show_hash'),
];
return $element;
}
public function settingsSummary() {
$summary = [];
$summary['overview'] = $this
->t('Displays the color as a hexadecimal string');
if ($this
->getSetting('show_hash')) {
$show_hash = $this
->t('Yes');
}
else {
$show_hash = $this
->t('No');
}
$summary['show_hash'] = $this
->t('Prefix with hash (#) symbol: @value', [
'@value' => $show_hash,
]);
return $summary + parent::settingsSummary();
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
foreach ($items as $delta => $item) {
$this
->addHumanReadableNameToElement($element, $delta, $item);
$color = $item
->getHexadecimal();
$element[$delta]['color'] = [
'#theme' => 'colorapi_text_display',
'#entity_delta' => $delta,
'#item' => $item,
'#hexadecimal_color' => $this
->getSetting('show_hash') ? $color : substr($color, 1),
];
}
return $element;
}
}