Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
* Contains \Drupal\entity_test\Entity\EntityTest.
Dries Buytaert
committed
*/
namespace Drupal\entity_test\Entity;
Dries Buytaert
committed
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
Alex Pott
committed
use Drupal\Core\Field\BaseFieldDefinition;
catch
committed
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\user\EntityOwnerInterface;
use Drupal\user\UserInterface;
Dries Buytaert
committed
/**
* Defines the test entity class.
Angie Byron
committed
*
Alex Pott
committed
* @ContentEntityType(
Angie Byron
committed
* id = "entity_test",
* label = @Translation("Test entity"),
Alex Pott
committed
* handlers = {
* "list_builder" = "Drupal\entity_test\EntityTestListBuilder",
Alex Pott
committed
* "view_builder" = "Drupal\entity_test\EntityTestViewBuilder",
* "access" = "Drupal\entity_test\EntityTestAccessControlHandler",
Alex Pott
committed
* "default" = "Drupal\entity_test\EntityTestForm",
* "delete" = "Drupal\entity_test\EntityTestDeleteForm"
Alex Pott
committed
* "translation" = "Drupal\content_translation\ContentTranslationHandler",
* "views_data" = "Drupal\views\EntityViewsData"
Angie Byron
committed
* },
* base_table = "entity_test",
* persistent_cache = FALSE,
Angie Byron
committed
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid",
* "bundle" = "type",
* "label" = "name",
* "langcode" = "langcode",
Angie Byron
committed
* },
Alex Pott
committed
* links = {
Angie Byron
committed
* "canonical" = "entity.entity_test.canonical",
* "edit-form" = "entity.entity_test.edit_form",
* "delete-form" = "entity.entity_test.delete_form",
Alex Pott
committed
* },
Angie Byron
committed
* field_ui_base_route = "entity.entity_test.admin_form",
Angie Byron
committed
* )
Dries Buytaert
committed
*/
class EntityTest extends ContentEntityBase implements EntityOwnerInterface {
Dries Buytaert
committed
Alex Pott
committed
/**
* {@inheritdoc}
*/
catch
committed
public static function preCreate(EntityStorageInterface $storage, array &$values) {
parent::preCreate($storage, $values);
Alex Pott
committed
if (empty($values['type'])) {
catch
committed
$values['type'] = $storage->getEntityTypeId();
Alex Pott
committed
}
}
Alex Pott
committed
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
Alex Pott
committed
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the test entity.'))
catch
committed
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
Alex Pott
committed
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the test entity.'))
->setReadOnly(TRUE);
Alex Pott
committed
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The language code of the test entity.'));
Alex Pott
committed
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the test entity.'))
->setTranslatable(TRUE)
->setSetting('max_length', 32)
->setDisplayOptions('view', array(
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
Alex Pott
committed
))
->setDisplayOptions('form', array(
'type' => 'string_textfield',
Alex Pott
committed
'weight' => -5,
));
// @todo: Add allowed values validation.
Alex Pott
committed
$fields['type'] = BaseFieldDefinition::create('string')
->setLabel(t('Type'))
->setDescription(t('The bundle of the test entity.'))
->setRequired(TRUE);
Alex Pott
committed
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('User ID'))
->setDescription(t('The ID of the associated user.'))
Alex Pott
committed
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
// Default EntityTest entities to have the root user as the owner, to
// simplify testing.
Alex Pott
committed
->setDefaultValue(array(0 => array('target_id' => 1)))
Alex Pott
committed
->setTranslatable(TRUE)
->setDisplayOptions('form', array(
'type' => 'entity_reference_autocomplete',
'weight' => -1,
'settings' => array(
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
),
));
Alex Pott
committed
return $fields;
}
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* {@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;
}