Skip to content

Instantly share code, notes, and snippets.

@georgringer
Created November 29, 2018 10:02
Show Gist options
  • Save georgringer/942c0cd284d54d2b1d7f8ce1e69b5bf0 to your computer and use it in GitHub Desktop.
Save georgringer/942c0cd284d54d2b1d7f8ce1e69b5bf0 to your computer and use it in GitHub Desktop.
<?php
namespace GeorgRinger\News\ViewHelpers\Format;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
/**
* This file is part of the "news" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/
class SortAlphabeticalViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
{
/**
* @var bool
*/
protected $escapeOutput = false;
/**
* Initialize arguments.
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('items', 'mixed', 'items', true);
$this->registerArgument('as', 'string', 'as', true);
}
/**
* Render children but do nothing else
*
*/
public function render()
{
$as = $this->arguments['as'];
$items = $this->arguments['items'];
if ($items instanceof ObjectStorage) {
$items = $items->toArray();
} elseif (!is_array($items)) {
$items = [];
// todo error
}
usort($items, function ($a, $b) {
return mb_strtolower($a->getTitle()) > mb_strtolower($b->getTitle());
});
$this->templateVariableContainer->add($as, $items);
$output = $this->renderChildren();
$this->templateVariableContainer->remove($as);
return $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment