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