You are here

public function EmailContactLinkFormatter::viewElements in Email Contact 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/EmailContactLinkFormatter.php, line 95

Class

EmailContactLinkFormatter
Plugin implementation of the 'email_contact_link' formatter.

Namespace

Drupal\email_contact\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = array();
  foreach ($items as $delta => $item) {

    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $item
      ->getEntity();
    $elements[$delta]['#markup'] = Link::fromTextAndUrl($this
      ->getSetting('link_text'), new Url('email_contact.form', [
      'entity_type' => $entity
        ->getEntityTypeId(),
      'entity_id' => $entity
        ->id(),
      'field_name' => $items
        ->getName(),
      'view_mode' => $this->viewMode,
    ]))
      ->toString();
    break;
  }
  return $elements;
}