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