123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- <?php
- namespace Drupal\heartbeat8\Entity;
- use Drupal\Core\Entity\EntityStorageInterface;
- use Drupal\Core\Field\BaseFieldDefinition;
- use Drupal\Core\Entity\RevisionableContentEntityBase;
- use Drupal\Core\Entity\EntityChangedTrait;
- use Drupal\Core\Entity\EntityTypeInterface;
- use Drupal\user\UserInterface;
- class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterface {
- use EntityChangedTrait;
-
- public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
- parent::preCreate($storage_controller, $values);
- $values += array(
- 'user_id' => \Drupal::currentUser()->id(),
- );
- }
-
- public function preSave(EntityStorageInterface $storage) {
- parent::preSave($storage);
- foreach (array_keys($this->getTranslationLanguages()) as $langcode) {
- $translation = $this->getTranslation($langcode);
-
- if (!$translation->getOwner()) {
- $translation->setOwnerId(0);
- }
- }
-
-
- if (!$this->getRevisionUser()) {
- $this->setRevisionUserId($this->getOwnerId());
- }
- }
-
- public function getType() {
- return $this->bundle();
- }
-
- public function getName() {
- return $this->get('name')->value;
- }
-
- public function setName($name) {
- $this->set('name', $name);
- return $this;
- }
-
- public function getCreatedTime() {
- return $this->get('created')->value;
- }
-
- public function setCreatedTime($timestamp) {
- $this->set('created', $timestamp);
- return $this;
- }
-
- 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 isPublished() {
- return (bool) $this->getEntityKey('status');
- }
-
- public function setPublished($published) {
- $this->set('status', $published ? TRUE : FALSE);
- return $this;
- }
-
- public function getRevisionCreationTime() {
- return $this->get('revision_timestamp')->value;
- }
-
- public function setRevisionCreationTime($timestamp) {
- $this->set('revision_timestamp', $timestamp);
- return $this;
- }
-
- public function getRevisionUser() {
- return $this->get('revision_uid')->entity;
- }
-
- public function setRevisionUserId($uid) {
- $this->set('revision_uid', $uid);
- return $this;
- }
-
- public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
- $fields = parent::baseFieldDefinitions($entity_type);
- $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
- ->setLabel(t('Authored by'))
- ->setDescription(t('The user ID of author of the Heartbeat entity.'))
- ->setRevisionable(TRUE)
- ->setSetting('target_type', 'user')
- ->setSetting('handler', 'default')
- ->setTranslatable(TRUE)
- ->setDisplayOptions('view', array(
- 'label' => 'hidden',
- 'type' => 'author',
- 'weight' => 0,
- ))
- ->setDisplayOptions('form', array(
- 'type' => 'entity_reference_autocomplete',
- 'weight' => 5,
- 'settings' => array(
- 'match_operator' => 'CONTAINS',
- 'size' => '60',
- 'autocomplete_type' => 'tags',
- 'placeholder' => '',
- ),
- ))
- ->setDisplayConfigurable('form', TRUE)
- ->setDisplayConfigurable('view', TRUE);
- $fields['name'] = BaseFieldDefinition::create('string')
- ->setLabel(t('Name'))
- ->setDescription(t('The name of the Heartbeat entity.'))
- ->setRevisionable(TRUE)
- ->setSettings(array(
- 'max_length' => 50,
- 'text_processing' => 0,
- ))
- ->setDefaultValue('')
- ->setDisplayOptions('view', array(
- 'label' => 'above',
- 'type' => 'string',
- 'weight' => -4,
- ))
- ->setDisplayOptions('form', array(
- 'type' => 'string_textfield',
- 'weight' => -4,
- ))
- ->setDisplayConfigurable('form', TRUE)
- ->setDisplayConfigurable('view', TRUE);
- $fields['status'] = BaseFieldDefinition::create('boolean')
- ->setLabel(t('Publishing status'))
- ->setDescription(t('A boolean indicating whether the Heartbeat is published.'))
- ->setRevisionable(TRUE)
- ->setDefaultValue(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.'));
- $fields['revision_timestamp'] = BaseFieldDefinition::create('created')
- ->setLabel(t('Revision timestamp'))
- ->setDescription(t('The time that the current revision was created.'))
- ->setQueryable(FALSE)
- ->setRevisionable(TRUE);
- $fields['revision_uid'] = BaseFieldDefinition::create('entity_reference')
- ->setLabel(t('Revision user ID'))
- ->setDescription(t('The user ID of the author of the current revision.'))
- ->setSetting('target_type', 'user')
- ->setQueryable(FALSE)
- ->setRevisionable(TRUE);
- $fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean')
- ->setLabel(t('Revision translation affected'))
- ->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))
- ->setReadOnly(TRUE)
- ->setRevisionable(TRUE)
- ->setTranslatable(TRUE);
- return $fields;
- }
- }
|