CertificateTestEntity.php in Certificate 4.x
Namespace
Drupal\certificate_test\EntityFile
modules/certificate_test/src/Entity/CertificateTestEntity.phpView source
<?php
namespace Drupal\certificate_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;
/**
* Defines the test entity class.
*
* @ContentEntityType(
* id = "certificate_test_entity",
* label = @Translation("Certificate Test entity"),
* persistent_cache = FALSE,
* base_table = "certificate_test_entity",
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid",
* "label" = "name",
* },
* fieldable = TRUE,
* field_ui_base_route = "entity.certificate_test_entity.collection",
* admin_permission = "administer certificate",
* handlers = {
* "permission_provider" = "Drupal\entity\EntityPermissionProvider",
* "access" = "Drupal\entity\EntityAccessControlHandler",
* "list_builder" = "Drupal\Core\Entity\EntityListBuilder",
* "local_task_provider" = {
* "default" = "\Drupal\entity\Menu\DefaultEntityLocalTaskProvider",
* },
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
* },
* "form" = {
* "default" = "Drupal\Core\Entity\ContentEntityForm",
* },
* },
*
* links = {
* "collection" = "/certificate_test_entity",
* "canonical" = "/certificate_test_entity/{certificate_test_entity}",
* "add-form" = "/certificate_test_entity/add",
* "delete-form" = "/certificate_test_entity/{certificate_test_entity}/delete",
* "edit-form" = "/certificate_test_entity/{certificate_test_entity}/edit",
* },
* )
*/
class CertificateTestEntity extends ContentEntityBase implements EntityOwnerInterface {
/**
* {@inheritdoc}
*/
public static function preCreate(EntityStorageInterface $storage, array &$values) {
parent::preCreate($storage, $values);
if (empty($values['type'])) {
$values['type'] = $storage
->getEntityTypeId();
}
}
/**
* {@inheritdoc}
*/
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', 32)
->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);
return $fields;
}
/**
* {@inheritdoc}
*/
public function getOwner() {
return $this
->get('user_id')->entity;
}
/**
* {@inheritdoc}
*/
public function getOwnerId() {
return $this
->get('user_id')->target_id;
}
/**
* {@inheritdoc}
*/
public function setOwnerId($uid) {
$this
->set('user_id', $uid);
return $this;
}
/**
* {@inheritdoc}
*/
public function setOwner(UserInterface $account) {
$this
->set('user_id', $account
->id());
return $this;
}
/**
* Sets the name.
*
* @param string $name
* Name of the entity.
*
* @return $this
*/
public function setName($name) {
$this
->set('name', $name);
return $this;
}
/**
* Returns the name.
*
* @return string
*/
public function getName() {
return $this
->get('name')->value;
}
/**
* {@inheritdoc}
*/
public function getEntityKey($key) {
// Typically this protected method is used internally by entity classes and
// exposed publicly through more specific getter methods. So that test cases
// are able to set and access entity keys dynamically, update the visibility
// of this method to public.
return parent::getEntityKey($key);
}
}
Classes
Name | Description |
---|---|
CertificateTestEntity | Defines the test entity class. |