You are here

public function StarRatingFormatter::viewElements in Star Rating 8.2

Same name and namespace in other branches
  1. 8.4 src/Plugin/Field/FieldFormatter/StarRatingFormatter.php \Drupal\starrating\Plugin\Field\FieldFormatter\StarRatingFormatter::viewElements()
  2. 8 src/Plugin/Field/FieldFormatter/StarRatingFormatter.php \Drupal\starrating\Plugin\Field\FieldFormatter\StarRatingFormatter::viewElements()
  3. 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\FieldFormatter

Code

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] = [
      '#markup' => $this
        ->formatterExecute($rate, $min, $max, $icon_type, $icon_color, $fill_blank),
    ];
  }
  $elements['#attached']['library'][] = 'starrating/' . $icon_type;
  return $elements;
}