htools-8.x-1.x-dev/modules/htools_relations/src/Entity/RelationalEntity.php
modules/htools_relations/src/Entity/RelationalEntity.php
<?php
namespace Drupal\htools_relations\Entity;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityPublishedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\EntityOwnerInterface;
use Drupal\user\EntityOwnerTrait;
/**
* Defines the Relational entity entity.
*
* @ingroup htools_relations
*
* @ContentEntityType(
* id = "relational_entity",
* label = @Translation("Relational entity"),
* bundle_label = @Translation("Relational entity type"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\htools_relations\RelationalEntityListBuilder",
* "views_data" = "Drupal\htools_relations\Entity\RelationalEntityViewsData",
*
* "form" = {
* "default" = "Drupal\htools_relations\Form\RelationalEntityForm",
* "add" = "Drupal\htools_relations\Form\RelationalEntityForm",
* "edit" = "Drupal\htools_relations\Form\RelationalEntityForm",
* "delete" = "Drupal\htools_relations\Form\RelationalEntityDeleteForm",
* },
* "route_provider" = {
* "html" = "Drupal\htools_relations\RelationalEntityHtmlRouteProvider",
* },
* "access" = "Drupal\htools_relations\RelationalEntityAccessControlHandler",
* },
* base_table = "relational_entity",
* translatable = FALSE,
* admin_permission = "administer relational entity entities",
* entity_keys = {
* "id" = "id",
* "bundle" = "type",
* "label" = "name",
* "uuid" = "uuid",
* "uid" = "uid",
* "owner" = "uid",
* "langcode" = "langcode",
* "published" = "status",
* },
* bundle_entity_type = "relational_entity_type",
* field_ui_base_route = "entity.relational_entity_type.edit_form"
* )
*/
class RelationalEntity extends ContentEntityBase implements RelationalEntityInterface, EntityOwnerInterface {
use EntityChangedTrait;
use EntityPublishedTrait;
use EntityOwnerTrait;
/**
* {@inheritdoc}
*/
public function getName() {
return $this->get('name')->value;
}
/**
* {@inheritdoc}
*/
public function setName($name) {
$this->set('name', $name);
return $this;
}
/**
* {@inheritdoc}
*/
public function getCreatedTime() {
return $this->get('created')->value;
}
/**
* {@inheritdoc}
*/
public function setCreatedTime($timestamp) {
$this->set('created', $timestamp);
return $this;
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
// Add the published field.
$fields += static::publishedBaseFieldDefinitions($entity_type);
$fields += static::ownerBaseFieldDefinitions($entity_type);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the Relational entity entity.'))
->setSettings([
'max_length' => 50,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -4,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setRequired(TRUE);
$fields['uid']
->setLabel(t('Authored by'))
->setDescription(t('The username of the content author.'))
->setRevisionable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the entity was created.'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'));
return $fields;
}
}
