1 {% extends "base/class.php.twig" %}
4 \Drupal\{{ module }}\Entity\{{ entity_class }}.
7 {% block namespace_class %}
8 namespace Drupal\{{ module }}\Entity;
12 use Drupal\Core\Entity\EntityStorageInterface;
13 use Drupal\Core\Field\BaseFieldDefinition;
15 use Drupal\Core\Entity\RevisionableContentEntityBase;
17 use Drupal\Core\Entity\ContentEntityBase;
19 use Drupal\Core\Entity\EntityChangedTrait;
20 use Drupal\Core\Entity\EntityTypeInterface;
21 use Drupal\user\UserInterface;
24 {% block class_declaration %}
26 * Defines the {{ label }} entity.
28 * @ingroup {{ module }}
31 * id = "{{ entity_name }}",
32 * label = @Translation("{{ label }}"),
33 {% if bundle_entity_type %}
34 * bundle_label = @Translation("{{ label }} type"),
38 * "storage" = "Drupal\{{ module }}\{{ entity_class }}Storage",
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",
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",
53 * "access" = "Drupal\{{ module }}\{{ entity_class }}AccessControlHandler",
54 * "route_provider" = {
55 * "html" = "Drupal\{{ module }}\{{ entity_class }}HtmlRouteProvider",
58 * base_table = "{{ entity_name }}",
59 {% if is_translatable %}
60 * data_table = "{{ entity_name }}_field_data",
63 * revision_table = "{{ entity_name }}_revision",
64 * revision_data_table = "{{ entity_name }}_field_revision",
66 {% if is_translatable %}
67 * translatable = TRUE,
69 * admin_permission = "administer {{ label|lower }} entities",
75 {% if bundle_entity_type %}
81 * "langcode" = "langcode",
82 * "status" = "status",
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~'}' }}",
90 * "add-form" = "{{ base_path }}/{{ entity_name }}/add",
92 * "edit-form" = "{{ base_path }}/{{ entity_name }}/{{ '{'~entity_name~'}' }}/edit",
93 * "delete-form" = "{{ base_path }}/{{ entity_name }}/{{ '{'~entity_name~'}' }}/delete",
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}",
101 * "revision_delete" = "{{ base_path }}/{{ entity_name }}/{{ '{'~entity_name~'}' }}/revisions/{{ '{'~entity_name~'_revision}' }}/delete",
103 * "collection" = "{{ base_path }}/{{ entity_name }}",
105 {% if bundle_entity_type %}
106 * bundle_entity_type = "{{ bundle_entity_type }}",
107 * field_ui_base_route = "entity.{{ bundle_entity_type }}.edit_form"
109 * field_ui_base_route = "{{ entity_name }}.settings"
113 class {{ entity_class }} extends {% if revisionable %}RevisionableContentEntityBase{% else %}ContentEntityBase{% endif %} implements {{ entity_class }}Interface {% endblock %}
115 {% block use_trait %}
116 use EntityChangedTrait;
119 {% block class_methods %}
124 public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
125 parent::preCreate($storage_controller, $values);
127 'user_id' => \Drupal::currentUser()->id(),
130 {% if revisionable %}
135 public function preSave(EntityStorageInterface $storage) {
136 parent::preSave($storage);
138 foreach (array_keys($this->getTranslationLanguages()) as $langcode) {
139 $translation = $this->getTranslation($langcode);
141 // If no owner has been set explicitly, make the anonymous user the owner.
142 if (!$translation->getOwner()) {
143 $translation->setOwnerId(0);
147 // If no revision author has been set explicitly, make the {{ entity_name }} owner the
149 if (!$this->getRevisionUser()) {
150 $this->setRevisionUserId($this->getOwnerId());
158 public function getName() {
159 return $this->get('name')->value;
165 public function setName($name) {
166 $this->set('name', $name);
173 public function getCreatedTime() {
174 return $this->get('created')->value;
180 public function setCreatedTime($timestamp) {
181 $this->set('created', $timestamp);
188 public function getOwner() {
189 return $this->get('user_id')->entity;
195 public function getOwnerId() {
196 return $this->get('user_id')->target_id;
202 public function setOwnerId($uid) {
203 $this->set('user_id', $uid);
210 public function setOwner(UserInterface $account) {
211 $this->set('user_id', $account->id());
218 public function isPublished() {
219 return (bool) $this->getEntityKey('status');
225 public function setPublished($published) {
226 $this->set('status', $published ? TRUE : FALSE);
233 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
234 $fields = parent::baseFieldDefinitions($entity_type);
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', [
248 ->setDisplayOptions('form', [
249 'type' => 'entity_reference_autocomplete',
252 'match_operator' => 'CONTAINS',
254 'autocomplete_type' => 'tags',
258 ->setDisplayConfigurable('form', TRUE)
259 ->setDisplayConfigurable('view', TRUE);
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)
269 'text_processing' => 0,
271 ->setDefaultValue('')
272 ->setDisplayOptions('view', [
277 ->setDisplayOptions('form', [
278 'type' => 'string_textfield',
281 ->setDisplayConfigurable('form', TRUE)
282 ->setDisplayConfigurable('view', TRUE);
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)
290 ->setDefaultValue(TRUE);
292 $fields['created'] = BaseFieldDefinition::create('created')
293 ->setLabel(t('Created'))
294 ->setDescription(t('The time that the entity was created.'));
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 %}
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.'))
305 ->setRevisionable(TRUE)
306 ->setTranslatable(TRUE);