View source
<?php
namespace Drupal\string_field_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\StringFormatter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Component\Utility\Html;
class PlainStringFormatter extends StringFormatter {
protected static $wrapTagEmptyValue = '_none';
public static function defaultSettings() {
return parent::defaultSettings() + [
'wrap_tag' => static::$wrapTagEmptyValue,
'wrap_class' => '',
];
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$element['wrap_tag'] = [
'#title' => $this
->t('Wrapper tag'),
'#type' => 'select',
'#default_value' => $this
->getSetting('wrap_tag'),
'#empty_option' => $this
->t('- None -'),
'#empty_value' => static::$wrapTagEmptyValue,
'#options' => $this
->wrapTagOptions(),
];
$element['wrap_class'] = [
'#title' => $this
->t('Classes for wrapper tag'),
'#type' => 'textfield',
'#maxlength' => 128,
'#default_value' => $this
->getSetting('wrap_class'),
'#description' => $this
->t('Space separated list of HTML classes.'),
];
return $element;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$wrapTags = $this
->wrapTagOptions();
$wrapTag = $this
->getSetting('wrap_tag');
$hasWrapTag = $wrapTag !== static::$wrapTagEmptyValue;
$wrapTagLabel = isset($wrapTags[$wrapTag]) ? $wrapTags[$wrapTag] : $wrapTag;
$summary[] = $hasWrapTag ? $this
->t('Wrapper tag: @tag', [
'@tag' => $wrapTagLabel,
]) : $this
->t('No wrapper tag defined.');
if ($hasWrapTag) {
$class = $this
->prepareClasses($this
->getSetting('wrap_class'));
$args = [
'@class' => implode(' ', $class),
];
$summary[] = $class ? $this
->formatPlural(count($class), 'Class: @class', 'Classes: @class', $args) : $this
->t('Without classes.');
}
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
$wrapTag = $this
->getSetting('wrap_tag');
if ($wrapTag === static::$wrapTagEmptyValue) {
return $elements;
}
$attributes = [
'class' => $this
->prepareClasses($this
->getSetting('wrap_class')),
];
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#type' => 'html_tag',
'#tag' => $wrapTag,
'#attributes' => $attributes,
'content' => $elements[$delta],
];
}
return $elements;
}
protected function prepareClasses($classes) {
$prepared = [];
foreach ($this
->explode($classes) as $class) {
$prepared[] = Html::getClass($class);
}
return $prepared;
}
protected function explode($text) {
return preg_split('/[,\\s]+/', $text, -1, PREG_SPLIT_NO_EMPTY);
}
protected function wrapTagOptions() {
$options = [
'context' => 'HTML tag',
];
return [
'h1' => $this
->t('H1', [], $options),
'h2' => $this
->t('H2', [], $options),
'h3' => $this
->t('H3', [], $options),
'h4' => $this
->t('H4', [], $options),
'h5' => $this
->t('H5', [], $options),
'h6' => $this
->t('H6', [], $options),
'blockquote' => $this
->t('BLOCKQUOTE', [], $options),
'pre' => $this
->t('PRE', [], $options),
'template' => $this
->t('TEMPLATE', [], $options),
'abbr' => $this
->t('ABBR', [], $options),
'address' => $this
->t('ADDRESS', [], $options),
'cite' => $this
->t('CITE', [], $options),
'code' => $this
->t('CODE', [], $options),
'del' => $this
->t('DEL', [], $options),
'em' => $this
->t('EM', [], $options),
'ins' => $this
->t('INS', [], $options),
'kbd' => $this
->t('KBD', [], $options),
'mark' => $this
->t('MARK', [], $options),
'meter' => $this
->t('METER', [], $options),
'progress' => $this
->t('PROGRESS', [], $options),
'q' => $this
->t('Q', [], $options),
's' => $this
->t('S', [], $options),
'samp' => $this
->t('SAMP', [], $options),
'small' => $this
->t('SMALL', [], $options),
'strong' => $this
->t('STRONG', [], $options),
'sub' => $this
->t('SUB', [], $options),
'sup' => $this
->t('SUP', [], $options),
'time' => $this
->t('TIME', [], $options),
'u' => $this
->t('U', [], $options),
'var' => $this
->t('VAR', [], $options),
'div' => $this
->t('DIV', [], $options),
'span' => $this
->t('SPAN', [], $options),
];
}
}