InternationalPhoneDefaultFormatter.php in International Phone Field 8
File
src/Plugin/Field/FieldFormatter/InternationalPhoneDefaultFormatter.php
View source
<?php
namespace Drupal\international_phone\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Component\Utility\SafeMarkup;
class InternationalPhoneDefaultFormatter extends FormatterBase {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$text = '';
if (isset($item->value)) {
$text = SafeMarkup::checkPlain($item->value);
if (strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') !== FALSE) {
$text = '<a href="tel:' . $text . '">' . $text . '</a>';
}
}
$elements[$delta] = [
'#type' => 'markup',
'#markup' => $text,
'#attached' => [
'library' => [
'international_phone/international_phone',
],
],
];
}
return $elements;
}
}