You are here

public function MailWithTypeFormatter::viewElements in CRM Core 8.2

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

modules/crm_core_contact/src/Plugin/Field/FieldFormatter/MailWithTypeFormatter.php, line 28

Class

MailWithTypeFormatter
Plugin implementation of the 'mail_with_type' formatter.

Namespace

Drupal\crm_core_contact\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $email_types = $this->fieldDefinition
    ->getFieldStorageDefinition()
    ->getSetting('email_types');
  $elements = [];
  foreach ($items as $delta => $item) {
    $elements[$delta] = [
      '#type' => 'link',
      '#title' => $item->value,
      '#url' => Url::fromUri('mailto:' . $item->value),
      '#prefix' => $email_types[$item->type] . ': ',
    ];
  }
  return $elements;
}