View source
<?php
namespace Drupal\country_state_city\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\country_state_city\CountryListInterface;
use Drupal\user\UserInterface;
use Drupal\Core\Entity\EntityChangedTrait;
class CountryList extends ContentEntityBase implements CountryListInterface {
use EntityChangedTrait;
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'user_id' => \Drupal::currentUser()
->id(),
];
}
public function getName() {
return $this
->get('name')->value;
}
public function setName($name) {
$this
->set('name', $name);
return $this;
}
public function getIso2() {
return $this
->get('iso2')->value;
}
public function setIso2($iso2) {
$this
->set('iso2', $iso2);
return $this;
}
public function getIso3() {
return $this
->get('iso3')->value;
}
public function setIso3($iso3) {
$this
->set('iso3', $iso3);
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 static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Country entity.'))
->setReadOnly(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the Country entity.'))
->setReadOnly(TRUE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the Country entity.'))
->setTranslatable(TRUE)
->setSettings([
'max_length' => 255,
'text_processing' => 0,
])
->setDefaultValue(NULL)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -6,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -6,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setRequired(TRUE);
$fields['iso3'] = BaseFieldDefinition::create('string')
->setLabel(t('ISO3'))
->setDescription(t('The iso3 name of the Country list entity.'))
->setTranslatable(TRUE)
->setSettings([
'max_length' => 10,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -4,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setRequired(TRUE);
$fields['iso2'] = BaseFieldDefinition::create('string')
->setLabel(t('ISO2'))
->setDescription(t('The iso2 name of the Country list entity.'))
->setTranslatable(TRUE)
->setSettings([
'max_length' => 10,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -4,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setRequired(TRUE);
$fields['currency'] = BaseFieldDefinition::create('string')
->setLabel(t('Currency'))
->setDescription(t('The currency of the Country list entity.'))
->setTranslatable(TRUE)
->setSettings([
'max_length' => 50,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -4,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setRequired(TRUE);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The language code of Country entity.'));
$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;
}
}