You are here

public function SimplenewsSubscriptionStatusFormatter::viewElements in Simplenews 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/SimplenewsSubscriptionStatusFormatter.php \Drupal\simplenews\Plugin\Field\FieldFormatter\SimplenewsSubscriptionStatusFormatter::viewElements()
  2. 3.x src/Plugin/Field/FieldFormatter/SimplenewsSubscriptionStatusFormatter.php \Drupal\simplenews\Plugin\Field\FieldFormatter\SimplenewsSubscriptionStatusFormatter::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/SimplenewsSubscriptionStatusFormatter.php, line 24

Class

SimplenewsSubscriptionStatusFormatter
Formatter that displays a newsletter subscription with the status.

Namespace

Drupal\simplenews\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = array();
  foreach ($this
    ->getEntitiesToView($items, $langcode) as $delta => $entity) {
    $label = $entity
      ->label();

    // Do not explicitly display the status for confirmed subscriptions.
    $output = $label;

    // Add status label for the unconfirmed subscriptions.
    if ($items[$delta]->status == SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED) {
      $output = $this
        ->t('@label (Unconfirmed)', array(
        '@label' => $label,
      ));
    }

    // Add status label for the unsubscribed subscriptions.
    if ($items[$delta]->status == SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED) {
      $output = $this
        ->t('@label (Unsubscribed)', array(
        '@label' => $label,
      ));
    }

    // Add the label.
    $elements[$delta]['#markup'] = $output;
  }
  return $elements;
}