-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPatternkitBlock.php
More file actions
159 lines (141 loc) · 5.34 KB
/
Copy pathPatternkitBlock.php
File metadata and controls
159 lines (141 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace Drupal\patternkit\Entity;
use Drupal\block_content\Entity\BlockContent;
use Drupal\Core\Entity\EditorialContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Defines the custom block entity class.
*
* @ContentEntityType(
* id = "patternkit_block",
* label = @Translation("Patternkit block"),
* label_collection = @Translation("Patternkit blocks"),
* label_singular = @Translation("Patternkit block"),
* label_plural = @Translation("Patternkit blocks"),
* label_count = @PluralTranslation(
* singular = "@count Patternkit block",
* plural = "@count Patternkit blocks",
* ),
* handlers = {
* "storage" = "Drupal\Core\Entity\Sql\SqlContentEntityStorage",
* "access" = "Drupal\block_content\BlockContentAccessControlHandler",
* "list_builder" = "Drupal\block_content\BlockContentListBuilder",
* "view_builder" = "Drupal\block_content\BlockContentViewBuilder",
* "views_data" = "Drupal\patternkit\PatternViewsData",
* "form" = {
* "add" = "Drupal\patternkit\Form\PatternkitForm",
* "edit" = "Drupal\patternkit\Form\PatternkitForm",
* "delete" = "Drupal\block_content\Form\BlockContentDeleteForm",
* "default" = "Drupal\patternkit\Form\PatternkitForm"
* },
* "translation" = "Drupal\patternkit\PatternTranslationHandler"
* },
* admin_permission = "administer blocks",
* base_table = "patternkit",
* revision_table = "patternkit_revision",
* data_table = "patternkit_field_data",
* revision_data_table = "patternkit_field_revision",
* show_revision_ui = TRUE,
* links = {
* "canonical" = "/block/{patternkit_block}",
* "delete-form" = "/block/{patternkit_block}/delete",
* "edit-form" = "/block/{patternkit_block}",
* "collection" = "/admin/structure/block/patternkit",
* "create" = "/block",
* },
* translatable = TRUE,
* entity_keys = {
* "id" = "id",
* "revision" = "revision_id",
* "label" = "info",
* "langcode" = "langcode",
* "uuid" = "uuid",
* "published" = "status",
* "pattern" = "pattern_id",
* "data" = "data",
* },
* revision_metadata_keys = {
* "revision_user" = "revision_user",
* "revision_created" = "revision_created",
* "revision_log_message" = "revision_log"
* },
* render_cache = FALSE,
* )
*
* Note that render caching of block_content entities is disabled because they
* are always rendered as blocks, and blocks already have their own render
* caching.
* See https://www.drupal.org/node/2284917#comment-9132521 for more information.
*
* @todo 2.0 Rename to PatternkitInstance, since this entity could have other
* potential uses other than blocks, such as panes or views stubs.
*/
class PatternkitBlock extends BlockContent {
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type): array {
/** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
$fields = EditorialContentEntityBase::baseFieldDefinitions($entity_type);
$fields['id']->setLabel(t('Custom block ID'))
->setDescription(t('The custom block ID.'));
$fields['uuid']->setDescription(t('The custom block UUID.'));
$fields['revision_id']->setDescription(t('The revision ID.'));
$fields['langcode']->setDescription(t('The custom block language code.'));
$fields['revision_log']->setDescription(t('The log entry explaining the changes in this revision.'));
$fields['info'] = BaseFieldDefinition::create('string')
->setLabel(t('Block description'))
->setDescription(t('A brief description of your block.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setRequired(TRUE)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE)
->addConstraint('UniqueField', []);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the custom block was last edited.'))
->setTranslatable(TRUE)
->setRevisionable(TRUE);
$fields['pattern_id'] = BaseFieldDefinition::create('string')
->setLabel(t('Patternkit Pattern Derivative ID'))
->setDescription(t('The machine name of the Patternkit pattern.'));
$fields['data'] = BaseFieldDefinition::create('serialized_data')
->setLabel(t('Data'))
->setDescription(t('The patternkit block configuration data and content.'))
->setTranslatable(TRUE)
->setRevisionable(TRUE);
$fields['reusable'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Reusable'))
->setDescription(t('A boolean indicating whether this block is reusable.'))
->setTranslatable(FALSE)
->setRevisionable(FALSE)
->setDefaultValue(TRUE);
return $fields;
}
/**
* Sets the block pattern.
*
* @param string $pattern
* The Patternkit pattern derivative id.
*
* @return $this
*/
public function setPattern(string $pattern): self {
$this->set('pattern_id', $pattern);
return $this;
}
/**
* Returns the block Patternkit pattern derivative id.
*
* @return string|null
* The Patternkit pattern derivative id.
*/
public function getPattern(): ?string {
return $this->get('pattern_id')->getString();
}
}