ShapeItem.php in Drupal 9
File
core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/ShapeItem.php
View source
<?php
namespace Drupal\entity_test\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Field\FieldItemBase;
class ShapeItem extends FieldItemBase {
public static function defaultStorageSettings() {
return [
'foreign_key_name' => 'shape',
] + parent::defaultStorageSettings();
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['shape'] = DataDefinition::create('string')
->setLabel(t('Shape'));
$properties['color'] = DataDefinition::create('string')
->setLabel(t('Color'));
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$foreign_keys = [];
if ($field_definition
->getSetting('foreign_key_name')) {
$foreign_keys['foreign keys'] = [
$field_definition
->getSetting('foreign_key_name') => [
'table' => $field_definition
->getSetting('foreign_key_name'),
'columns' => [
$field_definition
->getSetting('foreign_key_name') => 'id',
],
],
];
}
return [
'columns' => [
'shape' => [
'type' => 'varchar',
'length' => 32,
],
'color' => [
'type' => 'varchar',
'length' => 32,
],
],
] + $foreign_keys;
}
public function isEmpty() {
$item = $this
->getValue();
return empty($item['shape']) && empty($item['color']);
}
public static function mainPropertyName() {
return 'shape';
}
}