Files

102 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Fluid\ViewHelpers\Be;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Imaging\IconSize;
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
/**
* ViewHelper for rendering a styled content infobox markup.
*
* ```
* <f:be.infobox title="Message title">your box content</f:be.infobox>
* <f:be.infobox title="Error!" state="{f:constant(name: 'TYPO3\CMS\Core\Type\ContextualFeedbackSeverity::ERROR')}" iconName="check">your box content</f:be.infobox>
* ```
*
* @see https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-be-infobox
*/
final class InfoboxViewHelper extends AbstractViewHelper
{
/**
* As this ViewHelper renders HTML, the output must not be escaped.
*
* @var bool
*/
protected $escapeOutput = false;
public function __construct(
private readonly IconFactory $iconFactory
) {}
public function initializeArguments(): void
{
$this->registerArgument('message', 'string', 'The message of the info box, if NULL tag content is used');
$this->registerArgument('title', 'string', 'The title of the info box');
$this->registerArgument('state', 'mixed', 'The state of the box, accepts ContextualFeedbackSeverity enum or integer value', false, ContextualFeedbackSeverity::NOTICE);
$this->registerArgument('iconName', 'string', 'Identifier of the icon as registered in the Icon Registry. NULL sets default icon');
$this->registerArgument('disableIcon', 'bool', 'If set to TRUE, the icon is not rendered.', false, false);
}
public function render(): string
{
$title = (string)$this->arguments['title'];
$message = (string)$this->renderChildren();
$state = $this->arguments['state'];
// The state argument accepts both a ContextualFeedbackSeverity enum and a raw integer value
if ($state instanceof ContextualFeedbackSeverity) {
$severity = $state;
} else {
$state = (int)$state;
$severity = ContextualFeedbackSeverity::from($state);
}
$disableIcon = $this->arguments['disableIcon'];
$icon = $this->arguments['iconName'] ?? $severity->getIconIdentifier();
$iconTemplate = '';
if (!$disableIcon) {
$iconTemplate = ''
. '<div class="callout-icon">'
. '<span class="icon-emphasized">'
. $this->iconFactory->getIcon($icon, IconSize::SMALL)->render()
. '</span>'
. '</div>';
}
$titleTemplate = '';
if ($title !== '') {
$titleTemplate = '<div class="callout-title">' . htmlspecialchars($title) . '</div>';
}
return '<div class="callout callout-' . htmlspecialchars($severity->getCssClass()) . '">'
. $iconTemplate
. '<div class="callout-content">'
. $titleTemplate
. '<div class="callout-body">' . $message . '</div>'
. '</div>'
. '</div>';
}
/**
* Explicitly set argument name to be used as content.
*/
public function getContentArgumentName(): string
{
return 'message';
}
}