diff --git a/modules/acquiadam_asset_import/src/Services/AssetQueueService.php b/modules/acquiadam_asset_import/src/Services/AssetQueueService.php index 162d512feda84771ec5f35dc433fc99ff1895bb6..cca6b0eff894bbc75664b84dc62e42d81fd7093d 100644 --- a/modules/acquiadam_asset_import/src/Services/AssetQueueService.php +++ b/modules/acquiadam_asset_import/src/Services/AssetQueueService.php @@ -170,10 +170,10 @@ class AssetQueueService implements AssetQueueInterface { $response = []; switch ($source) { case 'categories': - $response = $this->fetchAssets($source, $source_uuid); + $response = $this->fetchAssets($source, $source_uuid, $this->getFilters($bundles)); break; case 'asset_groups': - $response = $this->fetchAssets($source, $source_uuid); + $response = $this->fetchAssets($source, $source_uuid, $this->getFilters($bundles)); break; default: $this->damLoggerChannel->error('Invalid source type: %source', [ @@ -208,19 +208,21 @@ class AssetQueueService implements AssetQueueInterface { * The category or asset group. * @param string $uuid * The list UUID. + * @param string|null $filters + * The filter value. * * @return array|null * The response array or NULL on failure. */ - private function fetchAssets(string $type, string $uuid): ?array { + private function fetchAssets(string $type, string $uuid, string $filters = ''): ?array { try { $response = []; switch ($type) { case 'categories': - $response = $this->userClientFactory->getSiteClient()->getAssetsInCategory($uuid); + $response = $this->userClientFactory->getSiteClient()->getAssetsInCategory($uuid, $filters); break; case 'asset_groups': - $response = $this->userClientFactory->getSiteClient()->getAssetsInAssetGroup($uuid); + $response = $this->userClientFactory->getSiteClient()->getAssetsInAssetGroup($uuid, $filters); break; } @@ -257,7 +259,7 @@ class AssetQueueService implements AssetQueueInterface { return NULL; } - $asset_media_type = $this->mediaTypeResolver->resolve($asset_data); + $asset_media_type = $this->mediaTypeResolver->resolve($asset_data, $bundles); if ($asset_media_type === NULL) { return NULL; } @@ -333,4 +335,38 @@ class AssetQueueService implements AssetQueueInterface { return array_diff($asset_ids, $existing_uuids); } + /** + * Builds and returns a filter string based on the provided media bundles. + * + * @param array $bundles + * An array of media type IDs. + * + * @return string + * A filter string for asset queries. + */ + public function getFilters(array $bundles): string { + $filters = []; + + foreach ($bundles as $bundle) { + $media_type = $this->entityTypeManager->getStorage('media_type')->load($bundle); + if ($media_type) { + $media_source = $media_type->getSource(); + $plugin_definition = $media_source->getPluginDefinition(); + $asset_search_key = $plugin_definition['asset_search_key'] ?? null; + $asset_search_value = $plugin_definition['asset_search_value'] ?? null; + + if ($asset_search_key && $asset_search_value) { + $filters[$asset_search_key][] = $asset_search_value; + } + } + } + + $filter_parts = []; + foreach ($filters as $key => $values) { + $filter_parts[] = sprintf('%s:(%s)', $key, implode(' or ', $values)); + } + + return implode(' or ', $filter_parts); + } + } diff --git a/src/Client/AcquiaDamClient.php b/src/Client/AcquiaDamClient.php index 289938c04ea34a89be9c9435188e88e3c37e6df7..4104f66c0019f81a9d6696b1c5ed0197165f1b41 100644 --- a/src/Client/AcquiaDamClient.php +++ b/src/Client/AcquiaDamClient.php @@ -258,14 +258,16 @@ class AcquiaDamClient extends Client { * * @param string $category_uuid * Widen's UUID of the category itself. + * @param string $filters + * The filters. * * @return array * The results. */ - public function getAssetsInCategory(string $category_uuid) { + public function getAssetsInCategory(string $category_uuid, string $filters = ''): array { $category_name = $this->convertCategoryUuidToName($category_uuid); - return $this->search("cat:($category_name)"); + return $this->search("cat:($category_name) $filters"); } /** diff --git a/src/MediaTypeResolver.php b/src/MediaTypeResolver.php index a4d1b634759efe36ee50a7b53327f48ad0362875..f653d95bad945a4b38a0107615dcc91080dc4450 100644 --- a/src/MediaTypeResolver.php +++ b/src/MediaTypeResolver.php @@ -36,6 +36,8 @@ final class MediaTypeResolver { * * @param array $asset * The asset data. + * @param array $bundle + * The media type list. * * @return \Drupal\media\MediaTypeInterface|null * The media type, or NULL if one cannot be resolved. @@ -43,7 +45,7 @@ final class MediaTypeResolver { * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException */ - public function resolve(array $asset): ?MediaTypeInterface { + public function resolve(array $asset, array $bundles = []): ?MediaTypeInterface { $result = NULL; if (!isset($asset['file_properties'])) { @@ -66,7 +68,7 @@ final class MediaTypeResolver { // Compare the data type the actual source plugin can handle vs. the type // of the current asset in question. - if ($definition['asset_search_value'] === $property_value) { + if ($definition['asset_search_value'] === $property_value && (empty($bundles) || in_array($media_type->id(), $bundles))) { $result = $media_type; break; }