@@ -36,6 +36,7 @@ final class SiteSetSettingsDirective extends BaseDirective
36
36
{
37
37
public const NAME = 'typo3:site-set-settings ' ;
38
38
public const FACET = 'Site Setting ' ;
39
+ public const CATEGORY_FACET = 'Site Setting Category ' ;
39
40
40
41
public function __construct (
41
42
private readonly LoggerInterface $ logger ,
@@ -194,18 +195,21 @@ public function buildConfvalMenu(Directive $directive, array $settings, array $c
194
195
if ($ directive ->getOptionString ('name ' ) !== '' ) {
195
196
$ idPrefix = $ directive ->getOptionString ('name ' ) . '- ' ;
196
197
}
197
- $ categoryArray = [];
198
- foreach ($ categories as $ key => $ category ) {
199
- $ categoryArray [$ key ] = [
200
- 'label ' => $ category ['label ' ] ?? $ categoryLabels [$ key ] ?? '' ,
201
- 'parent ' => $ category ['parent ' ] ?? '' ,
202
- ];
198
+ $ categoryArray = $ this ->buildCategoryArray ($ categories , $ categoryLabels );
199
+ $ rootCategories = [];
200
+ foreach ($ categoryArray as $ key => $ category ) {
201
+ if (isset ($ categoryArray [$ category ['parent ' ]])) {
202
+ assert (is_array ($ categoryArray [$ category ['parent ' ]]['children ' ]));
203
+ $ categoryArray [$ category ['parent ' ]]['children ' ][] = &$ categoryArray [$ key ];
204
+ } else {
205
+ $ rootCategories [] = &$ categoryArray [$ key ];
206
+ }
203
207
}
204
-
205
- $ confvals = [];
206
208
foreach ($ settings as $ key => $ setting ) {
207
- $ confvals [] = $ this ->buildConfval ($ setting , $ idPrefix , $ key , $ directive , $ labels , $ descriptions , $ categoryArray );
209
+ $ confval = $ this ->buildConfval ($ setting , $ idPrefix , $ key , $ directive , $ labels , $ descriptions , $ categoryArray );
210
+ $ this ->assignConfvalsToCategories ($ setting ['category ' ] ?? '' , $ categoryArray , $ confval , $ rootCategories );
208
211
}
212
+ $ confvals = $ this ->buildCategoryConfvals ($ rootCategories , $ idPrefix , $ directive );
209
213
$ reservedParameterNames = [
210
214
'name ' ,
211
215
'class ' ,
@@ -335,4 +339,87 @@ private function getCategoryRootline(array $categoryArray, string $key): string
335
339
return $ this ->getCategoryRootline ($ categoryArray , $ parent ) . ' > ' . $ label ;
336
340
}
337
341
342
+ /**
343
+ * @param array<string, array<string, mixed>> $categories
344
+ * @param array<string, string> $categoryLabels
345
+ * @return array<string, array<string, mixed>>
346
+ */
347
+ public function buildCategoryArray (array $ categories , array $ categoryLabels ): array
348
+ {
349
+ $ categoryArray = [];
350
+ foreach ($ categories as $ key => $ category ) {
351
+ $ categoryArray [$ key ] = [
352
+ 'label ' => $ category ['label ' ] ?? $ categoryLabels [$ key ] ?? '' ,
353
+ 'parent ' => $ category ['parent ' ] ?? '' ,
354
+ 'key ' => $ key ,
355
+ 'confvals ' => [],
356
+ 'children ' => [],
357
+ ];
358
+ }
359
+ return $ categoryArray ;
360
+ }
361
+
362
+ /**
363
+ * @param array<string, array<string, mixed>> $categoryArray
364
+ * @param array<array<string, mixed>> $rootCategories
365
+ */
366
+ public function assignConfvalsToCategories (string $ category , array &$ categoryArray , ConfvalNode $ confval , array &$ rootCategories ): void
367
+ {
368
+ if (is_array ($ categoryArray [$ category ]['confvals ' ] ?? false )) {
369
+ $ categoryArray [$ category ]['confvals ' ][] = $ confval ;
370
+ } else {
371
+ $ categoryArray [$ category ] = [
372
+ 'label ' => '' ,
373
+ 'parent ' => '' ,
374
+ 'key ' => $ category ,
375
+ 'children ' => [],
376
+ 'confvals ' => [$ confval ],
377
+ ];
378
+ $ rootCategories [] = &$ categoryArray [$ category ];
379
+ }
380
+ }
381
+
382
+ /**
383
+ * @param array<array<string, mixed>> $categories
384
+ * @return ConfvalNode[]
385
+ */
386
+ private function buildCategoryConfvals (array $ categories , string $ idPrefix , Directive $ directive ): array
387
+ {
388
+ if ($ categories === []) {
389
+ return [];
390
+ }
391
+ $ confvals = [];
392
+ foreach ($ categories as $ category ) {
393
+ $ children = [];
394
+ if (is_array ($ category ['children ' ] ?? false )) {
395
+ $ children = $ this ->buildCategoryConfvals ($ category ['children ' ], $ idPrefix , $ directive );
396
+ }
397
+ $ key = $ category ['key ' ];
398
+ if ($ key === '' ) {
399
+ $ key = '_global ' ;
400
+ }
401
+ assert (is_string ($ key ));
402
+ $ additionalFields = [];
403
+ $ additionalFields ['searchFacet ' ] = new InlineCompoundNode ([new PlainTextInlineNode (self ::CATEGORY_FACET )]);
404
+
405
+ $ label = $ category ['label ' ];
406
+ if ($ label !== '' ) {
407
+ assert (is_string ($ label ));
408
+ $ additionalFields ['Label ' ] = new InlineCompoundNode ([new PlainTextInlineNode ($ label )]);
409
+ }
410
+ assert (is_array ($ category ['confvals ' ]));
411
+ $ confvals [] = new ConfvalNode (
412
+ $ this ->anchorNormalizer ->reduceAnchor ($ idPrefix . 'category- ' . $ key ),
413
+ $ key ,
414
+ null ,
415
+ false ,
416
+ null ,
417
+ $ additionalFields ,
418
+ array_merge ($ children , $ category ['confvals ' ]),
419
+ $ directive ->getOptionBool ('noindex ' ),
420
+ );
421
+ }
422
+ return $ confvals ;
423
+ }
424
+
338
425
}
0 commit comments