public function VotingApiFormatter::viewElements in Votingapi Widgets 8
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/ VotingApiFormatter.php, line 163
Class
- VotingApiFormatter
- Plugin implementation of the 'voting_api_formatter' formatter.
Namespace
Drupal\votingapi_widgets\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$entity = $items
->getEntity();
// Do not continue if the entity is being previewed.
if (!empty($entity->in_preview)) {
return $elements;
}
$field_settings = $this
->getFieldSettings();
$field_name = $this->fieldDefinition
->getName();
$vote_type = $field_settings['vote_type'];
$vote_plugin = $field_settings['vote_plugin'];
$show_own_vote = $this
->getSetting('show_own_vote') ? TRUE : FALSE;
$elements[] = [
'vote_form' => [
'#lazy_builder' => [
'voting_api.lazy_loader:buildForm',
[
$vote_plugin,
$entity
->getEntityTypeId(),
$entity
->bundle(),
$entity
->id(),
$vote_type,
$field_name,
serialize($this
->getSettings()),
],
],
'#create_placeholder' => TRUE,
],
'results' => [],
];
return $elements;
}