protected function PhoneLinkFieldFormatter::viewValue in Phone link 8
Generate the output appropriate for one field item.
Parameters
\Drupal\Core\Field\FieldItemInterface $item: One field item.
Return value
string The textual output generated.
1 call to PhoneLinkFieldFormatter::viewValue()
- PhoneLinkFieldFormatter::viewElements in src/
Plugin/ Field/ FieldFormatter/ PhoneLinkFieldFormatter.php - Builds a renderable array for a field value.
File
- src/
Plugin/ Field/ FieldFormatter/ PhoneLinkFieldFormatter.php, line 131
Class
- PhoneLinkFieldFormatter
- Plugin implementation of the 'phone_link_field_formatter' formatter.
Namespace
Drupal\phone_link\Plugin\Field\FieldFormatterCode
protected function viewValue(FieldItemInterface $item, $langcode) {
$phone = $item->value;
// Get formatter settings
$settingTitle = $this
->getSetting('title') ?: '';
$settingText = $this
->getSetting('text') ? new FormattableMarkup($this
->getSetting('text'), [
'@phone' => $phone,
]) : $phone;
$settingType = $this
->getSetting('type') ?: 'tel';
// Check allowed protocols
$allowedProtocols = UrlHelper::getAllowedProtocols();
// Add protocol if not defined
if (!in_array($settingType, $allowedProtocols)) {
$allowedProtocols[] = $settingType;
UrlHelper::setAllowedProtocols($allowedProtocols);
}
// Remove all non-phone symbols
$clean_phone = preg_replace('/[^\\d+]/', '', $phone);
/// Make link
$url = Url::fromUri("{$settingType}:" . substr($clean_phone, 0, 15));
$link = Link::fromTextAndUrl($settingText, $url)
->toRenderable();
/// Add attributes
$link['#options']['attributes']['title'] = new FormattableMarkup($settingTitle, [
'@phone' => $phone,
]);
$link['#options']['attributes']['class'][] = 'phone-link';
return $link;
}