ElementListClassTrait.php in Element Class Formatter 8
File
src/Plugin/Field/FieldFormatter/ElementListClassTrait.php
View source
<?php
namespace Drupal\element_class_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Template\Attribute;
trait ElementListClassTrait {
use ElementClassTrait;
public static function defaultSettings() {
$default_settings = parent::defaultSettings() + [
'list_type' => 'ul',
];
return ElementClassTrait::elementClassDefaultSettings($default_settings);
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$class = $this
->getSetting('class');
$elements['list_type'] = [
'#title' => $this
->t('List type'),
'#type' => 'select',
'#options' => [
'ul' => 'Un-ordered list',
'ol' => 'Ordered list',
],
'#default_value' => $this
->getSetting('list_type'),
];
return $this
->elementClassSettingsForm($elements, $class);
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$class = $this
->getSetting('class');
if ($tag = $this
->getSetting('list_type')) {
$summary[] = $this
->t('List type: @tag', [
'@tag' => $tag,
]);
}
return $this
->elementClassSettingsSummary($summary, $class);
}
public function viewElements(FieldItemListInterface $items, $langcode) {
if ($items
->isEmpty()) {
return [];
}
$elements = parent::viewElements($items, $langcode);
$class = $this
->getSetting('class');
$attributes = new Attribute();
if (!empty($class)) {
$attributes
->addClass($class);
}
return [
[
'#theme' => 'item_list',
'#items' => $elements,
'#list_type' => $this
->getSetting('list_type'),
'#attributes' => $attributes
->toArray(),
],
];
}
}
Traits
Name |
Description |
ElementListClassTrait |
The ElementListClassTrait is used to turn multi-cardinality fields
into lists with a class. |