1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the TYPO3 CMS project. |
7
|
|
|
* |
8
|
|
|
* It is free software; you can redistribute it and/or modify it under |
9
|
|
|
* the terms of the GNU General Public License, either version 2 |
10
|
|
|
* of the License, or any later version. |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please read the |
13
|
|
|
* LICENSE.txt file that was distributed with this source code. |
14
|
|
|
* |
15
|
|
|
* The TYPO3 project - inspiring people to share! |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace ApacheSolrForTypo3\Solr\ViewHelpers\Facet\Options\Group\Prefix; |
19
|
|
|
|
20
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\OptionCollection; |
21
|
|
|
use ApacheSolrForTypo3\Solr\ViewHelpers\AbstractSolrFrontendViewHelper; |
22
|
|
|
use Closure; |
23
|
|
|
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; |
24
|
|
|
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class LabelPrefixesViewHelper |
28
|
|
|
* |
29
|
|
|
* @author Timo Hund <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class LabelPrefixesViewHelper extends AbstractSolrFrontendViewHelper |
32
|
|
|
{ |
33
|
|
|
use CompileWithRenderStatic; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
protected $escapeOutput = false; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Initializes the arguments |
42
|
|
|
*/ |
43
|
|
|
public function initializeArguments() |
44
|
|
|
{ |
45
|
|
|
parent::initializeArguments(); |
46
|
|
|
$this->registerArgument('options', OptionCollection::class, 'The options where prefixed should be available', true); |
47
|
|
|
$this->registerArgument('length', 'int', 'The length of the prefixed that should be retrieved', false, 1); |
48
|
|
|
$this->registerArgument('sortBy', 'string', 'The sorting mode (count,alpha)', false, 'count'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param array $arguments |
53
|
|
|
* @param Closure $renderChildrenClosure |
54
|
|
|
* @param RenderingContextInterface $renderingContext |
55
|
|
|
* @return string |
56
|
|
|
* @noinspection PhpMissingReturnTypeInspection |
57
|
|
|
*/ |
58
|
2 |
|
public static function renderStatic( |
59
|
|
|
array $arguments, |
60
|
|
|
Closure $renderChildrenClosure, |
61
|
|
|
RenderingContextInterface $renderingContext |
62
|
|
|
) { |
63
|
|
|
/** @var $options OptionCollection */ |
64
|
2 |
|
$options = $arguments['options']; |
65
|
2 |
|
$length = $arguments['length'] ?? 1; |
66
|
2 |
|
$sortBy = $arguments['sortBy'] ?? 'count'; |
67
|
2 |
|
$prefixes = $options->getLowercaseLabelPrefixes($length); |
68
|
|
|
|
69
|
2 |
|
$prefixes = static::applySortBy($prefixes, $sortBy); |
70
|
|
|
|
71
|
2 |
|
$templateVariableProvider = $renderingContext->getVariableProvider(); |
72
|
2 |
|
$templateVariableProvider->add('prefixes', $prefixes); |
73
|
2 |
|
$content = $renderChildrenClosure(); |
74
|
2 |
|
$templateVariableProvider->remove('prefixes'); |
75
|
|
|
|
76
|
2 |
|
return $content; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Applies the configured sortBy. |
81
|
|
|
* |
82
|
|
|
* @param array $prefixes |
83
|
|
|
* @param string $sortBy |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
2 |
|
protected static function applySortBy(array $prefixes, string $sortBy = ''): array |
87
|
|
|
{ |
88
|
2 |
|
if ($sortBy === 'alpha') { |
89
|
1 |
|
sort($prefixes); |
90
|
1 |
|
return $prefixes; |
91
|
|
|
} |
92
|
|
|
// count is default |
93
|
1 |
|
return $prefixes; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|