public function PhoneInternationalDefaultFormatter::viewElements in International Phone 8
Same name and namespace in other branches
- 8.2 src/Plugin/Field/FieldFormatter/PhoneInternationalDefaultFormatter.php \Drupal\phone_international\Plugin\Field\FieldFormatter\PhoneInternationalDefaultFormatter::viewElements()
- 3.x src/Plugin/Field/FieldFormatter/PhoneInternationalDefaultFormatter.php \Drupal\phone_international\Plugin\Field\FieldFormatter\PhoneInternationalDefaultFormatter::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/ PhoneInternationalDefaultFormatter.php, line 27
Class
- PhoneInternationalDefaultFormatter
- Plugin implementation of the 'phone_international_formatter' formatter.
Namespace
Drupal\phone_international\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$phone_number = $this
->viewValue($item);
// Render each element as link.
$elements[$delta] = [
'#type' => 'link',
'#title' => $phone_number,
// Prepend 'tel:' to the telephone number.
'#url' => Url::fromUri('tel:' . $phone_number),
'#options' => [
'external' => TRUE,
],
];
if (!empty($item->_attributes)) {
$elements[$delta]['#options'] += [
'attributes' => [],
];
$elements[$delta]['#options']['attributes'] += $item->_attributes;
// Unset field item attributes since they have been included in the
// formatter output and should not be rendered in the field template.
unset($item->_attributes);
}
}
return $elements;
}