TelephoneDefaultWidget.php

Same filename and directory in other branches
  1. 9 core/modules/telephone/src/Plugin/Field/FieldWidget/TelephoneDefaultWidget.php
  2. 8.9.x core/modules/telephone/src/Plugin/Field/FieldWidget/TelephoneDefaultWidget.php
  3. 11.x core/modules/telephone/src/Plugin/Field/FieldWidget/TelephoneDefaultWidget.php

Namespace

Drupal\telephone\Plugin\Field\FieldWidget

File

core/modules/telephone/src/Plugin/Field/FieldWidget/TelephoneDefaultWidget.php

View source
<?php

namespace Drupal\telephone\Plugin\Field\FieldWidget;

use Drupal\Core\Field\Attribute\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\telephone\Plugin\Field\FieldType\TelephoneItem;

/**
 * Plugin implementation of the 'telephone_default' widget.
 */
class TelephoneDefaultWidget extends WidgetBase {
  
  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      'placeholder' => '',
    ] + parent::defaultSettings();
  }
  
  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $element['placeholder'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Placeholder'),
      '#default_value' => $this->getSetting('placeholder'),
      '#description' => $this->t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
    ];
    return $element;
  }
  
  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = [];
    $placeholder = $this->getSetting('placeholder');
    if (!empty($placeholder)) {
      $summary[] = $this->t('Placeholder: @placeholder', [
        '@placeholder' => $placeholder,
      ]);
    }
    else {
      $summary[] = $this->t('No placeholder');
    }
    return $summary;
  }
  
  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element['value'] = $element + [
      '#type' => 'tel',
      '#default_value' => $items[$delta]->value ?? NULL,
      '#placeholder' => $this->getSetting('placeholder'),
      '#maxlength' => TelephoneItem::MAX_LENGTH,
    ];
    return $element;
  }

}

Classes

Title Deprecated Summary
TelephoneDefaultWidget Plugin implementation of the 'telephone_default' widget.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.