trait ElementClassTrait in Element Class Formatter 8
Trait ElementClassTrait.
@package Drupal\element_class_formatter\Plugin\Field\FieldFormatter
Hierarchy
- trait \Drupal\element_class_formatter\Plugin\Field\FieldFormatter\ElementClassTrait uses StringTranslationTrait
File
- src/
Plugin/ Field/ FieldFormatter/ ElementClassTrait.php, line 13
Namespace
Drupal\element_class_formatter\Plugin\Field\FieldFormatterView source
trait ElementClassTrait {
use StringTranslationTrait;
/**
* Default class value.
*
* @param array $settings
* The original default settings array.
*
* @return array
* The new default settings array.
*/
public static function elementClassDefaultSettings(array $settings) {
return [
'class' => '',
] + $settings;
}
/**
* Setting form to collect class value.
*
* @param array $elements
* The original elements render array.
* @param string $class
* The class string.
*
* @return array
* The updated elements render array.
*/
public function elementClassSettingsForm(array $elements, $class) {
$elements['class'] = [
'#type' => 'textfield',
'#default_value' => $class,
'#title' => $this
->t('Element class'),
'#description' => 'A space separated set of classes.',
'#maxlength' => 200,
];
return $elements;
}
/**
* Text for settings summary.
*
* @param array $summary
* The original summary array.
* @param string $class
* The class string.
*
* @return array
* The updated summary array.
*/
public function elementClassSettingsSummary(array $summary, $class) {
if ($class) {
$summary[] = $this
->t('Element class: @class', [
'@class' => $class,
]);
}
return $summary;
}
/**
* Set the class on the element.
*
* @param array $elements
* The original elements render array.
* @param string $class
* The class string.
* @param \Drupal\Core\Field\FieldItemListInterface $items
* The list of field items.
*
* @return array
* The updated elements render array.
*/
public function setElementClass(array $elements, $class, FieldItemListInterface $items) {
foreach ($items as $delta => $item) {
// Add class.
if (!empty($class)) {
$elements[$delta]['#options']['attributes']['class'][] = $class;
}
}
return $elements;
}
/**
* Set the class on the entity.
*
* @param array $elements
* The original elements render array.
* @param string $class
* The class string.
* @param \Drupal\Core\Entity\EntityInterface[] $entities
* The referenced entities.
*
* @return array
* The updated elements render array.
*/
public function setEntityClass(array $elements, $class, array $entities) {
foreach ($entities as $delta => $entity) {
// Add class.
if (!empty($class)) {
$elements[$delta]['#item_attributes']['class'][] = $class;
}
}
return $elements;
}
}
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. | |
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. |