public static function BrightcoveTextTrack::createOrUpdate in Brightcove Video Connect 8
Same name and namespace in other branches
- 8.2 src/Entity/BrightcoveTextTrack.php \Drupal\brightcove\Entity\BrightcoveTextTrack::createOrUpdate()
- 3.x src/Entity/BrightcoveTextTrack.php \Drupal\brightcove\Entity\BrightcoveTextTrack::createOrUpdate()
Create or update an existing text track from a Brightcove object.
Parameters
\Brightcove\Object\Video\TextTrack $text_track: Brightcove Video object.
\Drupal\Core\Entity\EntityStorageInterface $storage: EntityStorage.
int $video_entity_id: The ID of the BrightcoveVideo entity.
Throws
\Exception If BrightcoveVideo ID is missing when a new entity is being created or if the BrightcoveVideo cannot be found or loaded.
2 calls to BrightcoveTextTrack::createOrUpdate()
- BrightcoveTextTrackQueueWorker::processItem in src/
Plugin/ QueueWorker/ BrightcoveTextTrackQueueWorker.php - Works on a single queue item.
- BrightcoveVideoController::ingestionCallback in src/
Controller/ BrightcoveVideoController.php - Ingestion callback for Brightcove.
File
- src/
Entity/ BrightcoveTextTrack.php, line 554
Class
- BrightcoveTextTrack
- Defines the Brightcove Text Track entity.
Namespace
Drupal\brightcove\EntityCode
public static function createOrUpdate(TextTrack $text_track, EntityStorageInterface $storage, $video_entity_id) {
/** @var \Drupal\brightcove\Entity\BrightcoveTextTrack $text_track_entity */
$text_track_entity = BrightcoveTextTrack::loadByTextTrackId($text_track
->getId());
$text_track_needs_save = FALSE;
if (!empty($video_entity_id)) {
/** @var \Drupal\brightcove\Entity\BrightcoveVideo $video */
$video = BrightcoveVideo::load($video_entity_id);
}
else {
throw new \Exception('The $video_entity_id argument cannot be empty.');
}
// Get sources.
$sources = $text_track
->getSources();
$field_sources = [];
foreach ($sources as $source) {
$field_sources[] = $source
->getSrc();
}
// Update existing text track.
if (!is_null($text_track_entity)) {
// Update source field if needed.
if ($text_track_entity
->getSource() != ($source = $text_track
->getSrc())) {
$text_track_entity
->setSource($source);
$text_track_needs_save = TRUE;
}
// Update source language field if needed.
if ($text_track_entity
->getSourceLanguage() != ($source_language = $text_track
->getSrclang())) {
$text_track_entity
->setSourceLanguage($source_language);
$text_track_needs_save = TRUE;
}
// Update label field if needed.
if ($text_track_entity
->getLabel() != ($label = $text_track
->getLabel())) {
$text_track_entity
->setLabel($label);
$text_track_needs_save = TRUE;
}
// Update kind field if needed.
if ($text_track_entity
->getKind() != ($kind = $text_track
->getKind())) {
$text_track_entity
->setKind($kind);
$text_track_needs_save = TRUE;
}
// Update mime type field if needed.
if ($text_track_entity
->getMimeType() != ($mime_type = $text_track
->getMimeType())) {
$text_track_entity
->setMimeType($mime_type);
$text_track_needs_save = TRUE;
}
// Update sources if needed.
$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;
}
// Update default if needed.
if ($text_track_entity
->isDefault() != ($default = $text_track
->isDefault())) {
$text_track_entity
->setDefault($default);
$text_track_needs_save = TRUE;
}
}
else {
// Build the new text track entity.
$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;
}
// Save text track entity.
if ($text_track_needs_save) {
// Set the same changed time for the text track as the video.
$text_track_entity
->setChangedTime($video
->getChangedTime());
$text_track_entity
->save();
}
// Add the current text track for the video if needed.
$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();
}
}