diff --git a/src/lib/UI/Config/Provider/Module/DamWidget.php b/src/lib/UI/Config/Provider/Module/DamWidget.php index 40363e5712..8478b75f4d 100644 --- a/src/lib/UI/Config/Provider/Module/DamWidget.php +++ b/src/lib/UI/Config/Provider/Module/DamWidget.php @@ -14,6 +14,7 @@ use Ibexa\Contracts\Core\Repository\ContentTypeService; use Ibexa\Contracts\Core\Repository\NameSchema\SchemaIdentifierExtractorInterface; use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType; +use Ibexa\Core\Base\Exceptions\NotFoundException; /** * @phpstan-type TConfig array{ @@ -100,10 +101,22 @@ private function getImageConfig(): array { $imageConfig = [ 'showImageFilters' => $this->showImageFilters(), - 'aggregations' => $this->config['image']['aggregations'], + 'aggregations' => [], 'enableMultipleDownload' => extension_loaded('zip'), ]; + // The content type may not have the default fields; in that case, don't add the aggregations + foreach ($this->config['image']['aggregations'] as $key => $aggregation) { + try { + $imageType = $this->contentTypeService->loadContentTypeByIdentifier($aggregation['contentTypeIdentifier']); + if ($imageType->hasFieldDefinition($aggregation['fieldDefinitionIdentifier'])) { + $imageConfig['aggregations'][$key] = $aggregation; + } + } catch (NotFoundException $e) { + $imageConfig['aggregations'][$key] = []; + } + } + $mappings = []; $contentTypeIdentifiers = []; $fieldDefinitionIdentifiers = []; diff --git a/tests/lib/UI/Config/Provider/Module/DamWidgetTest.php b/tests/lib/UI/Config/Provider/Module/DamWidgetTest.php index 758261ebe9..e05cb04143 100644 --- a/tests/lib/UI/Config/Provider/Module/DamWidgetTest.php +++ b/tests/lib/UI/Config/Provider/Module/DamWidgetTest.php @@ -70,8 +70,8 @@ final class DamWidgetTest extends TestCase private const IMAGE_AGGREGATIONS = [ 'KeywordTermAggregation' => [ 'name' => 'keywords', - 'contentTypeIdentifier' => 'keywords', - 'fieldDefinitionIdentifier' => 'keywords', + 'contentTypeIdentifier' => self::IMAGE_FOO_CONTENT_TYPE_IDENTIFIER, + 'fieldDefinitionIdentifier' => 'tags', ], ]; @@ -168,9 +168,14 @@ public function testGetConfigThrowInvalidSearchEngine(): void */ public function provideDataForTestGetConfig(): iterable { + $imageContentType = $this->createContentTypeMock(self::IMAGE_FOO_NAME_SCHEMA); + $imageContentType->expects(self::atLeastOnce()) + ->method('hasFieldDefinition') + ->willReturn(true); + $loadContentTypeValueMap = [ [self::FOLDER_CONTENT_TYPE_IDENTIFIER, [], $this->createContentTypeMock(self::FOLDER_NAME_SCHEMA)], - [self::IMAGE_FOO_CONTENT_TYPE_IDENTIFIER, [], $this->createContentTypeMock(self::IMAGE_FOO_NAME_SCHEMA)], + [self::IMAGE_FOO_CONTENT_TYPE_IDENTIFIER, [], $imageContentType], [self::IMAGE_BAR_CONTENT_TYPE_IDENTIFIER, [], $this->createContentTypeMock(self::IMAGE_BAR_NAME_SCHEMA)], ]; @@ -204,6 +209,9 @@ public function provideDataForTestGetConfig(): iterable ]; } + /** + * @phpstan-return \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType & \PHPUnit\Framework\MockObject\MockObject + */ private function createContentTypeMock(string $nameSchema): ContentType { $contentType = $this->createMock(ContentType::class);