Commit ed698f70 authored by justcaldwell's avatar justcaldwell Committed by rikki_iki
Browse files

Issue #3113746 by justcaldwell: Support List (text) fields

parent 21b56fb9
<?php
namespace Drupal\element_class_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Template\Attribute;
/**
* Formatter for displaying list (text) in an HTML list.
*
* @FieldFormatter(
* id="list_string_list_class",
* label="List (with class)",
* field_types={
* "list_string",
* }
* )
*/
class ListStringListClassFormatter extends FormatterBase {
use ElementListClassTrait;
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$attributes = new Attribute();
$class = $this->getSetting('class');
if (!empty($class)) {
$attributes->addClass($class);
}
$field_storage = $items->getFieldDefinition()->getFieldStorageDefinition();
$options = options_allowed_values($field_storage);
foreach ($items as $delta => $item) {
$elements[$delta] = $options[$item->getValue()['value']];
}
return [
[
'#theme' => 'item_list',
'#items' => $elements,
'#list_type' => $this->getSetting('list_type'),
'#attributes' => $attributes->toArray(),
],
];
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment