class UriWidget

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/UriWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\UriWidget
  2. 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/UriWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\UriWidget

Plugin implementation of the 'uri' widget.

Plugin annotation


@FieldWidget(
  id = "uri",
  label = @Translation("URI field"),
  field_types = {
    "uri",
  }
)

Hierarchy

Expanded class hierarchy of UriWidget

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/UriWidget.php, line 20

Namespace

Drupal\Core\Field\Plugin\Field\FieldWidget
View source
class UriWidget extends WidgetBase {
  
  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      'size' => 60,
      'placeholder' => '',
    ] + parent::defaultSettings();
  }
  
  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $element['size'] = [
      '#type' => 'number',
      '#title' => $this->t('Size of URI field'),
      '#default_value' => $this->getSetting('size'),
      '#required' => TRUE,
      '#min' => 1,
    ];
    $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 = [];
    $summary[] = $this->t('URI field size: @size', [
      '@size' => $this->getSetting('size'),
    ]);
    $placeholder = $this->getSetting('placeholder');
    if (!empty($placeholder)) {
      $summary[] = $this->t('Placeholder: @placeholder', [
        '@placeholder' => $placeholder,
      ]);
    }
    return $summary;
  }
  
  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element['value'] = $element + [
      '#type' => 'url',
      '#default_value' => $items[$delta]->value ?? NULL,
      '#size' => $this->getSetting('size'),
      '#placeholder' => $this->getSetting('placeholder'),
      '#maxlength' => $this->getFieldSetting('max_length'),
    ];
    return $element;
  }

}

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