public function TelephoneLinkFormatter::viewElements

Builds a renderable array for a field value.

Parameters

Drupal\Core\Entity\EntityInterface $entity: The entity being displayed.

string $langcode: The language associated with $items.

array $items: Array of values for this field.

Return value

array A renderable array for $items, as an array of child elements keyed by numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

drupal/core/modules/telephone/lib/Drupal/telephone/Plugin/field/formatter/TelephoneLinkFormatter.php, line 85
Contains \Drupal\telephone\Plugin\field\formatter\TelephoneLinkFormatter.

Class

TelephoneLinkFormatter
Plugin implementation of the 'telephone_link' formatter.

Namespace

Drupal\telephone\Plugin\field\formatter

Code

public function viewElements(EntityInterface $entity, $langcode, array $items) {
  $element = array();
  foreach ($items as $delta => $item) {

    // Prepend 'tel:' to the telephone number.
    $href = 'tel:' . rawurlencode(preg_replace('/\\s+/', '', $item['value']));

    // Render each element as link.
    $element[$delta] = array(
      '#type' => 'link',
      '#title' => $item['title'],
      '#href' => $href,
      '#options' => array(
        'external' => TRUE,
      ),
    );
  }
  return $element;
}