View source
<?php
namespace Drupal\phone_link\Plugin\Field\FieldFormatter;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
class PhoneLinkFieldFormatter extends FormatterBase {
public static function defaultSettings() {
return [
'title' => t('Call to @phone'),
'text' => '',
'type' => 'tel',
] + parent::defaultSettings();
}
public static function getPhoneTypes() {
return [
'tel' => t('Default phones'),
'callto' => t('Skype format'),
];
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements['title'] = [
'#type' => 'textfield',
'#title' => $this
->t('Title tip'),
'#description' => $this
->t('Provide "title" HTML-attribute for phone link. You can use "@phone" replacement (without quotes).'),
'#default_value' => $this
->getSetting('title'),
];
$elements['text'] = [
'#type' => 'textfield',
'#title' => $this
->t('Replace phone number'),
'#description' => $this
->t('Text displayed instead of phone. You can use "@phone" replacement (without quotes).'),
'#default_value' => $this
->getSetting('text'),
];
$elements['type'] = [
'#type' => 'select',
'#options' => $this
->getPhoneTypes(),
'#title' => $this
->t('Type of link'),
'#description' => $this
->t('Choose the type of phone link. Default phones: "tel:", or Skype-format "callto:".'),
'#default_value' => $this
->getSetting('type'),
];
return $elements;
}
public function settingsSummary() {
$summary = [];
$settings = $this
->getSettings();
if (!empty($settings['title'])) {
$summary[] = $this
->t('Title attribute: @title', [
'@title' => $settings['title'],
]);
}
else {
$summary[] = $this
->t('No title attribute.');
}
if (!empty($settings['text'])) {
$summary[] = $this
->t('Text: @text', [
'@text' => $settings['text'],
]);
}
else {
$summary[] = $this
->t('No text replacement.');
}
if (!empty($settings['type'])) {
$types = $this
->getPhoneTypes();
$summary[] = $this
->t('Phone link type: @type', [
'@type' => $types[$settings['type']],
]);
}
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$elements[$delta] = $this
->viewValue($item, $langcode);
}
return $elements;
}
protected function viewValue(FieldItemInterface $item, $langcode) {
$phone = $item->value;
$settingTitle = $this
->getSetting('title') ?: '';
$settingText = $this
->getSetting('text') ? new FormattableMarkup($this
->getSetting('text'), [
'@phone' => $phone,
]) : $phone;
$settingType = $this
->getSetting('type') ?: 'tel';
$allowedProtocols = UrlHelper::getAllowedProtocols();
if (!in_array($settingType, $allowedProtocols)) {
$allowedProtocols[] = $settingType;
UrlHelper::setAllowedProtocols($allowedProtocols);
}
$clean_phone = preg_replace('/[^\\d+]/', '', $phone);
$url = Url::fromUri("{$settingType}:" . substr($clean_phone, 0, 15));
$link = Link::fromTextAndUrl($settingText, $url)
->toRenderable();
$link['#options']['attributes']['title'] = new FormattableMarkup($settingTitle, [
'@phone' => $phone,
]);
$link['#options']['attributes']['class'][] = 'phone-link';
return $link;
}
}