Skip to content

Instantly share code, notes, and snippets.

@georgringer
Created November 29, 2018 10:02
Show Gist options
  • Select an option

  • Save georgringer/942c0cd284d54d2b1d7f8ce1e69b5bf0 to your computer and use it in GitHub Desktop.

Select an option

Save georgringer/942c0cd284d54d2b1d7f8ce1e69b5bf0 to your computer and use it in GitHub Desktop.

Revisions

  1. georgringer created this gist Nov 29, 2018.
    58 changes: 58 additions & 0 deletions SortAlphabeticalViewHelper.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    <?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;
    }
    }