trait ElementListClassTrait in Element Class Formatter 8
The ElementListClassTrait is used to turn multi-cardinality fields into lists with a class.
@package Drupal\element_class_formatter\Plugin\Field\FieldFormatter
Hierarchy
- trait \Drupal\element_class_formatter\Plugin\Field\FieldFormatter\ElementListClassTrait uses ElementClassTrait
File
- src/
Plugin/ Field/ FieldFormatter/ ElementListClassTrait.php, line 15
Namespace
Drupal\element_class_formatter\Plugin\Field\FieldFormatterView source
trait ElementListClassTrait {
use ElementClassTrait;
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
$default_settings = parent::defaultSettings() + [
'list_type' => 'ul',
];
return ElementClassTrait::elementClassDefaultSettings($default_settings);
}
/**
* {@inheritdoc}
*/
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);
}
/**
* {@inheritdoc}
*/
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);
}
/**
* {@inheritdoc}
*/
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(),
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ElementClassTrait:: |
public static | function | Default class value. | |
ElementClassTrait:: |
public | function | Setting form to collect class value. | |
ElementClassTrait:: |
public | function | Text for settings summary. | |
ElementClassTrait:: |
public | function | Set the class on the element. | |
ElementClassTrait:: |
public | function | Set the class on the entity. | |
ElementListClassTrait:: |
public static | function | ||
ElementListClassTrait:: |
public | function | ||
ElementListClassTrait:: |
public | function | ||
ElementListClassTrait:: |
public | function | 2 | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |