layout_paragraphs-1.0.x-dev/tests/modules/layout_paragraphs_custom_host_entity_test/src/Entity/LpHostEntity.php
tests/modules/layout_paragraphs_custom_host_entity_test/src/Entity/LpHostEntity.php
<?php
namespace Drupal\layout_paragraphs_custom_host_entity_test\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\layout_paragraphs_custom_host_entity_test\LpHostEntityInterface;
use Drupal\user\UserInterface;
/**
* Defines the lp host entity entity class.
*
* @ContentEntityType(
* id = "lp_host_entity",
* label = @Translation("LP Host Entity"),
* label_collection = @Translation("LP Host Entities"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\layout_paragraphs_custom_host_entity_test\LpHostEntityListBuilder",
* "views_data" = "Drupal\views\EntityViewsData",
* "form" = {
* "add" = "Drupal\layout_paragraphs_custom_host_entity_test\Form\LpHostEntityForm",
* "edit" = "Drupal\layout_paragraphs_custom_host_entity_test\Form\LpHostEntityForm",
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm"
* },
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
* }
* },
* base_table = "lp_host_entity",
* admin_permission = "administer lp host entity",
* entity_keys = {
* "id" = "id",
* "label" = "title",
* "uuid" = "uuid"
* },
* links = {
* "add-form" = "/admin/content/lp-host-entity/add",
* "canonical" = "/lp_host_entity/{lp_host_entity}",
* "edit-form" = "/admin/content/lp-host-entity/{lp_host_entity}/edit",
* "delete-form" = "/admin/content/lp-host-entity/{lp_host_entity}/delete",
* "collection" = "/admin/content/lp-host-entity"
* },
* field_ui_base_route = "entity.lp_host_entity.settings"
* )
*/
class LpHostEntity extends ContentEntityBase implements LpHostEntityInterface {
use EntityChangedTrait;
/**
* {@inheritdoc}
*
* Set the uid entity reference to the current user.
*/
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += ['uid' => \Drupal::currentUser()->id()];
}
/**
* {@inheritdoc}
*/
public function getTitle() {
return $this->get('title')->value;
}
/**
* {@inheritdoc}
*/
public function setTitle($title) {
$this->set('title', $title);
return $this;
}
/**
* {@inheritdoc}
*/
public function isEnabled() {
return (bool) $this->get('status')->value;
}
/**
* {@inheritdoc}
*/
public function setStatus($status) {
$this->set('status', $status);
return $this;
}
/**
* {@inheritdoc}
*/
public function getCreatedTime() {
return $this->get('created')->value;
}
/**
* {@inheritdoc}
*/
public function setCreatedTime($timestamp) {
$this->set('created', $timestamp);
return $this;
}
/**
* {@inheritdoc}
*/
public function getOwner() {
return $this->get('uid')->entity;
}
/**
* {@inheritdoc}
*/
public function getOwnerId() {
return $this->get('uid')->target_id;
}
/**
* {@inheritdoc}
*/
public function setOwnerId($uid) {
$this->set('uid', $uid);
return $this;
}
/**
* {@inheritdoc}
*/
public function setOwner(UserInterface $account) {
$this->set('uid', $account->id());
return $this;
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
->setDescription(t('The title of the lp host entity entity.'))
->setRequired(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
])
->setDisplayConfigurable('view', TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Status'))
->setDescription(t('A boolean indicating whether the lp host entity is enabled.'))
->setDefaultValue(TRUE)
->setSetting('on_label', 'Enabled')
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => FALSE,
],
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'type' => 'boolean',
'label' => 'above',
'weight' => 0,
'settings' => [
'format' => 'enabled-disabled',
],
])
->setDisplayConfigurable('view', TRUE);
$fields['description'] = BaseFieldDefinition::create('text_long')
->setLabel(t('Description'))
->setDescription(t('A description of the lp host entity.'))
->setDisplayOptions('form', [
'type' => 'text_textarea',
'weight' => 10,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'type' => 'text_default',
'label' => 'above',
'weight' => 10,
])
->setDisplayConfigurable('view', TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Author'))
->setDescription(t('The user ID of the lp host entity author.'))
->setSetting('target_type', 'user')
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'settings' => [
'match_operator' => 'CONTAINS',
'size' => 60,
'placeholder' => '',
],
'weight' => 15,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'author',
'weight' => 15,
])
->setDisplayConfigurable('view', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Authored on'))
->setDescription(t('The time that the lp host entity was created.'))
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'timestamp',
'weight' => 20,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'type' => 'datetime_timestamp',
'weight' => 20,
])
->setDisplayConfigurable('view', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the lp host entity was last edited.'));
return $fields;
}
}
