View source
<?php
namespace Drupal\field_nif\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
class NifItem extends FieldItemBase {
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(t('NIF value'))
->setComputed(TRUE)
->setClass('\\Drupal\\field_nif\\NifProcessed')
->setSetting('document source', [
'first_letter',
'number',
'last_letter',
]);
$properties['number'] = DataDefinition::create('string')
->setLabel(t('Document number'));
$properties['first_letter'] = DataDefinition::create('string')
->setLabel(t('First letter of the NIF/CIF/NIE'));
$properties['last_letter'] = DataDefinition::create('string')
->setLabel(t('Last letter of the NIF/CIF/NIE'));
$properties['type'] = DataDefinition::create('string')
->setLabel(t('Type of document'));
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$schema = [
'columns' => [
'number' => [
'type' => 'varchar',
'length' => 8,
],
'first_letter' => [
'type' => 'varchar',
'length' => 1,
],
'last_letter' => [
'type' => 'varchar',
'length' => 1,
],
'type' => [
'type' => 'varchar',
'length' => 3,
],
],
];
return $schema;
}
public function getConstraints() {
$constraints = parent::getConstraints();
$constraint_manager = \Drupal::typedDataManager()
->getValidationConstraintManager();
$constraints[] = $constraint_manager
->create('ComplexData', [
'value' => [
'NifValue' => [
'supportedTypes' => array_filter($this
->getSetting('supported_types')),
],
],
]);
return $constraints;
}
public static function defaultFieldSettings() {
return [
'supported_types' => [
'nif',
'cif',
'nie',
],
] + parent::defaultFieldSettings();
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = [];
$element['supported_types'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Supported document types'),
'#default_value' => $this
->getSetting('supported_types'),
'#options' => [
'nif' => $this
->t('NIF'),
'cif' => $this
->t('CIF'),
'nie' => $this
->t('NIE'),
],
'#required' => TRUE,
];
return $element;
}
public function isEmpty() {
$value = $this
->get('value')
->getValue();
return $value === NULL || $value === '';
}
}