Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / templates / module / src / Entity / entity-content.php.twig
1 {% extends "base/class.php.twig" %}
2
3 {% block file_path %}
4 \Drupal\{{ module }}\Entity\{{ entity_class }}.
5 {% endblock %}
6
7 {% block namespace_class %}
8 namespace Drupal\{{ module }}\Entity;
9 {% endblock %}
10
11 {% block use_class %}
12 use Drupal\Core\Entity\EntityStorageInterface;
13 use Drupal\Core\Field\BaseFieldDefinition;
14 {% if revisionable %}
15 use Drupal\Core\Entity\RevisionableContentEntityBase;
16 {% else %}
17 use Drupal\Core\Entity\ContentEntityBase;
18 {% endif %}
19 use Drupal\Core\Entity\EntityChangedTrait;
20 use Drupal\Core\Entity\EntityTypeInterface;
21 use Drupal\user\UserInterface;
22 {% endblock %}
23
24 {% block class_declaration %}
25 /**
26  * Defines the {{ label }} entity.
27  *
28  * @ingroup {{ module }}
29  *
30  * @ContentEntityType(
31  *   id = "{{ entity_name }}",
32  *   label = @Translation("{{ label }}"),
33 {% if bundle_entity_type %}
34  *   bundle_label = @Translation("{{ label }} type"),
35 {% endif %}
36  *   handlers = {
37 {% if revisionable %}
38  *     "storage" = "Drupal\{{ module }}\{{ entity_class }}Storage",
39 {% endif %}
40  *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
41  *     "list_builder" = "Drupal\{{ module }}\{{ entity_class }}ListBuilder",
42  *     "views_data" = "Drupal\{{ module }}\Entity\{{ entity_class }}ViewsData",
43 {% if is_translatable %}
44  *     "translation" = "Drupal\{{ module }}\{{ entity_class }}TranslationHandler",
45 {% endif %}
46  *
47  *     "form" = {
48  *       "default" = "Drupal\{{ module }}\Form\{{ entity_class }}Form",
49  *       "add" = "Drupal\{{ module }}\Form\{{ entity_class }}Form",
50  *       "edit" = "Drupal\{{ module }}\Form\{{ entity_class }}Form",
51  *       "delete" = "Drupal\{{ module }}\Form\{{ entity_class }}DeleteForm",
52  *     },
53  *     "access" = "Drupal\{{ module }}\{{ entity_class }}AccessControlHandler",
54  *     "route_provider" = {
55  *       "html" = "Drupal\{{ module }}\{{ entity_class }}HtmlRouteProvider",
56  *     },
57  *   },
58  *   base_table = "{{ entity_name }}",
59 {% if is_translatable %}
60  *   data_table = "{{ entity_name }}_field_data",
61 {% endif %}
62 {% if revisionable %}
63  *   revision_table = "{{ entity_name }}_revision",
64  *   revision_data_table = "{{ entity_name }}_field_revision",
65 {% endif %}
66 {% if is_translatable %}
67  *   translatable = TRUE,
68 {% endif %}
69  *   admin_permission = "administer {{ label|lower }} entities",
70  *   entity_keys = {
71  *     "id" = "id",
72 {% if revisionable %}
73  *     "revision" = "vid",
74 {% endif %}
75 {% if bundle_entity_type %}
76  *     "bundle" = "type",
77 {% endif %}
78  *     "label" = "name",
79  *     "uuid" = "uuid",
80  *     "uid" = "user_id",
81  *     "langcode" = "langcode",
82  *     "status" = "status",
83  *   },
84  *   links = {
85  *     "canonical" = "{{ base_path }}/{{ entity_name }}/{{ '{'~entity_name~'}' }}",
86 {% if bundle_entity_type %}
87  *     "add-page" = "{{ base_path }}/{{ entity_name }}/add",
88  *     "add-form" = "{{ base_path }}/{{ entity_name }}/add/{{ '{'~bundle_entity_type~'}' }}",
89 {% else %}
90  *     "add-form" = "{{ base_path }}/{{ entity_name }}/add",
91 {% endif %}
92  *     "edit-form" = "{{ base_path }}/{{ entity_name }}/{{ '{'~entity_name~'}' }}/edit",
93  *     "delete-form" = "{{ base_path }}/{{ entity_name }}/{{ '{'~entity_name~'}' }}/delete",
94 {% if revisionable %}
95  *     "version-history" = "{{ base_path }}/{{ entity_name }}/{{ '{'~entity_name~'}' }}/revisions",
96  *     "revision" = "{{ base_path }}/{{ entity_name }}/{{ '{'~entity_name~'}' }}/revisions/{{ '{'~entity_name~'_revision}' }}/view",
97 {% if is_translatable %}
98  *     "revision_revert" = "{{ base_path }}/{{ entity_name }}/{{ '{'~entity_name~'}' }}/revisions/{{ '{'~entity_name~'_revision}' }}/revert",
99  *     "translation_revert" = "{{ base_path }}/{{ entity_name }}/{{ '{'~entity_name~'}' }}/revisions/{{ '{'~entity_name~'_revision}' }}/revert/{langcode}",
100 {% endif %}
101  *     "revision_delete" = "{{ base_path }}/{{ entity_name }}/{{ '{'~entity_name~'}' }}/revisions/{{ '{'~entity_name~'_revision}' }}/delete",
102 {% endif %}
103  *     "collection" = "{{ base_path }}/{{ entity_name }}",
104  *   },
105 {% if bundle_entity_type %}
106  *   bundle_entity_type = "{{ bundle_entity_type }}",
107  *   field_ui_base_route = "entity.{{ bundle_entity_type }}.edit_form"
108 {% else %}
109  *   field_ui_base_route = "{{ entity_name }}.settings"
110 {% endif %}
111  * )
112  */
113 class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityBase{% else %}ContentEntityBase{% endif %} implements {{ entity_class }}Interface {% endblock %}
114
115 {% block use_trait %}
116   use EntityChangedTrait;
117 {% endblock %}
118
119 {% block class_methods %}
120
121   /**
122    * {@inheritdoc}
123    */
124   public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
125     parent::preCreate($storage_controller, $values);
126     $values += [
127       'user_id' => \Drupal::currentUser()->id(),
128     ];
129   }
130 {% if revisionable %}
131
132   /**
133    * {@inheritdoc}
134    */
135   public function preSave(EntityStorageInterface $storage) {
136     parent::preSave($storage);
137
138     foreach (array_keys($this->getTranslationLanguages()) as $langcode) {
139       $translation = $this->getTranslation($langcode);
140
141       // If no owner has been set explicitly, make the anonymous user the owner.
142       if (!$translation->getOwner()) {
143         $translation->setOwnerId(0);
144       }
145     }
146
147     // If no revision author has been set explicitly, make the {{ entity_name }} owner the
148     // revision author.
149     if (!$this->getRevisionUser()) {
150       $this->setRevisionUserId($this->getOwnerId());
151     }
152   }
153 {% endif %}
154
155   /**
156    * {@inheritdoc}
157    */
158   public function getName() {
159     return $this->get('name')->value;
160   }
161
162   /**
163    * {@inheritdoc}
164    */
165   public function setName($name) {
166     $this->set('name', $name);
167     return $this;
168   }
169
170   /**
171    * {@inheritdoc}
172    */
173   public function getCreatedTime() {
174     return $this->get('created')->value;
175   }
176
177   /**
178    * {@inheritdoc}
179    */
180   public function setCreatedTime($timestamp) {
181     $this->set('created', $timestamp);
182     return $this;
183   }
184
185   /**
186    * {@inheritdoc}
187    */
188   public function getOwner() {
189     return $this->get('user_id')->entity;
190   }
191
192   /**
193    * {@inheritdoc}
194    */
195   public function getOwnerId() {
196     return $this->get('user_id')->target_id;
197   }
198
199   /**
200    * {@inheritdoc}
201    */
202   public function setOwnerId($uid) {
203     $this->set('user_id', $uid);
204     return $this;
205   }
206
207   /**
208    * {@inheritdoc}
209    */
210   public function setOwner(UserInterface $account) {
211     $this->set('user_id', $account->id());
212     return $this;
213   }
214
215   /**
216    * {@inheritdoc}
217    */
218   public function isPublished() {
219     return (bool) $this->getEntityKey('status');
220   }
221
222   /**
223    * {@inheritdoc}
224    */
225   public function setPublished($published) {
226     $this->set('status', $published ? TRUE : FALSE);
227     return $this;
228   }
229
230   /**
231    * {@inheritdoc}
232    */
233   public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
234     $fields = parent::baseFieldDefinitions($entity_type);
235
236     $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
237       ->setLabel(t('Authored by'))
238       ->setDescription(t('The user ID of author of the {{ label }} entity.'))
239       ->setRevisionable(TRUE)
240       ->setSetting('target_type', 'user')
241       ->setSetting('handler', 'default')
242       ->setTranslatable(TRUE)
243       ->setDisplayOptions('view', [
244         'label' => 'hidden',
245         'type' => 'author',
246         'weight' => 0,
247       ])
248       ->setDisplayOptions('form', [
249         'type' => 'entity_reference_autocomplete',
250         'weight' => 5,
251         'settings' => [
252           'match_operator' => 'CONTAINS',
253           'size' => '60',
254           'autocomplete_type' => 'tags',
255           'placeholder' => '',
256         ],
257       ])
258       ->setDisplayConfigurable('form', TRUE)
259       ->setDisplayConfigurable('view', TRUE);
260
261     $fields['name'] = BaseFieldDefinition::create('string')
262       ->setLabel(t('Name'))
263       ->setDescription(t('The name of the {{ label }} entity.'))
264 {% if revisionable %}
265       ->setRevisionable(TRUE)
266 {% endif %}
267       ->setSettings([
268         'max_length' => 50,
269         'text_processing' => 0,
270       ])
271       ->setDefaultValue('')
272       ->setDisplayOptions('view', [
273         'label' => 'above',
274         'type' => 'string',
275         'weight' => -4,
276       ])
277       ->setDisplayOptions('form', [
278         'type' => 'string_textfield',
279         'weight' => -4,
280       ])
281       ->setDisplayConfigurable('form', TRUE)
282       ->setDisplayConfigurable('view', TRUE);
283
284     $fields['status'] = BaseFieldDefinition::create('boolean')
285       ->setLabel(t('Publishing status'))
286       ->setDescription(t('A boolean indicating whether the {{ label }} is published.'))
287 {% if revisionable %}
288       ->setRevisionable(TRUE)
289 {% endif %}
290       ->setDefaultValue(TRUE);
291
292     $fields['created'] = BaseFieldDefinition::create('created')
293       ->setLabel(t('Created'))
294       ->setDescription(t('The time that the entity was created.'));
295
296     $fields['changed'] = BaseFieldDefinition::create('changed')
297       ->setLabel(t('Changed'))
298       ->setDescription(t('The time that the entity was last edited.'));
299 {% if revisionable and is_translatable %}
300
301     $fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean')
302       ->setLabel(t('Revision translation affected'))
303       ->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))
304       ->setReadOnly(TRUE)
305       ->setRevisionable(TRUE)
306       ->setTranslatable(TRUE);
307 {% endif %}
308
309     return $fields;
310   }
311 {% endblock %}