You are here

public function VotingApiReactionFormatter::viewElements in Voting API Reaction 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/VotingApiReactionFormatter.php, line 146

Class

VotingApiReactionFormatter
Plugin implementation of the 'votingapi_reaction_default' formatter.

Namespace

Drupal\votingapi_reaction\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  if (is_null($items->status)) {
    $default_value = $items
      ->getFieldDefinition()
      ->getDefaultValue($items
      ->getEntity());
    $items->status = $default_value[0]['status'];
  }
  $extras = [
    'field_items' => $items,
    'formatter_settings' => $this
      ->getSettings(),
  ];
  $entity = Vote::create([
    'type' => '',
    'entity_id' => $items
      ->getEntity()
      ->id(),
    'entity_type' => $items
      ->getEntity()
      ->getEntityTypeId(),
    'value_type' => 'option',
    'value' => 1,
    'field_name' => $items
      ->getName(),
  ]);
  $form = $this->formBuilder
    ->getForm($entity, 'votingapi_reaction', $extras);
  if ($form['#access']) {
    $elements[] = [
      'form' => $form,
    ];
  }
  return $elements;
}