View source
<?php
namespace Drupal\brightcove\Entity;
use Brightcove\Item\Video\TextTrack;
use Drupal\brightcove\EntityChangedFieldsTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\brightcove\BrightcoveTextTrackInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\link\LinkItemInterface;
use Drupal\user\UserInterface;
class BrightcoveTextTrack extends ContentEntityBase implements BrightcoveTextTrackInterface {
use EntityChangedTrait;
use EntityChangedFieldsTrait;
const KIND_CAPTIONS = 'captions';
const KIND_SUBTITLES = 'subtitles';
const KIND_DESCRIPTION = 'descriptions';
const KIND_CHAPTERS = 'chapters';
const KIND_METADATA = 'metadata';
public function getName() {
return $this
->get('name')->value;
}
public function setName($name) {
return $this
->set('name', $name);
}
public function getWebVttFile() {
return $this
->get('webvtt_file')
->getValue();
}
public function setWebVttFile(array $file) {
return $this
->set('webvtt_file', $file);
}
public function getTextTrackId() {
return $this
->get('text_track_id')->value;
}
public function setTextTrackId($text_track_id) {
return $this
->set('text_track_id', $text_track_id);
}
public function getSource() {
return $this
->get('source')->value;
}
public function setSource($source) {
return $this
->set('source', $source);
}
public function getSourceLanguage() {
return $this
->get('source_language')->value;
}
public function setSourceLanguage($source_language) {
return $this
->set('source_language', $source_language);
}
public function getLabel() {
return $this
->get('label')->value;
}
public function setLabel($label) {
return $this
->set('label', $label);
}
public function getKind() {
return $this
->get('kind')->value;
}
public function setKind($kind) {
return $this
->set('kind', $kind);
}
public function getMimeType() {
return $this
->get('mime_type')->value;
}
public function setMimeType($mime_type) {
return $this
->set('mime_type', $mime_type);
}
public function getAssetId() {
return $this
->get('asset_id')->value;
}
public function setAssetId($asset_id) {
return $this
->set('asset_id', $asset_id);
}
public function getSources() {
return $this
->get('sources')
->getValue();
}
public function setSources(array $sources) {
return $this
->set('sources', $sources);
}
public function isDefault() {
return (bool) $this
->get('default_text_track')->value;
}
public function setDefault($default) {
return $this
->set('default_text_track', $default);
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
return $this
->set('created', $timestamp);
}
public function getOwner() {
return $this
->get('uid')->entity;
}
public function getOwnerId() {
return $this
->get('uid')->target_id;
}
public function setOwnerId($uid) {
return $this
->set('uid', $uid);
}
public function setOwner(UserInterface $account) {
return $this
->set('uid', $account
->id());
}
public function isPublished() {
return (bool) $this
->getEntityKey('status');
}
public function setPublished($published) {
return $this
->set('status', $published ? NODE_PUBLISHED : NODE_NOT_PUBLISHED);
}
public function preSave(EntityStorageInterface $storage) {
$this
->checkUpdatedFields($storage);
if (empty($this
->getLabel())) {
$this
->setName($this
->getSourceLanguage());
}
else {
$this
->setName($this
->getLabel());
}
return parent::preSave($storage);
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$weight = -30;
$fields['bcttid'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Brightcove Text Track entity.'))
->setReadOnly(TRUE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('Generated name for the Text Track.'))
->setRequired(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the Brightcove Text Track entity.'))
->setReadOnly(TRUE);
$fields['text_track_id'] = BaseFieldDefinition::create('string')
->setLabel(t('Text Track ID'))
->setDescription(t('Unique Text Track ID assigned by Brightcove.'))
->setReadOnly(TRUE)
->setDisplayOptions('view', [
'type' => 'string',
'label' => 'inline',
'weight' => ++$weight,
])
->setDisplayConfigurable('view', TRUE);
$fields['webvtt_file'] = BaseFieldDefinition::create('file')
->setLabel(t('WebVTT file'))
->setRequired(TRUE)
->setSettings([
'file_extensions' => 'vtt',
'file_directory' => '[random:hash:md5]',
])
->setDisplayOptions('form', [
'type' => 'file_generic',
'weight' => ++$weight,
])
->setDisplayOptions('view', [
'type' => 'file_url_plain',
'label' => 'inline',
'weight' => $weight,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['source'] = BaseFieldDefinition::create('link')
->setLabel(t('Source'))
->setDescription(t('Source text track.'))
->setSettings([
'max_length' => 500,
'link_type' => LinkItemInterface::LINK_GENERIC,
'title' => DRUPAL_DISABLED,
])
->setDisplayOptions('view', [
'type' => 'link',
'label' => 'above',
'weight' => $weight,
'settings' => [
'rel' => TRUE,
'target' => '_blank',
],
])
->setDisplayConfigurable('view', TRUE);
$fields['source_language'] = BaseFieldDefinition::create('string')
->setLabel(t('Language'))
->setDescription(t('ISO-639-1 language code with optional ISO-3166 country name (en, en-US, de, de-DE).'))
->setRequired(TRUE)
->setSettings([
'max_length' => 10,
'text_processing' => 0,
])
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => ++$weight,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => $weight,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['label'] = BaseFieldDefinition::create('string')
->setLabel(t('Label'))
->setDescription(t('Title to be displayed in the player menu.'))
->setSettings([
'max_length' => 255,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => ++$weight,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => $weight,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['kind'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Kind'))
->setDescription(t('How the vtt file is meant to be used.'))
->setRequired(TRUE)
->setDefaultValue(self::KIND_CAPTIONS)
->setSetting('allowed_values', [
self::KIND_CAPTIONS => 'captions',
self::KIND_SUBTITLES => 'subtitles',
self::KIND_DESCRIPTION => 'descriptions',
self::KIND_CHAPTERS => 'chapters',
self::KIND_METADATA => 'metadata',
])
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => ++$weight,
])
->setDisplayOptions('view', [
'type' => 'string',
'label' => 'inline',
'weight' => $weight,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['mime_type'] = BaseFieldDefinition::create('string')
->setLabel(t('MIME type'))
->setDescription(t('MIME type of the source text track.'))
->setReadOnly(TRUE)
->setDisplayOptions('view', [
'type' => 'string',
'label' => 'inline',
'weight' => ++$weight,
])
->setDisplayConfigurable('view', TRUE);
$fields['asset_id'] = BaseFieldDefinition::create('string')
->setLabel(t('Asset ID'))
->setDescription(t('Asset ID assigned by Brightcove.'))
->setReadOnly(TRUE)
->setDisplayOptions('view', [
'type' => 'string',
'label' => 'inline',
'weight' => ++$weight,
])
->setDisplayConfigurable('view', TRUE);
$fields['sources'] = BaseFieldDefinition::create('link')
->setLabel(t('Sources'))
->setDescription(t('Address of the track file(s).'))
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setSettings([
'max_length' => 500,
'link_type' => LinkItemInterface::LINK_GENERIC,
'title' => DRUPAL_DISABLED,
])
->setDisplayOptions('view', [
'type' => 'link',
'label' => 'above',
'weight' => $weight,
'settings' => [
'rel' => TRUE,
'target' => '_blank',
],
])
->setDisplayConfigurable('view', TRUE);
$fields['default_text_track'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Default'))
->setDescription(t('Setting this to true makes this the default captions file in the player menu.'))
->setDefaultValue(FALSE)
->setSettings([
'on_label' => t('Yes'),
'off_label' => t('No'),
])
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'weight' => ++$weight,
'settings' => [
'display_label' => TRUE,
],
])
->setDisplayOptions('view', [
'type' => 'string',
'label' => 'inline',
'weight' => $weight,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating whether the Brightcove Text Track is published.'))
->setDefaultValue(TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The username of the Brightcove Playlist author.'))
->setSetting('target_type', 'user')
->setDefaultValueCallback(self::class . '::getCurrentUserId')
->setTranslatable(TRUE)
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => ++$weight,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayOptions('view', [
'type' => 'author',
'label' => 'inline',
'weight' => $weight,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The language code for the Brightcove Text Track entity.'))
->setDisplayOptions('form', [
'type' => 'language_select',
'weight' => ++$weight,
])
->setDisplayConfigurable('form', 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 createOrUpdate(TextTrack $text_track, EntityStorageInterface $storage, $video_entity_id) {
$text_track_entity = BrightcoveTextTrack::loadByTextTrackId($text_track
->getId());
$text_track_needs_save = FALSE;
if (!empty($video_entity_id)) {
$video = BrightcoveVideo::load($video_entity_id);
}
else {
throw new \Exception('The $video_entity_id argument cannot be empty.');
}
$sources = $text_track
->getSources();
$field_sources = [];
foreach ($sources as $source) {
$field_sources[] = $source
->getSrc();
}
if (!is_null($text_track_entity)) {
if ($text_track_entity
->getSource() != ($source = $text_track
->getSrc())) {
$text_track_entity
->setSource($source);
$text_track_needs_save = TRUE;
}
if ($text_track_entity
->getSourceLanguage() != ($source_language = $text_track
->getSrclang())) {
$text_track_entity
->setSourceLanguage($source_language);
$text_track_needs_save = TRUE;
}
if ($text_track_entity
->getLabel() != ($label = $text_track
->getLabel())) {
$text_track_entity
->setLabel($label);
$text_track_needs_save = TRUE;
}
if ($text_track_entity
->getKind() != ($kind = $text_track
->getKind())) {
$text_track_entity
->setKind($kind);
$text_track_needs_save = TRUE;
}
if ($text_track_entity
->getMimeType() != ($mime_type = $text_track
->getMimeType())) {
$text_track_entity
->setMimeType($mime_type);
$text_track_needs_save = TRUE;
}
$entity_sources = $text_track_entity
->getSources();
$entity_field_sources = [];
foreach ($entity_sources as $entity_source) {
$entity_field_sources[] = $entity_source['uri'];
}
if ($entity_field_sources != $field_sources) {
$text_track_entity
->setSources($field_sources);
$text_track_needs_save = TRUE;
}
if ($text_track_entity
->isDefault() != ($default = $text_track
->isDefault())) {
$text_track_entity
->setDefault($default);
$text_track_needs_save = TRUE;
}
}
else {
$values = [
'text_track_id' => $text_track
->getId(),
'source' => $text_track
->getSrc(),
'source_language' => $text_track
->getSrclang(),
'label' => $text_track
->getLabel(),
'kind' => $text_track
->getKind(),
'mime_type' => $text_track
->getMimeType(),
'asset_id' => $text_track
->getAssetId(),
'sources' => $field_sources,
'default_text_track' => $text_track
->isDefault(),
'created' => $video
->getCreatedTime(),
];
$text_track_entity = BrightcoveTextTrack::create($values);
$text_track_needs_save = TRUE;
}
if ($text_track_needs_save) {
$text_track_entity
->setChangedTime($video
->getChangedTime());
$text_track_entity
->save();
}
$text_tracks = $video
->getTextTracks();
$exists = FALSE;
foreach ($text_tracks as $text_track) {
if ($text_track['target_id'] == $text_track_entity
->id()) {
$exists = TRUE;
break;
}
}
if (!$exists) {
$text_tracks[] = [
'target_id' => $text_track_entity
->id(),
];
$video
->setTextTracks($text_tracks);
$video
->save();
}
}
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'uid' => \Drupal::currentUser()
->id(),
'name' => t('New text track'),
];
}
public static function getCurrentUserId() {
return [
\Drupal::currentUser()
->id(),
];
}
public static function loadByTextTrackId($id) {
$entity_ids = \Drupal::entityQuery('brightcove_text_track')
->condition('text_track_id', $id)
->execute();
if (empty($entity_ids)) {
return NULL;
}
$entity_id = reset($entity_ids);
return self::load($entity_id);
}
}