Skip to content

Commit b5cdaad

Browse files
papcio122alongoszkonradoboza
authored
IBX-10289: Fixed image picker when Image type has no 'tags' field (#1821)
* IBX-10289 Removed aggregations from getImageConfig() when content type has no matching field definition * IBX-10289: using content type id instead of identifier * IBX-10289: updated tests * IBX-10289: use indetifier instead of id * [Tests] Aligned DamWidgetTest with the changes * IBX-10289: remove unused const * Update src/lib/UI/Config/Provider/Module/DamWidget.php formatting fix Co-authored-by: Konrad Oboza <konrad.oboza@ibexa.co> --------- Co-authored-by: Andrew Longosz <alongosz@users.noreply.github.com> Co-authored-by: Konrad Oboza <konrad.oboza@ibexa.co>
1 parent 5859d7e commit b5cdaad

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

src/lib/UI/Config/Provider/Module/DamWidget.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Ibexa\Contracts\Core\Repository\ContentTypeService;
1515
use Ibexa\Contracts\Core\Repository\NameSchema\SchemaIdentifierExtractorInterface;
1616
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
17+
use Ibexa\Core\Base\Exceptions\NotFoundException;
1718

1819
/**
1920
* @phpstan-type TConfig array{
@@ -100,10 +101,24 @@ private function getImageConfig(): array
100101
{
101102
$imageConfig = [
102103
'showImageFilters' => $this->showImageFilters(),
103-
'aggregations' => $this->config['image']['aggregations'],
104+
'aggregations' => [],
104105
'enableMultipleDownload' => extension_loaded('zip'),
105106
];
106107

108+
// The content type may not have the default fields; in that case, don't add the aggregations
109+
foreach ($this->config['image']['aggregations'] as $key => $aggregation) {
110+
try {
111+
$imageType = $this->contentTypeService->loadContentTypeByIdentifier(
112+
$aggregation['contentTypeIdentifier']
113+
);
114+
if ($imageType->hasFieldDefinition($aggregation['fieldDefinitionIdentifier'])) {
115+
$imageConfig['aggregations'][$key] = $aggregation;
116+
}
117+
} catch (NotFoundException $e) {
118+
$imageConfig['aggregations'][$key] = [];
119+
}
120+
}
121+
107122
$mappings = [];
108123
$contentTypeIdentifiers = [];
109124
$fieldDefinitionIdentifiers = [];

tests/lib/UI/Config/Provider/Module/DamWidgetTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ final class DamWidgetTest extends TestCase
7070
private const IMAGE_AGGREGATIONS = [
7171
'KeywordTermAggregation' => [
7272
'name' => 'keywords',
73-
'contentTypeIdentifier' => 'keywords',
74-
'fieldDefinitionIdentifier' => 'keywords',
73+
'contentTypeIdentifier' => self::IMAGE_FOO_CONTENT_TYPE_IDENTIFIER,
74+
'fieldDefinitionIdentifier' => 'tags',
7575
],
7676
];
7777

@@ -168,9 +168,14 @@ public function testGetConfigThrowInvalidSearchEngine(): void
168168
*/
169169
public function provideDataForTestGetConfig(): iterable
170170
{
171+
$imageContentType = $this->createContentTypeMock(self::IMAGE_FOO_NAME_SCHEMA);
172+
$imageContentType->expects(self::atLeastOnce())
173+
->method('hasFieldDefinition')
174+
->willReturn(true);
175+
171176
$loadContentTypeValueMap = [
172177
[self::FOLDER_CONTENT_TYPE_IDENTIFIER, [], $this->createContentTypeMock(self::FOLDER_NAME_SCHEMA)],
173-
[self::IMAGE_FOO_CONTENT_TYPE_IDENTIFIER, [], $this->createContentTypeMock(self::IMAGE_FOO_NAME_SCHEMA)],
178+
[self::IMAGE_FOO_CONTENT_TYPE_IDENTIFIER, [], $imageContentType],
174179
[self::IMAGE_BAR_CONTENT_TYPE_IDENTIFIER, [], $this->createContentTypeMock(self::IMAGE_BAR_NAME_SCHEMA)],
175180
];
176181

@@ -204,6 +209,9 @@ public function provideDataForTestGetConfig(): iterable
204209
];
205210
}
206211

212+
/**
213+
* @phpstan-return \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType & \PHPUnit\Framework\MockObject\MockObject
214+
*/
207215
private function createContentTypeMock(string $nameSchema): ContentType
208216
{
209217
$contentType = $this->createMock(ContentType::class);

0 commit comments

Comments
 (0)