public function BrightcoveVideo::save in Brightcove Video Connect 8
Same name and namespace in other branches
- 8.2 src/Entity/BrightcoveVideo.php \Drupal\brightcove\Entity\BrightcoveVideo::save()
- 3.x src/Entity/BrightcoveVideo.php \Drupal\brightcove\Entity\BrightcoveVideo::save()
Parameters
bool $upload: Whether to upload the video to Brightcove or not.
Overrides EntityBase::save
File
- src/
Entity/ BrightcoveVideo.php, line 611
Class
- BrightcoveVideo
- Defines the Brightcove Video entity.
Namespace
Drupal\brightcove\EntityCode
public function save($upload = FALSE) {
// Check if it will be a new entity or an existing one being updated.
$status = $this
->id() ? SAVED_UPDATED : SAVED_NEW;
// Make sure that preSave runs before any modification is made for the
// entity.
$saved = parent::save();
// Upload data for Brightcove only if we saved the video from form.
if ($upload) {
$cms = BrightcoveUtil::getCmsApi($this
->getApiClient());
// Setup video object and set minimum required values.
$video = new Video();
$video
->setName($this
->getName());
// Save or update description if needed.
if ($this
->isFieldChanged('description')) {
$video
->setDescription($this
->getDescription());
}
// Save or update duration if needed.
if ($this
->isFieldChanged('duration')) {
$video
->setDuration($this
->getDuration());
}
// Save or update economics if needed.
if ($this
->isFieldChanged('economics')) {
$video
->setEconomics($this
->getEconomics());
}
// Save or update tags if needed.
if ($this
->isFieldChanged('tags')) {
// Get term IDs.
$term_ids = [];
foreach ($this
->getTags() as $tag) {
$term_ids[] = $tag['target_id'];
}
// Load terms.
/** @var \Drupal\taxonomy\Entity\Term[] $terms */
$terms = Term::loadMultiple($term_ids);
$tags = [];
foreach ($terms as $term) {
$tags[] = $term
->getName();
}
$video
->setTags($tags);
}
// Save or update link if needed.
if ($this
->isFieldChanged('related_link')) {
$link = new Link();
$related_link = $this
->getRelatedLink();
$is_link_set = FALSE;
if (!empty($related_link['uri'])) {
$link
->setUrl(Url::fromUri($related_link['uri'], [
'absolute' => TRUE,
])
->toString());
$is_link_set = TRUE;
}
if (!empty($related_link['title'])) {
$link
->setText($related_link['title']);
$is_link_set = TRUE;
}
if ($is_link_set) {
$video
->setLink($link);
}
else {
$video
->setLink();
}
}
// Save or update long description if needed.
if ($this
->isFieldChanged('long_description')) {
$video
->setLongDescription($this
->getLongDescription());
}
// Save or update reference ID if needed.
if ($this
->isFieldChanged('reference_id')) {
$video
->setReferenceId($this
->getReferenceId());
}
// Save or update custom field values.
$custom_field_num = \Drupal::entityQuery('brightcove_custom_field')
->condition('api_client', $this
->getApiClient())
->count()
->execute();
if ($this
->isFieldChanged('custom_field_values') && $custom_field_num) {
$video
->setCustomFields($this
->getCustomFieldValues());
}
// Save or update schedule start at date if needed.
if ($this
->isFieldChanged('schedule_starts_at') || $this
->isFieldChanged('schedule_ends_at')) {
$starts_at = $this
->getScheduleStartsAt();
$ends_at = $this
->getScheduleEndsAt();
$schedule = new Schedule();
$is_schedule_set = FALSE;
if (!is_null($starts_at)) {
$schedule
->setStartsAt($starts_at . '.000Z');
$is_schedule_set = TRUE;
}
if (!is_null($ends_at)) {
$schedule
->setEndsAt($ends_at . '.000Z');
$is_schedule_set = TRUE;
}
if ($is_schedule_set) {
$video
->setSchedule($schedule);
}
else {
$video
->setSchedule();
}
}
// Upload text tracks.
if ($this
->isFieldChanged('text_tracks')) {
$video_text_tracks = [];
$ingest_text_tracks = [];
foreach ($this
->getTextTracks() as $text_track) {
if (!empty($text_track['target_id'])) {
/** @var \Drupal\brightcove\Entity\BrightcoveTextTrack $text_track_entity */
$text_track_entity = BrightcoveTextTrack::load($text_track['target_id']);
if (!is_null($text_track_entity)) {
// Setup ingestion request if there was a text track uploaded.
$webvtt_file = $text_track_entity
->getWebVttFile();
if (!empty($webvtt_file[0]['target_id'])) {
/** @var \Drupal\file\Entity\File $file */
$file = File::load($webvtt_file[0]['target_id']);
if (!is_null($file)) {
$ingest_text_tracks[] = (new IngestTextTrack())
->setSrclang($text_track_entity
->getSourceLanguage())
->setUrl(file_create_url($file
->getFileUri()))
->setKind($text_track_entity
->getKind())
->setLabel($text_track_entity
->getLabel())
->setDefault($text_track_entity
->isDefault());
}
}
else {
$video_text_track = (new TextTrack())
->setId($text_track_entity
->getTextTrackId())
->setSrclang($text_track_entity
->getSourceLanguage())
->setLabel($text_track_entity
->getLabel())
->setKind($text_track_entity
->getKind())
->setMimeType($text_track_entity
->getMimeType())
->setAssetId($text_track_entity
->getAssetId());
// If asset ID is set the src will be ignored, so in this case
// we don't set the src.
if (!empty($text_track_entity
->getAssetId())) {
$video_text_track
->setAssetId($text_track_entity
->getAssetId());
}
else {
$video_text_track
->setSrc($text_track_entity
->getSource());
}
// Get text track sources.
$video_text_track_sources = [];
foreach ($text_track_entity
->getSources() as $source) {
$text_track_source = new TextTrackSource();
$text_track_source
->setSrc($source['uri']);
$video_text_track_sources[] = $text_track_source;
}
$video_text_track
->setSources($video_text_track_sources);
$video_text_tracks[] = $video_text_track;
}
}
}
}
// Set existing text tracks.
$video
->setTextTracks($video_text_tracks);
}
// Update status field based on Brightcove's Video state.
if ($this
->isFieldChanged('status')) {
$video
->setState($this
->isPublished() ? self::STATE_ACTIVE : self::STATE_INACTIVE);
}
// Create or update a video.
switch ($status) {
case SAVED_NEW:
// Create new video on Brightcove.
$saved_video = $cms
->createVideo($video);
// Set the rest of the fields on BrightcoveVideo entity.
$this
->setVideoId($saved_video
->getId());
$this
->setCreatedTime(strtotime($saved_video
->getCreatedAt()));
break;
case SAVED_UPDATED:
// Set Video ID.
$video
->setId($this
->getVideoId());
// Update video.
$saved_video = $cms
->updateVideo($video);
break;
}
// Set ingestion for thumbnail.
if ($this
->isFieldChanged(self::IMAGE_TYPE_THUMBNAIL)) {
$this
->createIngestImage(self::IMAGE_TYPE_THUMBNAIL);
}
// Set ingestion for poster.
if ($this
->isFieldChanged(self::IMAGE_TYPE_POSTER)) {
$this
->createIngestImage(self::IMAGE_TYPE_POSTER);
}
// Set ingestion for video.
if ($this
->isFieldChanged('video_file') && !empty($this
->getVideoFile())) {
/** @var \Drupal\file\Entity\File $file */
$file = File::load($this
->getVideoFile()['target_id']);
$ingest_request = $this
->getIngestRequest();
$ingest_master = new IngestRequestMaster();
$ingest_master
->setUrl(file_create_url($file
->getFileUri()));
$ingest_request
->setMaster($ingest_master);
$profiles = self::getProfileAllowedValues($this
->getApiClient());
$ingest_request
->setProfile($profiles[$this
->getProfile()]);
}
// Set ingestion for video url.
if ($this
->isFieldChanged('video_url') && !empty($this
->getVideoUrl())) {
$ingest_request = $this
->getIngestRequest();
$ingest_master = new IngestRequestMaster();
$ingest_master
->setUrl($this
->getVideoUrl());
$ingest_request
->setMaster($ingest_master);
$profiles = self::getProfileAllowedValues($this
->getApiClient());
$ingest_request
->setProfile($profiles[$this
->getProfile()]);
}
// Set ingestion for text tracks.
if (!empty($ingest_text_tracks)) {
$ingest_request = $this
->getIngestRequest();
$ingest_request
->setTextTracks($ingest_text_tracks);
}
// Update changed time and video entity with the video ID.
if (isset($saved_video)) {
$this
->setChangedTime(strtotime($saved_video
->getUpdatedAt()));
// Save the entity again to save some new values which are only
// available after creating/updating the video on Brightcove.
// Also don't change the save state to show the correct message when if
// the entity is created or updated.
parent::save();
}
// Send the ingest request if there was an ingestible asset.
if (!is_null($this->ingestRequest)) {
// Get token.
$token = $this
->getIngestionToken();
// Make sure that the token is generated successfully. If there was an
// error generating the token then be nice to Brightcove and don't set
// the callback url, so it won't hit a wall by trying to notify us on a
// non-valid URL.
if (!is_null($token)) {
$callback_url = Url::fromRoute('brightcove_ingestion_callback', [
'token' => $token,
], [
'absolute' => TRUE,
])
->toString();
$this->ingestRequest
->setCallbacks([
$callback_url,
]);
}
// Send request.
$di = BrightcoveUtil::getDiApi($this
->getApiClient());
$di
->createIngest($this
->getVideoId(), $this->ingestRequest);
}
}
// Reset changed fields.
$this->changedFields = [];
return $saved;
}