sf_event_mgt
sf_event_mgt copied to clipboard
Better support of multiple categories in overwriteDemand.category
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);
}
}
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