View source
<?php
namespace Drupal\cpf\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\TypedDataInterface;
class CpfItem extends FieldItemBase {
const UNIQUE_VALUES = 0x1;
const SAME_VALUES = 0x10;
public $cpfService = NULL;
public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
parent::__construct($definition, $name, $parent);
$this->cpfService = \Drupal::service('cpf');
}
public static function defaultStorageSettings() {
return [
'unique' => static::SAME_VALUES,
'ignore_bundle' => 0,
] + parent::defaultStorageSettings();
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(t('Digits'))
->setDescription(t('The digits of the CPF.'));
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$schema = [
'columns' => [
'value' => [
'type' => 'text',
'size' => 'tiny',
'not null' => FALSE,
],
],
];
return $schema;
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$element = [];
$element['unique'] = [
'#type' => 'radios',
'#title' => t('Allow only unique values'),
'#default_value' => $this
->getSetting('unique'),
'#options' => [
static::SAME_VALUES => $this
->t('no'),
static::UNIQUE_VALUES => $this
->t('yes'),
],
'#disabled' => $has_data,
];
$element['ignore_bundle'] = [
'#type' => 'checkbox',
'#title' => t('Ignore bundle'),
'#default_value' => $this
->getSetting('ignore_bundle'),
'#description' => $this
->t('If this option is selected, validation of unique values will ignore the entity type (bundle).'),
'#states' => [
'visible' => [
':input[name="settings[unique]"]' => [
'value' => static::UNIQUE_VALUES,
],
],
],
'#disabled' => $has_data,
];
$element += parent::storageSettingsForm($form, $form_state, $has_data);
return $element;
}
public function getConstraints() {
$constraints = parent::getConstraints();
$unique = $this
->getSetting('unique');
$value_constraints = [];
$value_constraints['CpfValue'] = [];
if ($unique == static::UNIQUE_VALUES) {
$value_constraints['CpfUnique'] = [
'fieldDefinition' => $this
->getFieldDefinition(),
'ignoreBundle' => $this
->getSetting('ignore_bundle'),
'entity' => $this
->getEntity(),
];
}
$constraint_manager = \Drupal::typedDataManager()
->getValidationConstraintManager();
$constraints[] = $constraint_manager
->create('ComplexData', [
'value' => $value_constraints,
]);
return $constraints;
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$values['value'] = \Drupal::service('cpf')
->generate();
return $values;
}
public function isEmpty() {
$value = $this
->get('value')
->getValue();
$value = $this->cpfService
->digits($value);
return empty($value);
}
public function setValue($values, $notify = TRUE) {
$values['value'] = $this->cpfService
->digits($values['value']);
parent::setValue($values, $notify);
}
}