You are here

MappedTermsTextfieldWidget.php in Search API Field Map 4.x

File

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

namespace Drupal\search_api_field_map\Plugin\Field\FieldWidget;

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

/**
 * Plugin implementation of the 'mapped_terms_textfield' widget.
 *
 * @FieldWidget(
 *   id = "mapped_terms_textfield",
 *   label = @Translation("Mapped Terms Textfield"),
 *   field_types = {
 *     "mapped_terms"
 *   }
 * )
 */
class MappedTermsTextfieldWidget extends WidgetBase {

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

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

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

  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element['value'] = [
      '#type' => 'textfield',
      '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
      '#description' => $this
        ->t('This mapped term is used as a facet value in your search application.  It should consist of a hierarchy made up of a type (i.e. "Condition") and term (i.e. "Diabetes"), separated by ">".  For example: Condition>Diabetes.'),
      '#size' => $this
        ->getSetting('size'),
      '#placeholder' => $this
        ->getSetting('placeholder'),
      '#maxlength' => $this
        ->getFieldSetting('max_length'),
      '#attributes' => [
        'class' => [
          'js-text-full',
          'text-full',
        ],
      ],
    ];
    return $element;
  }

}

Classes

Namesort descending Description
MappedTermsTextfieldWidget Plugin implementation of the 'mapped_terms_textfield' widget.