sf_event_mgt icon indicating copy to clipboard operation
sf_event_mgt copied to clipboard

Better support of multiple categories in overwriteDemand.category

Open georgringer opened this issue 1 year ago • 1 comments

By providing just some very basic ViewHelpers it is possible to improve the developer experience a lot regarding setting multiple categories in the template.

All it would need are 3 VH and a little bit template magic. I propose to add the ViewHelpers to the extension and the template to docs as snippet.

<theme:events.inList list="{overwriteDemand.category}" item="{category.uid}">
      <f:then>
        <f:link.action
          action="list"
          controller="Event"
          arguments="{overwriteDemand:{category: '{theme:events.removeCategoryFromList(list:overwriteDemand.category,item:category.uid)}'}}"
          class="tag tag-{color}"
          section="c{contentObjectData.uid}"
          >{category.title}</f:link.action
        >
      </f:then>
      <f:else>
        <f:link.action
          action="list"
          controller="Event"
          arguments="{overwriteDemand:{category: '{theme:events.addCategoryToList(list:overwriteDemand.category,item:category.uid)}'}}"
          class="tag tag-{color}-inverted"
          section="c{contentObjectData.uid}"
          >{category.title}</f:link.action
        >
      </f:else>
    </theme:events.inList>
<?php

declare(strict_types=1);

namespace StudioMitte\Theme\ViewHelpers\Events;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

class AddCategoryToListViewHelper extends AbstractViewHelper
{
    use CompileWithRenderStatic;

    protected $escapeOutput = false;

    public function initializeArguments() // @phpstan-ignore-line
    {
        $this->registerArgument('list', 'string', 'list of ids', true);
        $this->registerArgument('item', 'int', 'id', true);
    }

    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)// @phpstan-ignore-line
    {
        $items = GeneralUtility::intExplode(',', (string)($arguments['list'] ?? ''));
        $items[] = (int)($arguments['item'] ?? 0);
        return implode(',', $items);
    }
}

<?php

declare(strict_types=1);

namespace StudioMitte\Theme\ViewHelpers\Events;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractConditionViewHelper;

class InListViewHelper extends AbstractConditionViewHelper
{
    public function initializeArguments()// @phpstan-ignore-line
    {
        parent::initializeArguments();
        $this->registerArgument('list', 'string', 'list of ids', true);
        $this->registerArgument('item', 'int', 'id', true);
    }

    /**
     * @param array $arguments
     * @param RenderingContextInterface $renderingContext
     * @return bool
     */
    public static function verdict(array $arguments, RenderingContextInterface $renderingContext)// @phpstan-ignore-line
    {
        return GeneralUtility::inList((string)($arguments['list'] ?? ''), (string)($arguments['item'] ?? ''));
    }
}

<?php

declare(strict_types=1);

namespace StudioMitte\Theme\ViewHelpers\Events;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

class RemoveCategoryFromListViewHelper extends AbstractViewHelper
{
    use CompileWithRenderStatic;

    protected $escapeOutput = false;

    public function initializeArguments() // @phpstan-ignore-line
    {
        $this->registerArgument('list', 'string', 'list of ids', true);
        $this->registerArgument('item', 'int', 'id', true);
    }

    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)// @phpstan-ignore-line
    {
        $items = GeneralUtility::intExplode(',', (string)($arguments['list'] ?? ''));
        $element = (int)($arguments['item'] ?? 0);
        foreach ($items as $k => $v) {
            if ($v === $element) {
                unset($items[$k]);
            }
        }
        return implode(',', $items);
    }
}

georgringer avatar Sep 21 '22 06:09 georgringer

Sounds good. Namespace should be DERHANSEN\SfEventMgt\ViewHelpers\List and the ViewHelpers can then be named more generic (and short) as shown below:

  • AddItemViewHelper
  • RemoveItemViewHelper
  • InListViewHelper

derhansen avatar Sep 21 '22 08:09 derhansen