0865f0c7c54a25b9dba31061bb7135396d618ddd
[yaffs-website] / web / core / modules / layout_builder / src / Plugin / Field / FieldType / LayoutSectionItem.php
1 <?php
2
3 namespace Drupal\layout_builder\Plugin\Field\FieldType;
4
5 use Drupal\Core\Field\FieldDefinitionInterface;
6 use Drupal\Core\Field\FieldItemBase;
7 use Drupal\Core\Field\FieldStorageDefinitionInterface;
8 use Drupal\Core\StringTranslation\TranslatableMarkup;
9 use Drupal\Core\TypedData\DataDefinition;
10 use Drupal\layout_builder\Section;
11
12 /**
13  * Plugin implementation of the 'layout_section' field type.
14  *
15  * @internal
16  *
17  * @FieldType(
18  *   id = "layout_section",
19  *   label = @Translation("Layout Section"),
20  *   description = @Translation("Layout Section"),
21  *   list_class = "\Drupal\layout_builder\Field\LayoutSectionItemList",
22  *   no_ui = TRUE,
23  *   cardinality = \Drupal\Core\Field\FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED
24  * )
25  *
26  * @property \Drupal\layout_builder\Section section
27  */
28 class LayoutSectionItem extends FieldItemBase {
29
30   /**
31    * {@inheritdoc}
32    */
33   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
34     $properties['section'] = DataDefinition::create('layout_section')
35       ->setLabel(new TranslatableMarkup('Layout Section'))
36       ->setRequired(FALSE);
37
38     return $properties;
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function __get($name) {
45     // @todo \Drupal\Core\Field\FieldItemBase::__get() does not return default
46     //   values for uninstantiated properties. This will forcibly instantiate
47     //   all properties with the side-effect of a performance hit, resolve
48     //   properly in https://www.drupal.org/node/2413471.
49     $this->getProperties();
50
51     return parent::__get($name);
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public static function mainPropertyName() {
58     return 'section';
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public static function schema(FieldStorageDefinitionInterface $field_definition) {
65     $schema = [
66       'columns' => [
67         'section' => [
68           'type' => 'blob',
69           'size' => 'normal',
70           'serialize' => TRUE,
71         ],
72       ],
73     ];
74
75     return $schema;
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
82     // @todo Expand this in https://www.drupal.org/node/2912331.
83     $values['section'] = new Section('layout_onecol');
84     return $values;
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function isEmpty() {
91     return empty($this->section);
92   }
93
94 }