View source
<?php
namespace Drupal\opigno_certificate\Entity;
use Drupal\opigno_certificate\OpignoCertificateInterface;
use Drupal\Core\Entity\EditorialContentEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\user\UserInterface;
class OpignoCertificate extends EditorialContentEntityBase implements OpignoCertificateInterface {
protected $viewModeSelectorField;
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if (!$this
->getRevisionUser()) {
$this
->setRevisionUserId($this
->getOwnerId());
}
}
public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
parent::preSaveRevision($storage, $record);
if (!$this
->isNewRevision() && isset($this->original) && (!isset($record->revision_log) || $record->revision_log === '')) {
$record->revision_log = $this->original->revision_log->value;
}
}
public function setLabel($label) {
$this
->set('label', $label);
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('uid')->entity;
}
public function getOwnerId() {
return $this
->getEntityKey('uid');
}
public function setOwnerId($uid) {
$this
->set('uid', $uid);
return $this;
}
public function setOwner(UserInterface $account) {
$this
->set('uid', $account
->id());
return $this;
}
public function getRevisionAuthor() {
return $this
->getRevisionUser();
}
public function setRevisionAuthorId($uid) {
$this
->setRevisionUserId($uid);
return $this;
}
public function getViewModeSelectorField() {
if (!isset($this->viewModeSelectorField)) {
$this->viewModeSelectorField = FALSE;
$fields = $this
->getFieldDefinitions();
foreach ($fields as $field) {
if ($field
->getType() == 'view_mode_selector') {
$this->viewModeSelectorField = $field;
break;
}
}
}
return $this->viewModeSelectorField ?: NULL;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['label'] = BaseFieldDefinition::create('string')
->setLabel(t('Label'))
->setRequired(TRUE)
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -6,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
$fields['paper_orientation'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Paper Orientation'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue('portrait')
->setSetting('allowed_values', [
'portrait' => t('Portrait'),
'landscape' => t('Landscape'),
])
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => -5,
]);
$fields['referencing_entity'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Referencing entity'))
->setComputed(TRUE)
->setRevisionable(FALSE)
->setTranslatable(FALSE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The username of the opigno_certificate author.'))
->setRequired(TRUE)
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setDefaultValueCallback('Drupal\\opigno_certificate\\Entity\\OpignoCertificate::getCurrentUserId')
->setTranslatable(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('view', TRUE)
->setDisplayConfigurable('form', TRUE);
$fields['status']
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
'weight' => 120,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Authored on'))
->setDescription(t('The time that the opigno_certificate was created.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'timestamp',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'datetime_timestamp',
'weight' => 10,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the certificate was last edited.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE);
return $fields;
}
public static function getCurrentUserId() {
return [
\Drupal::currentUser()
->id(),
];
}
}