View source
<?php
namespace Drupal\video_transcode\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\video_transcode\TranscodeJobInterface;
use Drupal\user\UserInterface;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Url;
use Drupal\Core\Link;
class TranscodeJob extends ContentEntityBase implements TranscodeJobInterface {
use EntityChangedTrait;
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += array(
'user_id' => \Drupal::currentUser()
->id(),
);
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function getChangedTime() {
return $this
->get('changed')->value;
}
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 Transcode Job entity.'))
->setReadOnly(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the Transcode Job entity.'))
->setReadOnly(TRUE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The Name of the Transcode Job entity.'))
->setReadOnly(TRUE);
$fields['input_file'] = BaseFieldDefinition::create('video')
->setLabel(t('Input File'))
->setDescription(t('The input file for transcode job.'))
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'video_player_list',
'weight' => -6,
))
->setDisplayOptions('form', array(
'type' => 'video_upload',
'weight' => -6,
))
->setRequired(TRUE)
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['transcoder'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Transcoder'))
->setDescription(t('The transcoder.'))
->setSettings(array(
'allowed_values' => array(
'zencoder' => 'Zencoder',
'ffmpeg' => 'FFMpeg',
),
))
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'string',
'weight' => -5,
))
->setDisplayOptions('form', array(
'type' => 'options_buttons',
'weight' => -5,
))
->setRequired(TRUE)
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$preset_ids = \Drupal::entityQuery('video_transcode_preset')
->execute();
$presets = \Drupal::entityTypeManager()
->getStorage('video_transcode_preset')
->loadMultiple($preset_ids);
$preset_options = array();
foreach ($presets as $key => $preset) {
$preset_options[$key] = $preset->label;
}
if (empty($preset_options)) {
$preset_options['_none'] = t('None - Please create a Video Preset @here.', [
'@here' => Link::fromTextAndUrl(t('here'), Url::fromUri('internal:/admin/config/media/transcode-preset')),
]);
}
$fields['transcoder_preset'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Transcoder preset'))
->setDescription(t('The transcoder presets to use for output.'))
->setSettings(array(
'allowed_values' => $preset_options,
))
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'string',
'weight' => -5,
))
->setDisplayOptions('form', array(
'type' => 'options_buttons',
'weight' => -5,
))
->setCardinality(-1)
->setRequired(TRUE)
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['output_files'] = BaseFieldDefinition::create('video')
->setLabel(t('Output Files'))
->setDescription(t('The output files from them transcode job.'))
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'video_player_list',
'weight' => -4,
))
->setDisplayOptions('form', array(
'type' => 'video_upload',
'weight' => -4,
))
->setCardinality(-1)
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['thumbnails'] = BaseFieldDefinition::create('image')
->setLabel(t('Thumbnails'))
->setDescription(t('The video thumbnails.'))
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'image',
'weight' => -4,
))
->setDisplayOptions('form', array(
'type' => 'image',
'weight' => -4,
))
->setCardinality(-1)
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('User Name'))
->setDescription(t('The Name of the associated user.'))
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'author',
'weight' => -3,
))
->setDisplayOptions('form', array(
'type' => 'entity_reference_autocomplete',
'settings' => array(
'match_operator' => 'CONTAINS',
'size' => 60,
'placeholder' => '',
),
'weight' => -3,
))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['state'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Job state'))
->setDescription(t('The transcoder job state.'))
->setSettings(array(
'allowed_values' => array(
'idle' => 'Idle',
'processing' => 'Processing',
'finished' => 'Finished',
),
))
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'string',
'weight' => -5,
))
->setDisplayOptions('form', array(
'type' => 'options_select',
'weight' => -5,
))
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', FALSE);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The language code of Transcode Job 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.'));
$fields['finished'] = BaseFieldDefinition::create('changed')
->setLabel(t('Finished'))
->setDescription(t('The time that the transcode job was finished.'));
$fields['submitted'] = BaseFieldDefinition::create('changed')
->setLabel(t('Submitted'))
->setDescription(t('The time that the transcode job was submitted.'));
return $fields;
}
}