Skip to content

Instantly share code, notes, and snippets.

@rpflamm
Created October 21, 2019 08:11
Show Gist options
  • Save rpflamm/60862131f5f97af29192f6dc540686b4 to your computer and use it in GitHub Desktop.
Save rpflamm/60862131f5f97af29192f6dc540686b4 to your computer and use it in GitHub Desktop.
Own TYPO3 ForViewHelper for array shuffle function
<html
xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
xmlns:my="http://typo3.org/ns/Vendor/Extension/ViewHelpers"
data-namespace-typo3-fluid="true">
<table>
<thead>
<my:for each="{arrayData}" shuffle="true" as="data">
<tr>
<td>{data.fieldOne}</td>
</tr>
</my>
</thead>
</table>
</html>
<?php
namespace Vendor\Extension\ViewHelpers;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
class ForViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
/**
* @var boolean
*/
protected $escapeOutput = false;
/**
* @return void
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('each', 'array', 'The array or \SplObjectStorage to iterated over', true);
$this->registerArgument('as', 'string', 'The name of the iteration variable', true);
$this->registerArgument('key', 'string', 'Variable to assign array key to', false);
$this->registerArgument('reverse', 'boolean', 'If TRUE, iterates in reverse', false, false);
$this->registerArgument('shuffle', 'boolean', 'If TRUE, shuffle the array', false, false);
$this->registerArgument('iteration', 'string', 'The name of the variable to store iteration information (index, cycle, isFirst, isLast, isEven, isOdd)');
}
/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return string
* @throws ViewHelper\Exception
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$templateVariableContainer = $renderingContext->getVariableProvider();
if (!isset($arguments['each'])) {
return '';
}
if (is_object($arguments['each']) && !$arguments['each'] instanceof \Traversable) {
throw new ViewHelper\Exception('ForViewHelper only supports arrays and objects implementing \Traversable interface', 1248728393);
}
if ($arguments['reverse'] === true) {
// array_reverse only supports arrays
if (is_object($arguments['each'])) {
/** @var $each \Traversable */
$each = $arguments['each'];
$arguments['each'] = iterator_to_array($each);
}
$arguments['each'] = array_reverse($arguments['each'], true);
}
if ($arguments['shuffle'] === true) {
// shuffle only supports arrays
if (is_object($arguments['each'])) {
/** @var $each \Traversable */
$each = $arguments['each'];
$arguments['each'] = iterator_to_array($each);
}
shuffle($arguments['each']);
}
if (isset($arguments['iteration'])) {
$iterationData = [
'index' => 0,
'cycle' => 1,
'total' => count($arguments['each'])
];
}
$output = '';
foreach ($arguments['each'] as $keyValue => $singleElement) {
$templateVariableContainer->add($arguments['as'], $singleElement);
if (isset($arguments['key'])) {
$templateVariableContainer->add($arguments['key'], $keyValue);
}
if (isset($arguments['iteration'])) {
$iterationData['isFirst'] = $iterationData['cycle'] === 1;
$iterationData['isLast'] = $iterationData['cycle'] === $iterationData['total'];
$iterationData['isEven'] = $iterationData['cycle'] % 2 === 0;
$iterationData['isOdd'] = !$iterationData['isEven'];
$templateVariableContainer->add($arguments['iteration'], $iterationData);
$iterationData['index']++;
$iterationData['cycle']++;
}
$output .= $renderChildrenClosure();
$templateVariableContainer->remove($arguments['as']);
if (isset($arguments['key'])) {
$templateVariableContainer->remove($arguments['key']);
}
if (isset($arguments['iteration'])) {
$templateVariableContainer->remove($arguments['iteration']);
}
}
return $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment