View source
<?php
namespace Drupal\bibcite_entity\Entity;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
class Contributor extends ContentEntityBase implements ContributorInterface {
use EntityChangedTrait;
const NAME_PARTS = [
'prefix',
'leading_title',
'first_name',
'middle_name',
'last_name',
'nick',
'suffix',
];
public function label() {
return $this
->getName();
}
public function getName() {
return $this
->get('name')->value;
}
public function getLeadingInitial() {
return $this
->get('leading_title')->value;
}
public function getFirstName() {
return $this
->get('first_name')->value;
}
public function getMiddleName() {
return $this
->get('middle_name')->value;
}
public function getLastName() {
return $this
->get('last_name')->value;
}
public function getNickName() {
return $this
->get('nick')->value;
}
public function getSuffix() {
return $this
->get('suffix')->value;
}
public function getPrefix() {
return $this
->get('prefix')->value;
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setName($name) {
$this
->set('name', $name);
return $this;
}
public function setLeadingInitial($leading) {
$this
->set('leading_title', $leading);
return $this;
}
public function setFirstName($first_name) {
$this
->set('first_name', $first_name);
return $this;
}
public function setMiddleName($middle_name) {
$this
->set('middle_name', $middle_name);
return $this;
}
public function setLastName($last_name) {
$this
->set('last_name', $last_name);
return $this;
}
public function setNickName($nick) {
$this
->set('nick', $nick);
return $this;
}
public function setSuffix($suffix) {
$this
->set('suffix', $suffix);
return $this;
}
public function setPrefix($prefix) {
$this
->set('prefix', $prefix);
return $this;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setComputed(TRUE)
->setReadOnly(FALSE)
->setCustomStorage(TRUE)
->setClass('\\Drupal\\bibcite_entity\\ContributorName')
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', [
'type' => 'bibcite_parse_name',
'weight' => 0,
]);
$fields['leading_title'] = BaseFieldDefinition::create('string')
->setLabel(t('Leading initial'))
->setDefaultValue('')
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 1,
])
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => 2,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['prefix'] = BaseFieldDefinition::create('string')
->setLabel(t('Prefix'))
->setDefaultValue('')
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 2,
])
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => 3,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['first_name'] = BaseFieldDefinition::create('string')
->setLabel(t('First name'))
->setDefaultValue('')
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 3,
])
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => 4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['middle_name'] = BaseFieldDefinition::create('string')
->setLabel(t('Middle name'))
->setDefaultValue('')
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 4,
])
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => 5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['last_name'] = BaseFieldDefinition::create('string')
->setLabel(t('Last name'))
->setDefaultValue('')
->setRequired(TRUE)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 5,
])
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => 6,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['nick'] = BaseFieldDefinition::create('string')
->setLabel(t('Nickname'))
->setDefaultValue('')
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 6,
])
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => 7,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['suffix'] = BaseFieldDefinition::create('string')
->setLabel(t('Suffix'))
->setDefaultValue('')
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 7,
])
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => 8,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', 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 static function getNameParts() {
return self::NAME_PARTS;
}
}