public function StarRatingFormatter::viewElements in Star Rating 8.4
Same name and namespace in other branches
- 8 src/Plugin/Field/FieldFormatter/StarRatingFormatter.php \Drupal\starrating\Plugin\Field\FieldFormatter\StarRatingFormatter::viewElements()
- 8.2 src/Plugin/Field/FieldFormatter/StarRatingFormatter.php \Drupal\starrating\Plugin\Field\FieldFormatter\StarRatingFormatter::viewElements()
- 8.3 src/Plugin/Field/FieldFormatter/StarRatingFormatter.php \Drupal\starrating\Plugin\Field\FieldFormatter\StarRatingFormatter::viewElements()
Builds a renderable array for a field value.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
string $langcode: The language that should be used to render the field.
Return value
array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.
Overrides FormatterInterface::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ StarRatingFormatter.php, line 37
Class
- StarRatingFormatter
- Plugin implementation of the 'Starrating' formatter.
Namespace
Drupal\starrating\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$field_settings = $this
->getFieldSettings();
$max = $field_settings['max_value'];
$min = 0;
$icon_type = $this
->getSetting('icon_type');
$icon_color = $this
->getSetting('icon_color');
$fill_blank = $this
->getSetting('fill_blank');
foreach ($items as $delta => $item) {
$rate = $item->value;
$elements[$delta] = [
'#theme' => 'starrating_formatter',
'#rate' => $rate,
'#min' => $min,
'#max' => $max,
'#icon_type' => $icon_type,
'#icon_color' => $icon_color,
'#fill_blank' => $fill_blank,
];
}
$elements['#attached']['library'][] = 'starrating/' . $icon_type;
return $elements;
}