You are here

UriWidget.php in Zircon Profile 8

Same filename and directory in other branches
  1. 8.0 core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/UriWidget.php

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/UriWidget.php
View source
<?php

/**
 * @file
 * Contains \Drupal\Core\Field\Plugin\Field\FieldWidget\UriWidget.
 */
namespace Drupal\Core\Field\Plugin\Field\FieldWidget;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Plugin implementation of the 'uri' widget.
 *
 * @FieldWidget(
 *   id = "uri",
 *   label = @Translation("URI field"),
 *   field_types = {
 *     "uri",
 *   }
 * )
 */
class UriWidget extends WidgetBase {

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return array(
      'size' => 60,
      'placeholder' => '',
    ) + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $element['size'] = array(
      '#type' => 'number',
      '#title' => $this
        ->t('Size of URI field'),
      '#default_value' => $this
        ->getSetting('size'),
      '#required' => TRUE,
      '#min' => 1,
    );
    $element['placeholder'] = array(
      '#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 = array();
    $summary[] = $this
      ->t('URI field size: @size', array(
      '@size' => $this
        ->getSetting('size'),
    ));
    $placeholder = $this
      ->getSetting('placeholder');
    if (!empty($placeholder)) {
      $summary[] = $this
        ->t('Placeholder: @placeholder', array(
        '@placeholder' => $placeholder,
      ));
    }
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element['value'] = $element + array(
      '#type' => 'url',
      '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
      '#size' => $this
        ->getSetting('size'),
      '#placeholder' => $this
        ->getSetting('placeholder'),
      '#maxlength' => $this
        ->getFieldSetting('max_length'),
    );
    return $element;
  }

}

Classes

Namesort descending Description
UriWidget Plugin implementation of the 'uri' widget.