class TelephoneLinkFormatter

Plugin implementation of the 'telephone_link' formatter.

Plugin annotation


@FieldFormatter(
  id = "telephone_link",
  module = "telephone",
  label = @Translation("Telephone link"),
  field_types = {
    "telephone"
  },
  settings = {
    "title" = ""
  }
)

Hierarchy

Expanded class hierarchy of TelephoneLinkFormatter

File

drupal/core/modules/telephone/lib/Drupal/telephone/Plugin/field/formatter/TelephoneLinkFormatter.php, line 30
Contains \Drupal\telephone\Plugin\field\formatter\TelephoneLinkFormatter.

Namespace

Drupal\telephone\Plugin\field\formatter
View source
class TelephoneLinkFormatter extends FormatterBase {

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, array &$form_state) {
    $elements['title'] = array(
      '#type' => 'textfield',
      '#title' => t('Title to replace basic numeric telephone number display.'),
      '#default_value' => $this
        ->getSetting('title'),
    );
    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = array();
    $settings = $this
      ->getSettings();
    if (!empty($settings['title'])) {
      $summary[] = t('Link using text: @title', array(
        '@title' => $settings['title'],
      ));
    }
    else {
      $summary[] = t('Link using provided telephone number.');
    }
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function prepareView(array $entities, $langcode, array &$items) {
    $settings = $this
      ->getSettings();
    foreach ($entities as $id => $entity) {
      foreach ($items[$id] as &$item) {

        // If available, set custom link text.
        if (!empty($settings['title'])) {
          $item['title'] = $settings['title'];
        }
        else {
          $item['title'] = $item['value'];
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(EntityInterface $entity, $langcode, array $items) {
    $element = array();
    foreach ($items as $delta => $item) {

      // Prepend 'tel:' to the telephone number.
      $href = 'tel:' . rawurlencode(preg_replace('/\\s+/', '', $item['value']));

      // Render each element as link.
      $element[$delta] = array(
        '#type' => 'link',
        '#title' => $item['title'],
        '#href' => $href,
        '#options' => array(
          'external' => TRUE,
        ),
      );
    }
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormatterBase::$field protected property The field definition.
FormatterBase::$instance protected property The field instance definition.
FormatterBase::$label protected property The label display setting.
FormatterBase::$settings protected property The formatter settings. Overrides PluginSettingsBase::$settings
FormatterBase::$viewMode protected property The view mode.
FormatterBase::view public function Builds a renderable array for one field on one entity instance. Overrides FormatterInterface::view
FormatterBase::__construct public function Constructs a FormatterBase object. Overrides PluginBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::getPluginDefinition public function Returns the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Returns the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::getDefaultSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getDefaultSettings(). Overrides PluginSettingsInterface::getDefaultSettings
PluginSettingsBase::getSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSetting(). Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSettings(). Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::setSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSetting(). Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSettings(). Overrides PluginSettingsInterface::setSettings
TelephoneLinkFormatter::prepareView public function Allows formatters to load information for field values being displayed. Overrides FormatterBase::prepareView
TelephoneLinkFormatter::settingsForm public function Returns a form to configure settings for the formatter. Overrides FormatterBase::settingsForm
TelephoneLinkFormatter::settingsSummary public function Returns a short summary for the current formatter settings. Overrides FormatterBase::settingsSummary
TelephoneLinkFormatter::viewElements public function Builds a renderable array for a field value. Overrides FormatterInterface::viewElements