cmlapi-8.x-1.2/src/Entity/CmlEntity.php
src/Entity/CmlEntity.php
<?php
namespace Drupal\cmlapi\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\user\UserInterface;
/**
* Defines the Cml entity entity.
*
* @ingroup cmlapi
*
* @ContentEntityType(
* id = "cml",
* label = @Translation("Cml entity"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\cmlapi\Entity\CmlEntityListBuilder",
* "views_data" = "Drupal\cmlapi\Entity\CmlEntityViewsData",
*
* "form" = {
* "default" = "Drupal\cmlapi\Form\CmlEntityForm",
* "add" = "Drupal\cmlapi\Form\CmlEntityForm",
* "edit" = "Drupal\cmlapi\Form\CmlEntityForm",
* "delete" = "Drupal\cmlapi\Form\CmlEntityDeleteForm",
* },
* "access" = "Drupal\cmlapi\Entity\CmlEntityAccessControlHandler",
* "route_provider" = {
* "html" = "Drupal\cmlapi\Entity\CmlEntityHtmlRouteProvider",
* },
* },
* base_table = "cml",
* admin_permission = "administer cml entity entities",
* entity_keys = {
* "id" = "id",
* "label" = "name",
* "uuid" = "uuid",
* "uid" = "user_id",
* "langcode" = "langcode",
* "status" = "status",
* },
* links = {
* "canonical" = "/admin/structure/cml/{cml}",
* "add-form" = "/admin/structure/cml/add",
* "edit-form" = "/admin/structure/cml/{cml}/edit",
* "delete-form" = "/admin/structure/cml/{cml}/delete",
* "collection" = "/admin/structure/cml",
* },
* field_ui_base_route = "cml.settings"
* )
*/
class CmlEntity extends ContentEntityBase implements CmlEntityInterface {
use EntityChangedTrait;
/**
* {@inheritdoc}
*/
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'user_id' => \Drupal::currentUser()->id(),
];
}
/**
* {@inheritdoc}
*/
public function getName() {
return $this->get('name')->value;
}
/**
* {@inheritdoc}
*/
public function setName($name) {
$this->set('name', $name);
return $this;
}
/**
* {@inheritdoc}
*/
public function getState() {
return $this->get('state')->value;
}
/**
* {@inheritdoc}
*/
public function setState($state) {
$this->set('state', $state);
return $this;
}
/**
* {@inheritdoc}
*/
public function getType() {
return $this->get('type')->value;
}
/**
* {@inheritdoc}
*/
public function setType($type) {
$this->set('type', $type);
return $this;
}
/**
* {@inheritdoc}
*/
public function getCreatedTime() {
return $this->get('created')->value;
}
/**
* {@inheritdoc}
*/
public function setCreatedTime($timestamp) {
$this->set('created', $timestamp);
return $this;
}
/**
* {@inheritdoc}
*/
public function getOwner() {
return $this->get('user_id')->entity;
}
/**
* {@inheritdoc}
*/
public function getOwnerId() {
return $this->get('user_id')->target_id;
}
/**
* {@inheritdoc}
*/
public function setOwnerId($uid) {
$this->set('user_id', $uid);
return $this;
}
/**
* {@inheritdoc}
*/
public function setOwner(UserInterface $account) {
$this->set('user_id', $account->id());
return $this;
}
/**
* {@inheritdoc}
*/
public function isPublished() {
return (bool) $this->getEntityKey('status');
}
/**
* {@inheritdoc}
*/
public function setPublished($published) {
$this->set('status', $published ? TRUE : FALSE);
return $this;
}
/**
* {@inheritdoc}
*/
public function setFull($full) {
$this->set('full', $full ? TRUE : FALSE);
return $this;
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the Cml entity entity.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->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['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the Cml entity entity.'))
->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);
$fields['type'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Type'))
->setDescription(t('Exchange type: catalog/sale.'))
->setSettings([
'allowed_values' => [
'catalog' => 'Catalog',
'sale' => 'Sale',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['state'] = BaseFieldDefinition::create('list_string')
->setLabel(t('State'))
->setDescription(t('Migration state.'))
->setSettings([
'allowed_values' => [
'zip' => 'Zip',
'new' => 'New',
'progress' => 'Progress',
'success' => 'Success',
'busy' => 'Busy',
'failure' => 'Failure',
],
])
->setDefaultValue('new')
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['login'] = BaseFieldDefinition::create('string')
->setLabel(t('1C Login'))
->setDescription(t('Exchange 1С login.'))
->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);
$fields['ip'] = BaseFieldDefinition::create('string')
->setLabel(t('IP'))
->setDescription(t('1C IP address.'))
->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);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating whether the Cml entity is published.'))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setDefaultValue(TRUE);
$fields['full'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Full Exchange'))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setDefaultValue(FALSE);
$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;
}
}
