f3ada1f6dfb808e0a86879181a1cdbbd17f4477e
[yaffs-website] / web / core / modules / system / tests / modules / entity_test / src / Entity / EntityTest.php
1 <?php
2
3 namespace Drupal\entity_test\Entity;
4
5 use Drupal\Core\Entity\ContentEntityBase;
6 use Drupal\Core\Entity\EntityTypeInterface;
7 use Drupal\Core\Field\BaseFieldDefinition;
8 use Drupal\Core\Entity\EntityStorageInterface;
9 use Drupal\user\EntityOwnerInterface;
10 use Drupal\user\UserInterface;
11
12 /**
13  * Defines the test entity class.
14  *
15  * @ContentEntityType(
16  *   id = "entity_test",
17  *   label = @Translation("Test entity"),
18  *   handlers = {
19  *     "list_builder" = "Drupal\entity_test\EntityTestListBuilder",
20  *     "view_builder" = "Drupal\entity_test\EntityTestViewBuilder",
21  *     "access" = "Drupal\entity_test\EntityTestAccessControlHandler",
22  *     "form" = {
23  *       "default" = "Drupal\entity_test\EntityTestForm",
24  *       "delete" = "Drupal\entity_test\EntityTestDeleteForm"
25  *     },
26  *     "route_provider" = {
27  *       "html" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
28  *     },
29  *     "translation" = "Drupal\content_translation\ContentTranslationHandler",
30  *     "views_data" = "Drupal\entity_test\EntityTestViewsData"
31  *   },
32  *   base_table = "entity_test",
33  *   admin_permission = "administer entity_test content",
34  *   persistent_cache = FALSE,
35  *   list_cache_contexts = { "entity_test_view_grants" },
36  *   entity_keys = {
37  *     "id" = "id",
38  *     "uuid" = "uuid",
39  *     "bundle" = "type",
40  *     "label" = "name",
41  *     "langcode" = "langcode",
42  *   },
43  *   links = {
44  *     "canonical" = "/entity_test/{entity_test}",
45  *     "add-form" = "/entity_test/add",
46  *     "edit-form" = "/entity_test/manage/{entity_test}/edit",
47  *     "delete-form" = "/entity_test/delete/entity_test/{entity_test}",
48  *   },
49  *   field_ui_base_route = "entity.entity_test.admin_form",
50  * )
51  */
52 class EntityTest extends ContentEntityBase implements EntityOwnerInterface {
53
54   /**
55    * {@inheritdoc}
56    */
57   public static function preCreate(EntityStorageInterface $storage, array &$values) {
58     parent::preCreate($storage, $values);
59     if (empty($values['type'])) {
60       $values['type'] = $storage->getEntityTypeId();
61     }
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
68     $fields = parent::baseFieldDefinitions($entity_type);
69
70     $fields['name'] = BaseFieldDefinition::create('string')
71       ->setLabel(t('Name'))
72       ->setDescription(t('The name of the test entity.'))
73       ->setTranslatable(TRUE)
74       ->setSetting('max_length', 32)
75       ->setDisplayOptions('view', [
76         'label' => 'hidden',
77         'type' => 'string',
78         'weight' => -5,
79       ])
80       ->setDisplayOptions('form', [
81         'type' => 'string_textfield',
82         'weight' => -5,
83       ]);
84
85     $fields['created'] = BaseFieldDefinition::create('created')
86       ->setLabel(t('Authored on'))
87       ->setDescription(t('Time the entity was created'))
88       ->setTranslatable(TRUE);
89
90     $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
91       ->setLabel(t('User ID'))
92       ->setDescription(t('The ID of the associated user.'))
93       ->setSetting('target_type', 'user')
94       ->setSetting('handler', 'default')
95       // Default EntityTest entities to have the root user as the owner, to
96       // simplify testing.
97       ->setDefaultValue([0 => ['target_id' => 1]])
98       ->setTranslatable(TRUE)
99       ->setDisplayOptions('form', [
100         'type' => 'entity_reference_autocomplete',
101         'weight' => -1,
102         'settings' => [
103           'match_operator' => 'CONTAINS',
104           'size' => '60',
105           'placeholder' => '',
106         ],
107       ]);
108
109     return $fields;
110   }
111
112   /**
113    * {@inheritdoc}
114    */
115   public function getOwner() {
116     return $this->get('user_id')->entity;
117   }
118
119   /**
120    * {@inheritdoc}
121    */
122   public function getOwnerId() {
123     return $this->get('user_id')->target_id;
124   }
125
126   /**
127    * {@inheritdoc}
128    */
129   public function setOwnerId($uid) {
130     $this->set('user_id', $uid);
131     return $this;
132   }
133
134   /**
135    * {@inheritdoc}
136    */
137   public function setOwner(UserInterface $account) {
138     $this->set('user_id', $account->id());
139     return $this;
140   }
141
142   /**
143    * Sets the name.
144    *
145    * @param string $name
146    *   Name of the entity.
147    *
148    * @return $this
149    */
150   public function setName($name) {
151     $this->set('name', $name);
152     return $this;
153   }
154
155   /**
156    * Returns the name.
157    *
158    * @return string
159    */
160   public function getName() {
161     return $this->get('name')->value;
162   }
163
164 }