You are here

ExistingAutocompleteFieldWidget.php in Existing Values Autocomplete Widget 8

Same filename and directory in other branches
  1. 2.x src/Plugin/Field/FieldWidget/ExistingAutocompleteFieldWidget.php

File

src/Plugin/Field/FieldWidget/ExistingAutocompleteFieldWidget.php
View source
<?php

namespace Drupal\existing_values_autocomplete_widget\Plugin\Field\FieldWidget;

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

/**
 * Plugin implementation of the 'existing_autocomplete_field_widget' widget.
 *
 * @FieldWidget(
 *   id = "existing_autocomplete_field_widget",
 *   label = @Translation("Autocomplete: existing values"),
 *   field_types = {
 *     "string"
 *   }
 * )
 */
class ExistingAutocompleteFieldWidget extends WidgetBase {

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

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $elements = [];
    $elements['size'] = [
      '#type' => 'number',
      '#title' => t('Size of textfield'),
      '#default_value' => $this
        ->getSetting('size'),
      '#required' => TRUE,
      '#min' => 1,
    ];
    $elements['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.'),
    ];
    $elements['suggestions_count'] = [
      '#type' => 'number',
      '#title' => t('How many autocomplete suggestions to show?'),
      '#default_value' => $this
        ->getSetting('suggestions_count'),
      '#required' => TRUE,
      '#min' => 1,
    ];
    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = [];
    $summary[] = t('Textfield size: @size', [
      '@size' => $this
        ->getSetting('size'),
    ]);
    if (!empty($this
      ->getSetting('placeholder'))) {
      $summary[] = t('Placeholder: @placeholder', [
        '@placeholder' => $this
          ->getSetting('placeholder'),
      ]);
    }
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element['value'] = $element + [
      '#type' => 'textfield',
      '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
      '#size' => $this
        ->getSetting('size'),
      '#placeholder' => $this
        ->getSetting('placeholder'),
      '#maxlength' => $this
        ->getFieldSetting('max_length'),
      '#autocomplete_route_name' => 'existing_values_autocomplete_widget.autocomplete',
      '#autocomplete_route_parameters' => array(
        'field_name' => $items
          ->getName(),
        'count' => $this
          ->getSetting('suggestions_count'),
        'entity_type_id' => $this->fieldDefinition
          ->getTargetEntityTypeId(),
      ),
    ];
    return $element;
  }

}

Classes

Namesort descending Description
ExistingAutocompleteFieldWidget Plugin implementation of the 'existing_autocomplete_field_widget' widget.