public static function BrightcoveUtil::saveOrUpdateTags in Brightcove Video Connect 8
Same name and namespace in other branches
- 8.2 src/BrightcoveUtil.php \Drupal\brightcove\BrightcoveUtil::saveOrUpdateTags()
- 3.x src/BrightcoveUtil.php \Drupal\brightcove\BrightcoveUtil::saveOrUpdateTags()
Helper function to save or update tags.
Parameters
\Drupal\brightcove\BrightcoveVideoPlaylistCMSEntityInterface $entity: Video or playlist entity.
string $api_client_id: API Client ID.
array $tags: The list of tags from brightcove.
2 calls to BrightcoveUtil::saveOrUpdateTags()
- BrightcovePlaylist::createOrUpdate in src/
Entity/ BrightcovePlaylist.php - Create or update an existing playlist from a Brightcove Playlist object.
- BrightcoveVideo::createOrUpdate in src/
Entity/ BrightcoveVideo.php - Create or update an existing video from a Brightcove Video object.
File
- src/
BrightcoveUtil.php, line 417
Class
- BrightcoveUtil
- Utility class for Brightcove.
Namespace
Drupal\brightcoveCode
public static function saveOrUpdateTags(BrightcoveVideoPlaylistCMSEntityInterface $entity, $api_client_id, array $tags = []) {
$entity_tags = [];
$video_entity_tags = $entity
->getTags();
foreach ($video_entity_tags as $index => $tag) {
/** @var \Drupal\taxonomy\Entity\Term $term */
$term = Term::load($tag['target_id']);
if (!is_null($term)) {
$entity_tags[$term
->id()] = $term
->getName();
}
else {
unset($video_entity_tags[$index]);
$entity
->setTags($video_entity_tags);
}
}
if (array_values($entity_tags) != $tags) {
// Remove deleted tags from the video.
if (!empty($entity
->id())) {
$tags_to_remove = array_diff($entity_tags, $tags);
foreach (array_keys($tags_to_remove) as $entity_id) {
unset($entity_tags[$entity_id]);
}
}
// Add new tags.
$new_tags = array_diff($tags, $entity_tags);
$entity_tags = array_keys($entity_tags);
foreach ($new_tags as $tag) {
$taxonomy_term = NULL;
$existing_tags = \Drupal::entityQuery('taxonomy_term')
->condition('vid', BrightcoveVideo::TAGS_VID)
->condition('name', $tag)
->execute();
// Create new Taxonomy term item.
if (empty($existing_tags)) {
$values = [
'name' => $tag,
'vid' => BrightcoveVideo::TAGS_VID,
'brightcove_api_client' => [
'target_id' => $api_client_id,
],
];
$taxonomy_term = Term::create($values);
$taxonomy_term
->save();
}
$entity_tags[] = isset($taxonomy_term) ? $taxonomy_term
->id() : reset($existing_tags);
}
$entity
->setTags($entity_tags);
}
}