Version 1
[yaffs-website] / web / core / modules / system / tests / modules / entity_test / src / Plugin / Field / FieldType / ShapeItem.php
1 <?php
2
3 namespace Drupal\entity_test\Plugin\Field\FieldType;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\Core\TypedData\DataDefinition;
7 use Drupal\Core\Field\FieldItemBase;
8
9 /**
10  * Defines the 'shape' field type.
11  *
12  * @FieldType(
13  *   id = "shape",
14  *   label = @Translation("Shape"),
15  *   description = @Translation("Another dummy field type."),
16  * )
17  */
18 class ShapeItem extends FieldItemBase {
19
20   /**
21    * {@inheritdoc}
22    */
23   public static function defaultStorageSettings() {
24     return [
25       'foreign_key_name' => 'shape',
26     ] + parent::defaultStorageSettings();
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
33     $properties['shape'] = DataDefinition::create('string')
34       ->setLabel(t('Shape'));
35
36     $properties['color'] = DataDefinition::create('string')
37       ->setLabel(t('Color'));
38
39     return $properties;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public static function schema(FieldStorageDefinitionInterface $field_definition) {
46     $foreign_keys = [];
47     // The 'foreign keys' key is not always used in tests.
48     if ($field_definition->getSetting('foreign_key_name')) {
49       $foreign_keys['foreign keys'] = [
50         // This is a dummy foreign key definition, references a table that
51         // doesn't exist, but that's not a problem.
52         $field_definition->getSetting('foreign_key_name') => [
53           'table' => $field_definition->getSetting('foreign_key_name'),
54           'columns' => [$field_definition->getSetting('foreign_key_name') => 'id'],
55         ],
56       ];
57     }
58     return [
59       'columns' => [
60         'shape' => [
61           'type' => 'varchar',
62           'length' => 32,
63         ],
64         'color' => [
65           'type' => 'varchar',
66           'length' => 32,
67         ],
68       ],
69     ] + $foreign_keys;
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function isEmpty() {
76     $item = $this->getValue();
77     return empty($item['shape']) && empty($item['color']);
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public static function mainPropertyName() {
84     return 'shape';
85   }
86
87 }