View source
<?php
namespace Drupal\votingapi\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;
use Drupal\votingapi\VoteInterface;
class Vote extends ContentEntityBase implements VoteInterface {
public function getVotedEntityType() {
return $this
->get('entity_type')->value;
}
public function setVotedEntityType($name) {
return $this
->set('entity_type', $name);
}
public function getVotedEntityId() {
return $this
->get('entity_id')->target_id;
}
public function setVotedEntityId($id) {
return $this
->set('entity_id', $id);
}
public function getValue() {
return $this
->get('value')->value;
}
public function setValue($value) {
return $this
->set('value', $value);
}
public function getValueType() {
return $this
->get('value_type')->value;
}
public function setValueType($value_type) {
return $this
->set('value_type', $value_type);
}
public function getOwner() {
return $this
->get('user_id')->entity;
}
public function setOwner(UserInterface $account) {
$this
->set('user_id', $account
->id());
return $this;
}
public function getOwnerId() {
return $this
->get('user_id')->target_id;
}
public function setOwnerId($uid) {
$this
->set('user_id', $uid);
return $this;
}
public function getCreatedTime() {
return $this
->get('timestamp')->value;
}
public function setCreatedTime($timestamp) {
return $this
->set('timestamp', $timestamp);
}
public function getSource() {
return $this
->get('vote_source')->value;
}
public function setSource($source) {
return $this
->set('vote_source', $source);
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The vote ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The vote UUID.'))
->setReadOnly(TRUE);
$fields['type'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Type'))
->setDescription(t('The vote type.'))
->setSetting('target_type', 'vote_type')
->setReadOnly(TRUE);
$fields['entity_type'] = BaseFieldDefinition::create('string')
->setLabel(t('Entity Type'))
->setDescription(t('The type from the voted entity.'))
->setDefaultValue('node')
->setSettings([
'max_length' => 64,
])
->setRequired(TRUE);
$fields['entity_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Voted entity'))
->setDescription(t('The ID from the voted entity'))
->setDefaultValue(0)
->setRequired(TRUE);
$fields['value'] = BaseFieldDefinition::create('float')
->setLabel(t('Value'))
->setDescription(t('The numeric value of the vote.'))
->setDefaultValue(0)
->setRequired(TRUE);
$fields['value_type'] = BaseFieldDefinition::create('string')
->setLabel(t('Value Type'))
->setSettings([
'max_length' => 64,
])
->setDefaultValue('percent');
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user who submitted the vote.'))
->setSetting('target_type', 'user')
->setDefaultValueCallback('Drupal\\votingapi\\Entity\\Vote::getCurrentUserId');
$fields['timestamp'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the entity was created.'));
$fields['vote_source'] = BaseFieldDefinition::create('string')
->setLabel(t('Vote Source'))
->setDescription(t('The IP address hash from the user who submitted the vote.'))
->setDefaultValueCallback('Drupal\\votingapi\\Entity\\Vote::getCurrentIp')
->setSettings([
'max_length' => 255,
]);
return $fields;
}
public static function getCurrentUserId() {
return \Drupal::currentUser()
->id();
}
public static function getCurrentIp() {
return hash('sha256', serialize(\Drupal::request()
->getClientIp()));
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if (\Drupal::config('votingapi.settings')
->get('calculation_schedule') == 'immediate') {
\Drupal::service('plugin.manager.votingapi.resultfunction')
->recalculateResults($this
->getVotedEntityType(), $this
->getVotedEntityId(), $this
->bundle());
}
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
foreach ($entities as $entity) {
\Drupal::service('plugin.manager.votingapi.resultfunction')
->recalculateResults($entity
->getVotedEntityType(), $entity
->getVotedEntityId(), $entity
->bundle());
}
}
}