You are here

NifWidget.php in Field NIF 8

File

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

namespace Drupal\field_nif\Plugin\Field\FieldWidget;

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

/**
 * Plugin implementation of the 'nif_default' widget.
 *
 * @FieldWidget(
 *   id = "nif_default",
 *   label = @Translation("NIF/CIF/NIE textfield"),
 *   field_types = {
 *     "nif"
 *   }
 * )
 */
class NifWidget extends WidgetBase {

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

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $elements = [];
    $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.'),
    ];
    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = [];
    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' => 'nif',
      '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
      '#placeholder' => $this
        ->getSetting('placeholder'),
      '#supported_types' => array_filter($this
        ->getFieldSetting('supported_types')),
    ];
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {

    // Split entered document value into the different parts.
    foreach ($values as &$value) {
      if ($nif_values = NifUtils::validateNifCifNie($value['value'], $this
        ->getFieldSetting('supported_types'))) {
        $value += $nif_values;
      }
    }
    return $values;
  }

}

Classes

Namesort descending Description
NifWidget Plugin implementation of the 'nif_default' widget.