View source
<?php
namespace Drupal\activity_creator\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\activity_creator\ActivityInterface;
use Drupal\flag\Entity\Flagging;
use Drupal\user\UserInterface;
use Drupal\node\NodeInterface;
class Activity extends ContentEntityBase implements ActivityInterface {
use EntityChangedTrait;
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'user_id' => \Drupal::currentUser()
->id(),
];
}
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 ? NodeInterface::PUBLISHED : NodeInterface::NOT_PUBLISHED);
return $this;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Activity entity.'))
->setReadOnly(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the Activity entity.'))
->setReadOnly(TRUE);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the Activity entity.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValueCallback('Drupal\\node\\Entity\\Node::getDefaultEntityOwner')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating whether the Activity is published.'))
->setDefaultValue(TRUE);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The language code for the Activity entity.'))
->setDisplayOptions('form', [
'type' => 'language_select',
'weight' => 10,
])
->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;
}
public function getRelatedEntity() {
$related_object = $this
->get('field_activity_entity')
->getValue();
if (!empty($related_object)) {
$target_type = $related_object['0']['target_type'];
$target_id = $related_object['0']['target_id'];
$entity_storage = $this
->entityTypeManager()
->getStorage($target_type);
$entity = $entity_storage
->load($target_id);
return $entity;
}
return NULL;
}
public function getRelatedEntityUrl() {
$link = "";
$related_object = $this
->get('field_activity_entity')
->getValue();
if (!empty($related_object)) {
$target_type = $related_object['0']['target_type'];
$target_id = $related_object['0']['target_id'];
if ($target_type === 'vote') {
if ($vote = \Drupal::service('entity_type.manager')
->getStorage($target_type)
->load($target_id)) {
$target_type = $vote
->getVotedEntityType();
$target_id = $vote
->getVotedEntityId();
}
}
elseif ($target_type === 'group_content') {
if ($group_content = \Drupal::service('entity_type.manager')
->getStorage($target_type)
->load($target_id)) {
$target_type = $group_content
->getEntity()
->getEntityTypeId();
$target_id = $group_content
->getEntity()
->id();
}
}
elseif ($target_type === 'event_enrollment') {
$entity_storage = \Drupal::entityTypeManager()
->getStorage($target_type);
$entity = $entity_storage
->load($target_id);
if ($entity !== NULL) {
$event_id = $entity
->getFieldValue('field_event', 'target_id');
$target_id = $event_id;
$target_type = 'node';
}
}
elseif ($target_type === 'flagging') {
$flagging = Flagging::load($target_id);
$target_type = $flagging
->getFlaggableType();
$target_id = $flagging
->getFlaggableId();
}
$entity_storage = \Drupal::entityTypeManager()
->getStorage($target_type);
$entity = $entity_storage
->load($target_id);
if ($entity !== NULL) {
$link = $entity
->toUrl('canonical');
}
}
return $link;
}
public function getDestinations() {
$values = [];
$field_activity_destinations = $this->field_activity_destinations;
if (isset($field_activity_destinations)) {
$destinations = $field_activity_destinations
->getValue();
foreach ($destinations as $destination) {
$values[] = $destination['value'];
}
}
return $values;
}
public function getRecipient() {
$value = NULL;
$field_activity_recipient_user = $this->field_activity_recipient_user;
if (isset($field_activity_recipient_user)) {
$recipient_user = $field_activity_recipient_user
->getValue();
if (!empty($recipient_user)) {
$recipient_user['0']['target_type'] = 'user';
return $recipient_user;
}
}
$field_activity_recipient_group = $this->field_activity_recipient_group;
if (isset($field_activity_recipient_group)) {
$recipient_group = $field_activity_recipient_group
->getValue();
if (!empty($recipient_group)) {
$recipient_group['0']['target_type'] = 'group';
return $recipient_group;
}
}
return $value;
}
}