PlainStringFormatter.php in String field formatter 8.x
File
src/Plugin/Field/FieldFormatter/PlainStringFormatter.php
View source
<?php
namespace Drupal\string_field_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
class PlainStringFormatter extends FormatterBase {
public static function defaultSettings() {
return array(
'wrap_tag' => '_none',
) + parent::defaultSettings();
}
public static function defaultWrapTagOptions() {
return array(
'_none' => t('- None -'),
'div' => t('DIV'),
'h1' => t('H1'),
'h2' => t('H2'),
'h3' => t('H3'),
'h4' => t('H4'),
'h5' => t('H5'),
'h6' => t('H6'),
'span' => t('SPAN'),
);
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = array();
$element['wrap_tag'] = array(
'#title' => t('Wrap text in tag'),
'#type' => 'select',
'#default_value' => $this
->getSetting('wrap_tag'),
'#options' => $this
->defaultWrapTagOptions(),
);
return $element;
}
public function settingsSummary() {
$summary = array();
$wrap_tag = $this
->getSetting('wrap_tag');
if ('_none' == $wrap_tag) {
$summary[] = t('No wrap tag defined.');
}
else {
$summary[] = t('Wrap text with tag: @tag', array(
'@tag' => $wrap_tag,
));
}
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
$wrap_tag = $this
->getSetting('wrap_tag');
if ('_none' == $wrap_tag) {
$wrap_tag = '';
}
foreach ($items as $delta => $item) {
$elements[$delta] = array(
'#type' => 'html_tag',
'#tag' => $wrap_tag,
'#value' => $item->value,
);
}
return $elements;
}
}