RgItem.php in Brazilian IDs 8
File
src/Plugin/Field/FieldType/RgItem.php
View source
<?php
namespace Drupal\brazilian_ids\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Form\FormStateInterface;
class RgItem extends FieldItemBase {
public static function defaultStorageSettings() {
return [
'number_only' => FALSE,
] + parent::defaultStorageSettings();
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$schema = [
'columns' => [
'number' => [
'type' => 'varchar',
'length' => 20,
],
],
];
if (!$field_definition
->getSetting('number_only')) {
$schema['columns'] += [
'agency' => [
'type' => 'varchar',
'length' => 60,
],
'state' => [
'type' => 'varchar',
'length' => 2,
],
];
}
return $schema;
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties = [];
$properties['number'] = DataDefinition::create('string')
->setLabel(t('The RG number.'));
$properties['agency'] = DataDefinition::create('string')
->setLabel(t('The issuing agency.'));
$properties['state'] = DataDefinition::create('string')
->setLabel(t('The two-letter state code.'));
return $properties;
}
public function isEmpty() {
$number = $this
->get('number')
->getValue();
return empty($number);
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$element['number_only'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Number only'),
'#description' => $this
->t('If checked, the issuing agency and state will NOT be collected. This option can not be changed after data has been stored.'),
'#default_value' => $this
->getSetting('number_only'),
'#disabled' => $has_data,
];
return $element;
}
}
Classes
Name |
Description |
RgItem |
Field type for RG numbers. |