public function BynderService::updateMetadataLastMediaEntities in Bynder 4.0.x
Same name and namespace in other branches
- 8.3 src/BynderService.php \Drupal\bynder\BynderService::updateMetadataLastMediaEntities()
- 8.2 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 190
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 [];
}
$updated_entities = $this
->updateMediaEntities($bynder_media_entities);
$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),
];
}