public function BynderService::updateMetadataLastMediaEntities in Bynder 8.2
Same name and namespace in other branches
- 8.3 src/BynderService.php \Drupal\bynder\BynderService::updateMetadataLastMediaEntities()
- 4.0.x src/BynderService.php \Drupal\bynder\BynderService::updateMetadataLastMediaEntities()
Updates metadata of the next N media entities starting at the minimum ID.
Parameters
string|null $minimum_id: (optional) The minimum media entity ID to query items for.
int $limit: (optional) The number of items to update per run.
Return value
array Empty array if updates are not possible. Otherwise, array with the keys:
- updated (a list of updated media entities keyed by the media ID)
- skipped (a list of skipped media entities keyed by the media ID)
- total (the total count of processed media entities)
- max_id (the maximum media entity ID that was last processed)
Overrides BynderServiceInterface::updateMetadataLastMediaEntities
1 call to BynderService::updateMetadataLastMediaEntities()
- BynderService::updateLocalMetadataCron in src/
BynderService.php - Updates the cron metadata information of the local media entities.
File
- src/
BynderService.php, line 179
Class
- BynderService
- Bynder service.
Namespace
Drupal\bynderCode
public function updateMetadataLastMediaEntities($minimum_id = NULL, $limit = BynderService::MAX_ITEMS) {
$bynder_media_types = $this
->getBynderMediaTypes();
if (empty($bynder_media_types)) {
return [];
}
$entity_id_key = $this->mediaStorage
->getEntityType()
->getKey('id');
// Get the Bynder media entity IDs.
$query = $this->mediaStorage
->getQuery()
->accessCheck(FALSE)
->condition($this->mediaStorage
->getEntityType()
->getKey('bundle'), array_keys($bynder_media_types), 'IN')
->sort($entity_id_key)
->range(0, $limit);
if ($minimum_id) {
$query
->condition($entity_id_key, $minimum_id, '>');
}
$media_ids = $query
->execute();
/** @var \Drupal\media\MediaInterface[] $media_entities */
$media_entities = $this->mediaStorage
->loadMultiple($media_ids);
$bynder_media_entities = [];
// Get the remote media UUIDs.
foreach ($media_entities as $media_entity) {
if ($remote_uuid = $media_entity
->getSource()
->getSourceFieldValue($media_entity)) {
$bynder_media_entities[$remote_uuid] = $media_entity;
}
}
// No media entities to process.
if (empty($bynder_media_entities)) {
return [];
}
// Get the most recent metadata for the queried IDs.
$query = [
'ids' => implode(',', array_keys($bynder_media_entities)),
];
try {
$media_list = $this->bynderApi
->getMediaList($query);
} catch (ClientException $e) {
$this->logger
->error($e
->getMessage());
return [];
}
$updated_entities = [];
foreach ($media_list as $index => $item) {
/** @var \Drupal\media\MediaInterface $media_entity */
$media_entity = $bynder_media_entities[$item['id']];
/** @var \Drupal\bynder\Plugin\media\Source\Bynder $source */
$source = $media_entity
->getSource();
$remote_metadata = $source
->filterRemoteMetadata($item);
if ($source
->hasMetadataChanged($media_entity, $remote_metadata)) {
$media_entity
->set(BynderMetadataItem::METADATA_FIELD_NAME, Json::encode($remote_metadata))
->save();
}
$updated_entities[$media_entity
->id()] = $media_entity;
// Remove the processed item.
unset($bynder_media_entities[$item['id']]);
}
$missing_remote_entities = [];
// Log warning in case a media entity has been removed in the remote system.
foreach ($bynder_media_entities as $missing_remote_entity) {
$missing_remote_entities[$missing_remote_entity
->id()] = $missing_remote_entity;
$this->logger
->warning('The media entity (ID: @id, Remote UUID: @remote_uuid) has been removed from the remote system.', [
'@id' => $missing_remote_entity
->id(),
'@remote_uuid' => $missing_remote_entity
->getSource()
->getSourceFieldValue($missing_remote_entity),
]);
}
return [
'updated' => $updated_entities,
'skipped' => $missing_remote_entities,
'total' => count($media_ids),
'max_id' => max($media_ids),
];
}