-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvertiser.php
More file actions
250 lines (240 loc) · 8.68 KB
/
Advertiser.php
File metadata and controls
250 lines (240 loc) · 8.68 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
namespace Drupal\drupal10_custom_module\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\example_module\AdvertiserInterface;
use Drupal\user\UserInterface;
use Drupal\Core\Entity\EntityChangedTrait;
/**
* Defines the Advertiser entity.
*
* @ingroup advertiser
*
* @ContentEntityType(
* id = "advertiser",
* label = @Translation("Advertiser"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\example_module\Entity\Controller\AdvertiserListBuilder",
* "form" = {
* "default" = "Drupal\example_module\Form\AdvertiserForm",
* "delete" = "Drupal\example_module\Form\AdvertiserDeleteForm",
* },
* "access" = "Drupal\example_module\AdvertiserAccessControlHandler",
* },
* list_cache_contexts = { "user" },
* base_table = "advertiser",
* admin_permission = "administer advertiser entity",
* entity_keys = {
* "id" = "id",
* "label" = "name",
* "uuid" = "uuid",
* "created" = "created",
* "changed" = "changed",
* },
* links = {
* "canonical" = "/advertiser/{advertiser}",
* "edit-form" = "/advertiser/{advertiser}/edit",
* "delete-form" = "/contact/{advertiser}/delete",
* "collection" = "/advertiser/list"
* },
* field_ui_base_route = "advertiser.advertiser_settings",
* )
*/
class Advertiser extends ContentEntityBase implements AdvertiserInterface {
use EntityChangedTrait;
/**
* {@inheritdoc}
*
* When a new entity instance is added, set the user_id entity reference to
* the current user as the creator of the instance.
*/
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'user_id' => \Drupal::currentUser()->id(),
];
}
/**
* {@inheritdoc}
*/
public function getOwner() {
return $this->get('user_id')->entity;
}
/**
* {@inheritdoc}
*/
public function getOwnerId() {
return $this->get('user_id')->target_id;
}
/**
* {@inheritdoc}
*/
public function setOwnerId($uid) {
$this->set('user_id', $uid);
return $this;
}
/**
* {@inheritdoc}
*/
public function setOwner(UserInterface $account) {
$this->set('user_id', $account->id());
return $this;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
// Standard field, used as unique if primary index.
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Advertiser entity.'))
->setReadOnly(TRUE);
// Standard field, unique outside of the scope of the current project.
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the Advertiser entity.'))
->setReadOnly(TRUE);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The user language code.'))
->setDisplayOptions('form', ['region' => 'hidden']);
// Name field for the contact.
// We set display options for the view as well as the form.
// Users with correct privileges can change the view and edit configuration.
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the Advertiser entity.'))
->setSettings([
'max_length' => 255,
'text_processing' => 0,
])
// Set no default value.
->setDefaultValue(NULL)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -6,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -6,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['first_name'] = BaseFieldDefinition::create('string')
->setLabel(t('First Name'))
->setDescription(t('The name of the Advertiser entity.'))
->setSettings([
'max_length' => 255,
'text_processing' => 0,
])
// Set no default value.
->setDefaultValue(NULL)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -6,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -6,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['image'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Promotion image'))
->setDescription(t('Recommended image size is 1600x900 pixels.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setRequired(FALSE)
->setSetting('target_type', 'media')
->setSetting('handler', 'default:media')
->setSettings([
'handler_settings' => [
'target_bundles' => [
'image' => 'image',
],
'sort' => [
'field' => '_none',
],
'auto_create' => FALSE,
'auto_create_bundle' => '',
],
])
->setCardinality(10)
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', [
'settings' => [
'entity_browser' => 'image_browser',
'field_widget_display' => 'rendered_entity',
'field_widget_edit' => TRUE,
'field_widget_remove' => TRUE,
'open' => TRUE,
'selection_mode' => 'selection_append',
'field_widget_display_settings' => [
'view_mode' => 'default',
],
'field_widget_replace' => FALSE,
],
'type' => 'entity_browser_entity_reference',
'weight' => 3,
])
->setDisplayOptions('view', [
'label' => 'hidden',
'settings' => [
'target_type' => 'media',
],
'weight' => 3,
]);
$fields['field_image'] = BaseFieldDefinition::create('image')
->setLabel(t('Image'))
->setDescription(t('Image field'))
->setSettings([
'file_directory' => 'IMAGE_FOLDER',
'alt_field_required' => FALSE,
'file_extensions' => 'png jpg jpeg',
])
->setDisplayOptions('view', array(
'label' => 'hidden',
'type' => 'default',
'weight' => 0,
))
->setDisplayOptions('form', array(
'label' => 'hidden',
'type' => 'image_image',
'weight' => 0,
))
->setCardinality(5)
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('User Name'))
->setDescription(t('The Name of the associated user.'))
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'author',
'weight' => -3,
))
->setDisplayOptions('form', array(
'type' => 'entity_reference_autocomplete',
'settings' => array(
'match_operator' => 'CONTAINS',
'size' => 60,
'placeholder' => '',
),
'weight' => -3,
))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the entity was created.'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'));
return $fields;
}
}