TelephoneDefaultWidget.php in Drupal 10
File
core/modules/telephone/src/Plugin/Field/FieldWidget/TelephoneDefaultWidget.php
View source
<?php
namespace Drupal\telephone\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
class TelephoneDefaultWidget extends WidgetBase {
public static function defaultSettings() {
return [
'placeholder' => '',
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['placeholder'] = [
'#type' => 'textfield',
'#title' => t('Placeholder'),
'#default_value' => $this
->getSetting('placeholder'),
'#description' => 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;
}
public function settingsSummary() {
$summary = [];
$placeholder = $this
->getSetting('placeholder');
if (!empty($placeholder)) {
$summary[] = t('Placeholder: @placeholder', [
'@placeholder' => $placeholder,
]);
}
else {
$summary[] = t('No placeholder');
}
return $summary;
}
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'),
];
return $element;
}
}