Same filename in this branch
- 10 core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php
- 10 core/modules/views/tests/src/Unit/Plugin/area/EntityTest.php
- 10 core/modules/views/tests/src/Unit/Plugin/argument_validator/EntityTest.php
Same filename and directory in other branches
- 8.9.x core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php
- 9 core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php
Namespace
Drupal\entity_test\Entity
File
core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php
View source
<?php
namespace Drupal\entity_test\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\user\EntityOwnerInterface;
use Drupal\user\UserInterface;
class EntityTest extends ContentEntityBase implements EntityOwnerInterface {
public static function preCreate(EntityStorageInterface $storage, array &$values) {
parent::preCreate($storage, $values);
if (empty($values['type'])) {
$values['type'] = $storage
->getEntityTypeId();
}
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the test entity.'))
->setTranslatable(TRUE)
->setSetting('max_length', 64)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
]);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Authored on'))
->setDescription(t('Time the entity was created'))
->setTranslatable(TRUE);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('User ID'))
->setDescription(t('The ID of the associated user.'))
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValue([
0 => [
'target_id' => 1,
],
])
->setTranslatable(TRUE)
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => -1,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'placeholder' => '',
],
]);
return $fields + \Drupal::state()
->get($entity_type
->id() . '.additional_base_field_definitions', []);
}
public function getOwner() {
return $this
->get('user_id')->entity;
}
public function getOwnerId() {
return $this
->get('user_id')->target_id;
}
public function setOwnerId($uid) {
$this
->set('user_id', $uid);
return $this;
}
public function setOwner(UserInterface $account) {
$this
->set('user_id', $account
->id());
return $this;
}
public function setName($name) {
$this
->set('name', $name);
return $this;
}
public function getName() {
return $this
->get('name')->value;
}
public function getEntityKey($key) {
return parent::getEntityKey($key);
}
}
Classes