public function BrightcovePlaylist::save in Brightcove Video Connect 8
Same name and namespace in other branches
- 8.2 src/Entity/BrightcovePlaylist.php \Drupal\brightcove\Entity\BrightcovePlaylist::save()
- 3.x src/Entity/BrightcovePlaylist.php \Drupal\brightcove\Entity\BrightcovePlaylist::save()
Parameters
bool $upload: Whether to upload the video to Brightcove or not.
Overrides EntityBase::save
File
- src/
Entity/ BrightcovePlaylist.php, line 233
Class
- BrightcovePlaylist
- Defines the Brightcove Playlist.
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();
if ($upload) {
$cms = BrightcoveUtil::getCmsApi($this
->getApiClient());
// Setup playlist object and set minimum required values.
$playlist = new Playlist();
$playlist
->setName($this
->getName());
// Save or update type if needed.
if ($this
->isFieldChanged('type')) {
$playlist
->setType($this
->getType());
// Unset search if the playlist is manual.
if ($playlist
->getType() == 'EXPLICIT') {
$playlist
->setSearch('');
$this
->setTags([]);
}
else {
$playlist
->setVideoIds([]);
$this
->setVideos([]);
}
}
// Save or update description if needed.
if ($this
->isFieldChanged('description')) {
$playlist
->setDescription($this
->getDescription());
}
// Save or update reference ID if needed.
if ($this
->isFieldChanged('reference_id')) {
$playlist
->setReferenceId($this
->getReferenceId());
}
// Save or update search if needed.
if ($this
->isFieldChanged('tags') || $this
->isFieldChanged('tags_search_condition')) {
$condition = '';
if ($this
->getTagsSearchCondition() == self::TAG_SEARCH_CONTAINS_ALL) {
$condition = '+';
}
$tags = '';
if (!empty($tag_items = $this
->getTags())) {
if (count($tag_items) == 1) {
$this
->setTagsSearchCondition(self::TAG_SEARCH_CONTAINS_ALL);
}
$tags .= $condition . 'tags:"';
$first = TRUE;
foreach ($tag_items as $tag) {
$tag_term = Term::load($tag['target_id']);
$tags .= ($first ? '' : '","') . $tag_term
->getName();
if ($first) {
$first = FALSE;
}
}
$tags .= '"';
}
$playlist
->setSearch($tags);
}
// Save or update videos list if needed.
if ($this
->isFieldChanged('videos')) {
$video_entities = $this
->getVideos();
$videos = [];
foreach ($video_entities as $video) {
$videos[] = BrightcoveVideo::load($video['target_id'])
->getVideoId();
}
$playlist
->setVideoIds($videos);
}
// Create or update a playlist.
switch ($status) {
case SAVED_NEW:
// Create new playlist on Brightcove.
$saved_playlist = $cms
->createPlaylist($playlist);
// Set the rest of the fields on BrightcoveVideo entity.
$this
->setPlaylistId($saved_playlist
->getId());
$this
->setCreatedTime(strtotime($saved_playlist
->getCreatedAt()));
break;
case SAVED_UPDATED:
// Set playlist ID.
$playlist
->setId($this
->getPlaylistId());
// Update playlist.
$saved_playlist = $cms
->updatePlaylist($playlist);
break;
}
// Update changed time and playlist entity with the video ID.
if (isset($saved_playlist)) {
$this
->setChangedTime(strtotime($saved_playlist
->getUpdatedAt()));
// Save the entity again to save some new values which are only
// available after creating/updating the playlist on Brightcove.
// Also don't change the save state to show the correct message when
// the entity is created or updated.
parent::save();
}
}
return $saved;
}