CountryItem.php in Address 8
File
src/Plugin/Field/FieldType/CountryItem.php
View source
<?php
namespace Drupal\address\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
class CountryItem extends FieldItemBase {
use AvailableCountriesTrait;
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'char',
'length' => 2,
'not null' => FALSE,
],
],
'indexes' => [
'value' => [
'value',
],
],
];
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(t('Country'));
return $properties;
}
public static function defaultFieldSettings() {
return self::defaultCountrySettings();
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
return $this
->countrySettingsForm($form, $form_state);
}
public function isEmpty() {
return empty($this->value);
}
public function getConstraints() {
$constraints = parent::getConstraints();
$constraint_manager = $this
->getTypedDataManager()
->getValidationConstraintManager();
$constraints[] = $constraint_manager
->create('ComplexData', [
'value' => [
'Country' => [
'availableCountries' => $this
->getAvailableCountries(),
],
],
]);
return $constraints;
}
}
Classes
Name |
Description |
CountryItem |
Plugin implementation of the 'address_country' field type. |