3 namespace Drupal\telephone\Plugin\Field\FieldFormatter;
5 use Drupal\Core\Field\FormatterBase;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Form\FormStateInterface;
11 * Plugin implementation of the 'telephone_link' formatter.
14 * id = "telephone_link",
15 * label = @Translation("Telephone link"),
21 class TelephoneLinkFormatter extends FormatterBase {
26 public static function defaultSettings() {
29 ] + parent::defaultSettings();
35 public function settingsForm(array $form, FormStateInterface $form_state) {
36 $elements['title'] = [
37 '#type' => 'textfield',
38 '#title' => t('Title to replace basic numeric telephone number display'),
39 '#default_value' => $this->getSetting('title'),
48 public function settingsSummary() {
50 $settings = $this->getSettings();
52 if (!empty($settings['title'])) {
53 $summary[] = t('Link using text: @title', ['@title' => $settings['title']]);
56 $summary[] = t('Link using provided telephone number.');
65 public function viewElements(FieldItemListInterface $items, $langcode) {
67 $title_setting = $this->getSetting('title');
69 foreach ($items as $delta => $item) {
70 // If the telephone number is 5 or less digits, parse_url() will think
71 // it's a port number rather than a phone number which causes the link
72 // formatter to throw an InvalidArgumentException. Avoid this by inserting
73 // a dash (-) after the first digit - RFC 3966 defines the dash as a
74 // visual separator character and so will be removed before the phone
75 // number is used. See https://bugs.php.net/bug.php?id=70588 for more.
76 // While the bug states this only applies to numbers <= 65535, a 5 digit
77 // number greater than 65535 will cause parse_url() to return FALSE so
78 // we need the work around on any 5 digit (or less) number.
79 // First we strip whitespace so we're counting actual digits.
80 $phone_number = preg_replace('/\s+/', '', $item->value);
81 if (strlen($phone_number) <= 5) {
82 $phone_number = substr_replace($phone_number, '-', 1, 0);
85 // Render each element as link.
88 // Use custom title if available, otherwise use the telephone number
90 '#title' => $title_setting ?: $item->value,
91 // Prepend 'tel:' to the telephone number.
92 '#url' => Url::fromUri('tel:' . rawurlencode($phone_number)),
93 '#options' => ['external' => TRUE],
96 if (!empty($item->_attributes)) {
97 $element[$delta]['#options'] += ['attributes' => []];
98 $element[$delta]['#options']['attributes'] += $item->_attributes;
99 // Unset field item attributes since they have been included in the
100 // formatter output and should not be rendered in the field template.
101 unset($item->_attributes);