a20265e55a1d53ed8cb2d7151d22d8f70ffffc40
[yaffs-website] / TestObjectItem.php
1 <?php
2
3 namespace Drupal\field_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 'test_object_field' entity field item.
11  *
12  * @FieldType(
13  *   id = "test_object_field",
14  *   label = @Translation("Test object field"),
15  *   description = @Translation("Test field type that has an object to test serialization"),
16  *   default_widget = "test_object_field_widget",
17  *   default_formatter = "object_field_test_default"
18  * )
19  */
20 class TestObjectItem extends FieldItemBase {
21
22   /**
23    * {@inheritdoc}
24    */
25   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
26     $properties['value'] = DataDefinition::create('any')
27       ->setLabel(t('Value'))
28       ->setRequired(TRUE);
29
30     return $properties;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public static function schema(FieldStorageDefinitionInterface $field_definition) {
37     return [
38       'columns' => [
39         'value' => [
40           'description' => 'The object item value.',
41           'type' => 'blob',
42           'not null' => TRUE,
43           'serialize' => TRUE,
44         ],
45       ],
46     ];
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function setValue($values, $notify = TRUE) {
53     if (isset($values['value'])) {
54       // @todo Remove this in https://www.drupal.org/node/2788637.
55       if (is_string($values['value'])) {
56         $values['value'] = unserialize($values['value']);
57       }
58     }
59     parent::setValue($values, $notify);
60   }
61
62 }