107e8c042c687d7874c852d57ada261540e0a604
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / module / content-entity / src / Entity / Example.php.twig
1 <?php
2
3 namespace Drupal\{{ machine_name }}\Entity;
4
5 {% sort %}
6 {% if not revisionable %}
7 use Drupal\Core\Entity\ContentEntityBase;
8 {% endif %}
9 use Drupal\Core\Entity\EntityStorageInterface;
10 use Drupal\Core\Entity\EntityTypeInterface;
11 use Drupal\Core\Field\BaseFieldDefinition;
12 {% if revisionable %}
13 use Drupal\Core\Entity\RevisionableContentEntityBase;
14 {% endif %}
15 use Drupal\{{ machine_name }}\{{ class_prefix }}Interface;
16 {% if author_base_field %}
17 use Drupal\user\UserInterface;
18 {% endif %}
19 {% if changed_base_field %}
20 use Drupal\Core\Entity\EntityChangedTrait;
21 {% endif %}
22 {% endsort %}
23
24 /**
25  * Defines the {{ entity_type_label|lower }} entity class.
26  *
27  * @ContentEntityType(
28  *   id = "{{ entity_type_id }}",
29  *   label = @Translation("{{ entity_type_label }}"),
30  *   label_collection = @Translation("{{ entity_type_label|plural }}"),
31 {% if bundle %}
32  *   bundle_label = @Translation("{{ entity_type_label }} type"),
33 {% endif %}
34  *   handlers = {
35 {% if template %}
36  *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
37 {% else %}
38  *     "view_builder" = "Drupal\{{ machine_name }}\{{ class_prefix }}ViewBuilder",
39 {% endif %}
40  *     "list_builder" = "Drupal\{{ machine_name }}\{{ class_prefix }}ListBuilder",
41  *     "views_data" = "Drupal\views\EntityViewsData",
42 {% if access_controller %}
43  *     "access" = "Drupal\{{ machine_name }}\{{ class_prefix }}AccessControlHandler",
44 {% endif %}
45  *     "form" = {
46  *       "add" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}Form",
47  *       "edit" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}Form",
48  *       "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm"
49  *     },
50  *     "route_provider" = {
51  *       "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
52  *     }
53  *   },
54  *   base_table = "{{ entity_type_id }}",
55  *   data_table = "{{ entity_type_id }}_field_data",
56 {% if revisionable %}
57  *   revision_table = "{{ entity_type_id }}_revision",
58  *   revision_data_table = "{{ entity_type_id }}_field_revision",
59  *   show_revision_ui = TRUE,
60 {% endif %}
61 {% if translatable %}
62  *   translatable = TRUE,
63 {% endif %}
64  *   admin_permission = "administer {{ entity_type_label|lower }}",
65  *   entity_keys = {
66  *     "id" = "id",
67 {% if revisionable %}
68  *     "revision" = "revision_id",
69 {% endif %}
70 {% if translatable %}
71  *     "langcode" = "langcode",
72 {% endif %}
73 {% if bundle %}
74  *     "bundle" = "bundle",
75 {% endif %}
76  *     "label" = "{{ title_base_field ? 'title' : 'id' }}",
77  *     "uuid" = "uuid"
78  *   },
79 {% if revisionable %}
80  *   revision_metadata_keys = {
81 {% if author_base_field %}
82  *     "revision_user" = "revision_uid",
83 {% endif %}
84 {% if created_base_field %}
85  *     "revision_created" = "revision_timestamp",
86 {% endif %}
87  *     "revision_log_message" = "revision_log"
88  *   },
89 {% endif %}
90  *   links = {
91 {% if bundle %}
92  *     "add-form" = "{{ entity_base_path }}/add/{{ '{' }}{{ entity_type_id }}{{ '_type}' }}",
93  *     "add-page" = "{{ entity_base_path }}/add",
94 {% else %}
95  *     "add-form" = "{{ entity_base_path }}/add",
96 {% endif %}
97  *     "canonical" = "/{{ entity_type_id }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}",
98  *     "edit-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/edit",
99  *     "delete-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/delete",
100  *     "collection" = "/admin/content/{{ entity_type_id|u2h }}"
101  *   },
102 {% if bundle %}
103  *   bundle_entity_type = "{{ entity_type_id }}_type",
104  *   field_ui_base_route = "entity.{{ entity_type_id }}_type.edit_form"
105 {% elseif fieldable %}
106  *   field_ui_base_route = "entity.{{ entity_type_id }}.settings"
107 {% endif %}
108  * )
109  */
110 class {{ class_prefix }} extends {% if revisionable %}Revisionable{% endif %}ContentEntityBase implements {{ class_prefix }}Interface {
111
112 {% if changed_base_field %}
113   use EntityChangedTrait;
114
115 {% endif %}
116   /**
117    * {@inheritdoc}
118    *
119    * When a new {{ entity_type_label|lower }} entity is created, set the uid entity reference to
120    * the current user as the creator of the entity.
121    */
122   public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
123     parent::preCreate($storage_controller, $values);
124     $values += ['uid' => \Drupal::currentUser()->id()];
125   }
126
127 {% if title_base_field %}
128   /**
129    * {@inheritdoc}
130    */
131   public function getTitle() {
132     return $this->get('title')->value;
133   }
134
135   /**
136    * {@inheritdoc}
137    */
138   public function setTitle($title) {
139     $this->set('title', $title);
140     return $this;
141   }
142
143 {% endif %}
144 {% if status_base_field %}
145   /**
146    * {@inheritdoc}
147    */
148   public function isEnabled() {
149     return (bool) $this->get('status')->value;
150   }
151
152   /**
153    * {@inheritdoc}
154    */
155   public function setStatus($status) {
156     $this->set('status', $status);
157     return $this;
158   }
159
160 {% endif %}
161 {% if created_base_field %}
162   /**
163    * {@inheritdoc}
164    */
165   public function getCreatedTime() {
166     return $this->get('created')->value;
167   }
168
169   /**
170    * {@inheritdoc}
171    */
172   public function setCreatedTime($timestamp) {
173     $this->set('created', $timestamp);
174     return $this;
175   }
176
177 {% endif %}
178 {% if author_base_field %}
179   /**
180    * {@inheritdoc}
181    */
182   public function getOwner() {
183     return $this->get('uid')->entity;
184   }
185
186   /**
187    * {@inheritdoc}
188    */
189   public function getOwnerId() {
190     return $this->get('uid')->target_id;
191   }
192
193   /**
194    * {@inheritdoc}
195    */
196   public function setOwnerId($uid) {
197     $this->set('uid', $uid);
198     return $this;
199   }
200
201   /**
202    * {@inheritdoc}
203    */
204   public function setOwner(UserInterface $account) {
205     $this->set('uid', $account->id());
206     return $this;
207   }
208
209 {% endif %}
210   /**
211    * {@inheritdoc}
212    */
213   public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
214
215     $fields = parent::baseFieldDefinitions($entity_type);
216
217 {% if title_base_field %}
218     $fields['title'] = BaseFieldDefinition::create('string')
219 {% if revisionable %}
220       ->setRevisionable(TRUE)
221 {% endif %}
222 {% if translatable %}
223       ->setTranslatable(TRUE)
224 {% endif %}
225       ->setLabel(t('Title'))
226       ->setDescription(t('The title of the {{ entity_type_label|lower }} entity.'))
227       ->setRequired(TRUE)
228       ->setSetting('max_length', 255)
229       ->setDisplayOptions('form', [
230         'type' => 'string_textfield',
231         'weight' => -5,
232       ])
233       ->setDisplayConfigurable('form', TRUE)
234       ->setDisplayOptions('view', [
235         'label' => 'hidden',
236         'type' => 'string',
237         'weight' => -5,
238       ])
239       ->setDisplayConfigurable('view', TRUE);
240
241 {% endif %}
242 {% if status_base_field %}
243     $fields['status'] = BaseFieldDefinition::create('boolean')
244 {% if revisionable %}
245       ->setRevisionable(TRUE)
246 {% endif %}
247       ->setLabel(t('Status'))
248       ->setDescription(t('A boolean indicating whether the {{ entity_type_label|lower }} is enabled.'))
249       ->setDefaultValue(TRUE)
250       ->setSetting('on_label', 'Enabled')
251       ->setDisplayOptions('form', [
252         'type' => 'boolean_checkbox',
253         'settings' => [
254           'display_label' => FALSE,
255         ],
256         'weight' => 0,
257       ])
258       ->setDisplayConfigurable('form', TRUE)
259       ->setDisplayOptions('view', [
260         'type' => 'boolean',
261         'label' => 'above',
262         'weight' => 0,
263         'settings' => [
264           'format' => 'enabled-disabled',
265         ],
266       ])
267       ->setDisplayConfigurable('view', TRUE);
268
269 {% endif %}
270 {% if description_base_field %}
271     $fields['description'] = BaseFieldDefinition::create('text_long')
272 {% if revisionable %}
273       ->setRevisionable(TRUE)
274 {% endif %}
275 {% if translatable %}
276       ->setTranslatable(TRUE)
277 {% endif %}
278       ->setLabel(t('Description'))
279       ->setDescription(t('A description of the {{ entity_type_label|lower }}.'))
280       ->setDisplayOptions('form', [
281         'type' => 'text_textarea',
282         'weight' => 10,
283       ])
284       ->setDisplayConfigurable('form', TRUE)
285       ->setDisplayOptions('view', [
286         'type' => 'text_default',
287         'label' => 'above',
288         'weight' => 10,
289       ])
290       ->setDisplayConfigurable('view', TRUE);
291
292 {% endif %}
293 {% if author_base_field %}
294     $fields['uid'] = BaseFieldDefinition::create('entity_reference')
295 {% if revisionable %}
296       ->setRevisionable(TRUE)
297 {% endif %}
298 {% if translatable %}
299       ->setTranslatable(TRUE)
300 {% endif %}
301       ->setLabel(t('Author'))
302       ->setDescription(t('The user ID of the {{ entity_type_label|lower }} author.'))
303       ->setSetting('target_type', 'user')
304       ->setDisplayOptions('form', [
305         'type' => 'entity_reference_autocomplete',
306         'settings' => [
307           'match_operator' => 'CONTAINS',
308           'size' => 60,
309           'placeholder' => '',
310         ],
311         'weight' => 15,
312       ])
313       ->setDisplayConfigurable('form', TRUE)
314       ->setDisplayOptions('view', [
315         'label' => 'above',
316         'type' => 'author',
317         'weight' => 15,
318       ])
319       ->setDisplayConfigurable('view', TRUE);
320
321 {% endif %}
322 {% if created_base_field %}
323     $fields['created'] = BaseFieldDefinition::create('created')
324       ->setLabel(t('Authored on'))
325 {% if translatable %}
326       ->setTranslatable(TRUE)
327 {% endif %}
328       ->setDescription(t('The time that the {{ entity_type_label|lower }} was created.'))
329       ->setDisplayOptions('view', [
330         'label' => 'above',
331         'type' => 'timestamp',
332         'weight' => 20,
333       ])
334       ->setDisplayConfigurable('form', TRUE)
335       ->setDisplayOptions('form', [
336         'type' => 'datetime_timestamp',
337         'weight' => 20,
338       ])
339       ->setDisplayConfigurable('view', TRUE);
340
341 {% endif %}
342 {% if changed_base_field %}
343     $fields['changed'] = BaseFieldDefinition::create('changed')
344       ->setLabel(t('Changed'))
345 {% if translatable %}
346       ->setTranslatable(TRUE)
347 {% endif %}
348       ->setDescription(t('The time that the {{ entity_type_label|lower }} was last edited.'));
349
350 {% endif %}
351     return $fields;
352   }
353
354 }