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;
class NifWidget extends WidgetBase {
public static function defaultSettings() {
return [
'placeholder' => '',
] + parent::defaultSettings();
}
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;
}
public function settingsSummary() {
$summary = [];
if (!empty($this
->getSetting('placeholder'))) {
$summary[] = t('Placeholder: @placeholder', [
'@placeholder' => $this
->getSetting('placeholder'),
]);
}
return $summary;
}
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;
}
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
foreach ($values as &$value) {
if ($nif_values = NifUtils::validateNifCifNie($value['value'], $this
->getFieldSetting('supported_types'))) {
$value += $nif_values;
}
}
return $values;
}
}
Classes
Name |
Description |
NifWidget |
Plugin implementation of the 'nif_default' widget. |