View source
<?php
namespace Drupal\data_stream\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
class DataStream extends ContentEntityBase implements DataStreamInterface {
use EntityChangedTrait;
public function label() {
return $this
->getName();
}
public function getPlugin() {
return \Drupal::service('plugin.manager.data_stream_type')
->createInstance($this
->bundle());
}
public function getName() {
return $this
->get('name')->value;
}
public function setName($name) {
$this
->set('name', $name);
return $this;
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
public function getPrivateKey() {
return $this
->get('private_key')->value;
}
public function isPublic() {
return $this
->get('public')->value;
}
public function getBundleLabel() {
$type = \Drupal::entityTypeManager()
->getStorage('data_stream_type')
->load($this
->bundle());
return $type
->label();
}
public static function getRequestTime() {
return \Drupal::time()
->getRequestTime();
}
public static function createUniqueKey() {
return hash('md5', mt_rand());
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the data stream.'))
->setTranslatable(TRUE)
->setDefaultValue('')
->setSetting('max_length', 255)
->setSetting('text_processing', 0)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['private_key'] = BaseFieldDefinition::create('string')
->setLabel(t('Private Key'))
->setDescription(t('Private key for the data stream.'))
->setTranslatable(TRUE)
->setDefaultValueCallback(static::class . '::createUniqueKey')
->setSetting('max_length', 255)
->setSetting('text_processing', 0)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 3,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['public'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Public'))
->setDescription(t('If the data stream has public access via API.'))
->setDefaultValue(FALSE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'boolean',
'settings' => [
'format' => 'yes-no',
],
'weight' => -3,
])
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'weight' => 5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['asset'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Asset'))
->setDescription(t('Associate this data stream with any assets it describes.'))
->setTranslatable(FALSE)
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setSetting('target_type', 'asset')
->setDisplayOptions('form', [
'type' => 'entity_reference',
'weight' => 12,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created on'))
->setDescription(t('The time that the data_stream was created.'))
->setRevisionable(TRUE)
->setDefaultValueCallback(static::class . '::getRequestTime')
->setDisplayConfigurable('form', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time the data_stream was last edited.'))
->setRevisionable(TRUE);
return $fields;
}
}