Skip to content

Commit fc0a1e8

Browse files
authored
[FEATURE] Offer sorting options for viewhelper arguments (#662)
If option `:sortBy: name` or not sortBy is set, sort by name. Otherwise the sorting from the json is preserved. resolves #660
1 parent 69c6197 commit fc0a1e8

File tree

4 files changed

+175
-11
lines changed

4 files changed

+175
-11
lines changed

packages/typo3-docs-theme/src/Directives/ViewHelperDirective.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,21 @@ public function processNode(
8787
return $this->getErrorNode();
8888
}
8989

90+
$sortBy = $directive->getOptionString('sortBy', 'name');
91+
9092
$noindex = $directive->getOptionBool('noindex');
9193

9294
$data = $json['viewHelpers'][$directive->getData()];
9395
$viewHelperNode = $this->getViewHelperNode($data, $blockContext, $noindex);
9496
$arguments = [];
9597
foreach ($json['viewHelpers'][$directive->getData()]['argumentDefinitions'] ?? [] as $argumentDefinition) {
9698
if (is_array($argumentDefinition)) {
97-
$arguments[] = $this->getArgument($argumentDefinition, $viewHelperNode, $noindex);
99+
$arguments[$this->getString($argumentDefinition, 'name')] = $this->getArgument($argumentDefinition, $viewHelperNode, $noindex);
98100
}
99101
}
102+
if ($sortBy === 'name') {
103+
ksort($arguments);
104+
}
100105
$viewHelperNode->setArguments($arguments);
101106
$viewHelperNode->setValue($arguments);
102107

tests/Integration/tests/viewhelper/expected/index.html

Lines changed: 158 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,48 @@ <h2>Arguments</h2>
9898

9999
<p>The following arguments are available for the split ViewHelper: </p>
100100
<dl class="confval">
101+
<dt id="viewhelper-argument-typo3fluid-fluid-viewhelpers-splitviewhelper-limit" class="d-flex justify-content-between">
102+
<div class="confval-header">
103+
<code class="sig-name descname"><span class="pre">limit</span></code>
104+
<a class="headerlink" href="#viewhelper-argument-typo3fluid-fluid-viewhelpers-splitviewhelper-limit" data-bs-toggle="modal" data-bs-target="#linkReferenceModal" data-id="viewhelper-argument-typo3fluid-fluid-viewhelpers-splitviewhelper-limit" data-rstCode=":typo3:viewhelper-argument:`limit &lt;somemanual:typo3fluid-fluid-viewhelpers-splitviewhelper-limit&gt;`" title="Reference this configuration value"></a>
105+
</div>
106+
<div class="confval-back-to-top"> <a href="#" class="backToTop" title="Back to top"><i class="fa-solid fa-arrow-up fa-xs"></i></a> </div>
107+
</dt>
108+
<dd>
109+
<dl class="field-list simple">
110+
<dt class="field">Type</dt>
111+
<dd class="field">int
112+
</dd>
113+
<dt class="field">Default</dt>
114+
<dd class="field">0
115+
</dd>
116+
</dl>
117+
<div class="confval-description">
118+
If limit is positive, a maximum of $limit items will be returned. If limit is negative, all items except for the last $limit items will be returned. 0 will be treated as 1.
119+
</div>
120+
</dd>
121+
</dl> <dl class="confval">
122+
<dt id="viewhelper-argument-typo3fluid-fluid-viewhelpers-splitviewhelper-separator" class="d-flex justify-content-between">
123+
<div class="confval-header">
124+
<code class="sig-name descname"><span class="pre">separator</span></code>
125+
<a class="headerlink" href="#viewhelper-argument-typo3fluid-fluid-viewhelpers-splitviewhelper-separator" data-bs-toggle="modal" data-bs-target="#linkReferenceModal" data-id="viewhelper-argument-typo3fluid-fluid-viewhelpers-splitviewhelper-separator" data-rstCode=":typo3:viewhelper-argument:`separator &lt;somemanual:typo3fluid-fluid-viewhelpers-splitviewhelper-separator&gt;`" title="Reference this configuration value"></a>
126+
</div>
127+
<div class="confval-back-to-top"> <a href="#" class="backToTop" title="Back to top"><i class="fa-solid fa-arrow-up fa-xs"></i></a> </div>
128+
</dt>
129+
<dd>
130+
<dl class="field-list simple">
131+
<dt class="field">Type</dt>
132+
<dd class="field">string
133+
</dd>
134+
<dt class="field">Required</dt>
135+
<dd class="field">1
136+
</dd>
137+
</dl>
138+
<div class="confval-description">
139+
Separator string to explode with
140+
</div>
141+
</dd>
142+
</dl> <dl class="confval">
101143
<dt id="viewhelper-argument-typo3fluid-fluid-viewhelpers-splitviewhelper-value" class="d-flex justify-content-between">
102144
<div class="confval-header">
103145
<code class="sig-name descname"><span class="pre">value</span></code>
@@ -118,11 +160,123 @@ <h2>Arguments</h2>
118160
The string to explode
119161
</div>
120162
</dd>
163+
</dl>
164+
165+
166+
<p>The SplitViewHelper splits a string by the specified separator, which
167+
results in an array. The number of values in the resulting array can
168+
be limited with the limit parameter, which results in an array where
169+
the last item contains the remaining unsplit string.
170+
This ViewHelper mimicks PHP&#039;s <code class="code-inline" translate="no" aria-description="PHP" aria-details="Dynamic server-side scripting language.">explode()</code> function.</p>
171+
<section class="section" id="examples">
172+
<h2>Examples<a class="headerlink" href="#examples" data-bs-toggle="modal" data-bs-target="#linkReferenceModal" title="Reference this headline"></a></h2>
173+
<section class="section" id="split-with-a-separator">
174+
<h3>Split with a separator<a class="headerlink" href="#split-with-a-separator" data-bs-toggle="modal" data-bs-target="#linkReferenceModal" title="Reference this headline"></a></h3>
175+
<div class="code-block-wrapper" translate="no">
176+
177+
<pre class="code-block"><code>&lt;f:split value=&quot;1,5,8&quot; separator=&quot;,&quot; /&gt;</code></pre>
178+
<div> <button class="code-block-copy" title="Copy to clipboard">
179+
<i class="icon fa-regular fa-copy code-block-copy-icon"></i>
180+
<i class="fa-solid fa-check code-block-check-icon code-block-hide"></i>
181+
</button>
182+
<span class="code-block-check-tooltip code-block-hide" aria-live="polite">Copied!</span>
183+
</div>
184+
</div>
185+
<div class="code-block-wrapper" translate="no">
186+
187+
<pre class="code-block"><code class="hljs plaintext">{0: '1', 1: '5', 2: '8'}</code></pre>
188+
<div> <button class="code-block-copy" title="Copy to clipboard">
189+
<i class="icon fa-regular fa-copy code-block-copy-icon"></i>
190+
<i class="fa-solid fa-check code-block-check-icon code-block-hide"></i>
191+
</button>
192+
<span class="code-block-check-tooltip code-block-hide" aria-live="polite">Copied!</span>
193+
</div>
194+
</div>
195+
</section>
196+
<section class="section" id="split-using-tag-content-as-value">
197+
<h3>Split using tag content as value<a class="headerlink" href="#split-using-tag-content-as-value" data-bs-toggle="modal" data-bs-target="#linkReferenceModal" title="Reference this headline"></a></h3>
198+
<div class="code-block-wrapper" translate="no">
199+
200+
<pre class="code-block"><code>&lt;f:split separator=&quot;-&quot;&gt;1-5-8&lt;/f:split&gt;</code></pre>
201+
<div> <button class="code-block-copy" title="Copy to clipboard">
202+
<i class="icon fa-regular fa-copy code-block-copy-icon"></i>
203+
<i class="fa-solid fa-check code-block-check-icon code-block-hide"></i>
204+
</button>
205+
<span class="code-block-check-tooltip code-block-hide" aria-live="polite">Copied!</span>
206+
</div>
207+
</div>
208+
<div class="code-block-wrapper" translate="no">
209+
210+
<pre class="code-block"><code class="hljs plaintext">{0: '1', 1: '5', 2: '8'}</code></pre>
211+
<div> <button class="code-block-copy" title="Copy to clipboard">
212+
<i class="icon fa-regular fa-copy code-block-copy-icon"></i>
213+
<i class="fa-solid fa-check code-block-check-icon code-block-hide"></i>
214+
</button>
215+
<span class="code-block-check-tooltip code-block-hide" aria-live="polite">Copied!</span>
216+
</div>
217+
</div>
218+
</section>
219+
<section class="section" id="split-with-a-limit">
220+
<h3>Split with a limit<a class="headerlink" href="#split-with-a-limit" data-bs-toggle="modal" data-bs-target="#linkReferenceModal" title="Reference this headline"></a></h3>
221+
<div class="code-block-wrapper" translate="no">
222+
223+
<pre class="code-block"><code>&lt;f:split value=&quot;1,5,8&quot; separator=&quot;,&quot; limit=&quot;2&quot; /&gt;</code></pre>
224+
<div> <button class="code-block-copy" title="Copy to clipboard">
225+
<i class="icon fa-regular fa-copy code-block-copy-icon"></i>
226+
<i class="fa-solid fa-check code-block-check-icon code-block-hide"></i>
227+
</button>
228+
<span class="code-block-check-tooltip code-block-hide" aria-live="polite">Copied!</span>
229+
</div>
230+
</div>
231+
<div class="code-block-wrapper" translate="no">
232+
233+
<pre class="code-block"><code class="hljs plaintext">{0: '1', 1: '5,8'}</code></pre>
234+
<div> <button class="code-block-copy" title="Copy to clipboard">
235+
<i class="icon fa-regular fa-copy code-block-copy-icon"></i>
236+
<i class="fa-solid fa-check code-block-check-icon code-block-hide"></i>
237+
</button>
238+
<span class="code-block-check-tooltip code-block-hide" aria-live="polite">Copied!</span>
239+
</div>
240+
</div>
241+
</section>
242+
</section>
243+
244+
<h2>Source code</h2>
245+
246+
<p>
247+
Go to the source code of this ViewHelper:
248+
<a href="https://github.com/TYPO3/Fluid/blob/main/src/ViewHelpers/SplitViewHelper.php">SplitViewHelper.php (GitHub)</a>.
249+
</p>
250+
251+
<h2>Arguments</h2>
252+
253+
<p>The following arguments are available for the split ViewHelper: </p>
254+
<dl class="confval">
255+
<dt class="d-flex justify-content-between">
256+
<div class="confval-header">
257+
<code class="sig-name descname"><span class="pre">value</span></code>
258+
<span class="headerNoindex" title="This configuration value cannot be linked directly. Consider to link to the section above."><i class="fa-solid fa-link-slash"></i></span>
259+
</div>
260+
<div class="confval-back-to-top"> <a href="#" class="backToTop" title="Back to top"><i class="fa-solid fa-arrow-up fa-xs"></i></a> </div>
261+
</dt>
262+
<dd>
263+
<dl class="field-list simple">
264+
<dt class="field">Type</dt>
265+
<dd class="field">string
266+
</dd>
267+
<dt class="field">Default</dt>
268+
<dd class="field">&#039;&#039;
269+
</dd>
270+
</dl>
271+
<div class="confval-description">
272+
The string to explode
273+
</div>
274+
</dd>
121275
</dl> <dl class="confval">
122-
<dt id="viewhelper-argument-typo3fluid-fluid-viewhelpers-splitviewhelper-separator" class="d-flex justify-content-between">
276+
<dt class="d-flex justify-content-between">
123277
<div class="confval-header">
124278
<code class="sig-name descname"><span class="pre">separator</span></code>
125-
<a class="headerlink" href="#viewhelper-argument-typo3fluid-fluid-viewhelpers-splitviewhelper-separator" data-bs-toggle="modal" data-bs-target="#linkReferenceModal" data-id="viewhelper-argument-typo3fluid-fluid-viewhelpers-splitviewhelper-separator" data-rstCode=":typo3:viewhelper-argument:`separator &lt;somemanual:typo3fluid-fluid-viewhelpers-splitviewhelper-separator&gt;`" title="Reference this configuration value"></a>
279+
<span class="headerNoindex" title="This configuration value cannot be linked directly. Consider to link to the section above."><i class="fa-solid fa-link-slash"></i></span>
126280
</div>
127281
<div class="confval-back-to-top"> <a href="#" class="backToTop" title="Back to top"><i class="fa-solid fa-arrow-up fa-xs"></i></a> </div>
128282
</dt>
@@ -140,10 +294,10 @@ <h2>Arguments</h2>
140294
</div>
141295
</dd>
142296
</dl> <dl class="confval">
143-
<dt id="viewhelper-argument-typo3fluid-fluid-viewhelpers-splitviewhelper-limit" class="d-flex justify-content-between">
297+
<dt class="d-flex justify-content-between">
144298
<div class="confval-header">
145299
<code class="sig-name descname"><span class="pre">limit</span></code>
146-
<a class="headerlink" href="#viewhelper-argument-typo3fluid-fluid-viewhelpers-splitviewhelper-limit" data-bs-toggle="modal" data-bs-target="#linkReferenceModal" data-id="viewhelper-argument-typo3fluid-fluid-viewhelpers-splitviewhelper-limit" data-rstCode=":typo3:viewhelper-argument:`limit &lt;somemanual:typo3fluid-fluid-viewhelpers-splitviewhelper-limit&gt;`" title="Reference this configuration value"></a>
300+
<span class="headerNoindex" title="This configuration value cannot be linked directly. Consider to link to the section above."><i class="fa-solid fa-link-slash"></i></span>
147301
</div>
148302
<div class="confval-back-to-top"> <a href="#" class="backToTop" title="Back to top"><i class="fa-solid fa-arrow-up fa-xs"></i></a> </div>
149303
</dt>

tests/Integration/tests/viewhelper/expected/objects.inv.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,23 @@
6767
]
6868
},
6969
"typo3:viewhelper-argument": {
70-
"typo3fluid-fluid-viewhelpers-splitviewhelper-value": [
70+
"typo3fluid-fluid-viewhelpers-splitviewhelper-limit": [
7171
"-",
7272
"-",
73-
"index.html#typo3fluid-fluid-viewhelpers-splitviewhelper-value",
74-
"value"
73+
"index.html#typo3fluid-fluid-viewhelpers-splitviewhelper-limit",
74+
"limit"
7575
],
7676
"typo3fluid-fluid-viewhelpers-splitviewhelper-separator": [
7777
"-",
7878
"-",
7979
"index.html#typo3fluid-fluid-viewhelpers-splitviewhelper-separator",
8080
"separator"
8181
],
82-
"typo3fluid-fluid-viewhelpers-splitviewhelper-limit": [
82+
"typo3fluid-fluid-viewhelpers-splitviewhelper-value": [
8383
"-",
8484
"-",
85-
"index.html#typo3fluid-fluid-viewhelpers-splitviewhelper-limit",
86-
"limit"
85+
"index.html#typo3fluid-fluid-viewhelpers-splitviewhelper-value",
86+
"value"
8787
],
8888
"typo3-cms-fluid-viewhelpers-link-externalviewhelper-uri": [
8989
"-",

tests/Integration/tests/viewhelper/input/index.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ f:split
1111
.. typo3:viewhelper:: split
1212
:source: resources/global_viewhelpers_demo.json
1313

14+
.. typo3:viewhelper:: split
15+
:source: resources/global_viewhelpers_demo.json
16+
:sortBy: json
17+
:noindex:
18+
1419
f:link.external
1520
===============
1621

0 commit comments

Comments
 (0)