quellenform / t3x-iconpack

This extension provides an iconpack registry for custom iconpacks which can be used in backend and frontend and are rendered according to the configuration of the installed iconpacks.
GNU General Public License v3.0
10 stars 4 forks source link

How to have the custom RTEs processed for icons ? #1

Open lucmuller opened 1 year ago

lucmuller commented 1 year ago

I've encountered a problem and don't know at the moment how to solve it. If I input an icon in the bodytext of a tt_content the icon is properly displayed. If I setup a rte in another type of content (in my case build with flux) the rte isn't processed in the same way.

I guess the problem is coming from the fact that the icons are processed in a dataprocessing of lib.contentElement instead of the lib.parseFunc_RTE See the capture below first icon is set in a tt_content bodytext the other on in a custom content element.

image

any help would be welcome.

lucmuller commented 1 year ago

Right now I've achieved to solve my problem with a custom viewhelper

declare(strict_types=1);

namespace Ameos\EsieeSitepackage\ViewHelpers\Format;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Html\HtmlParser;
use Quellenform\Iconpack\Html\IconpackHtmlParser;
use TYPO3\CMS\Fluid\ViewHelpers\Format\HtmlViewHelper as TYPO3HtmlViewHelper;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;

class HtmlViewHelper extends TYPO3HtmlViewHelper
{
    /**
     * @param array $arguments
     * @param \Closure $renderChildrenClosure
     * @param RenderingContextInterface $renderingContext
     *
     * @return string the parsed string.
     */
    public static function renderStatic(
        array $arguments,
        \Closure $renderChildrenClosure,
        RenderingContextInterface $renderingContext
    ) {
        $htmlParser = GeneralUtility::makeInstance(HtmlParser::class);
        $sContent = parent::renderStatic($arguments, $renderChildrenClosure, $renderingContext);
        return sprintf(
            '<div class="cke_editable">%s</div>',
            GeneralUtility::makeInstance(IconpackHtmlParser::class)->transformRte(html_entity_decode($sContent), $htmlParser)
        );
    }
}
stephankellermayr commented 1 year ago

Yes, the output must be processed by the DataProcessor at the end.

Otherwise, the content would be displayed exactly as it is stored in the database:

<icon data-iconfig="fa6:solida,circle-info"></icon>

In principle, there are many ways to solve this, and a custom ViewHelper is also possible.

Here is an example for you:

  1. Create a database field in ext_tables.sql:
--
-- Table structure for table 'tt_content'
--
CREATE TABLE tt_content (
    icontest mediumtext,
);
  1. Add the field in the backend by inserting the following content (or similar) in Configuration/TCA/Overrides/tt_content.php:
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
    'tt_content',
    [
        'customfield' => [
            'label' => 'My custom field',
            'config' => [
                'type' => 'text',
                'cols' => 80,
                'rows' => 15,
                'enableRichtext' => true,
                'richtextConfiguration' => 'default',
            ],
        ]
    ]
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
    'tt_content',
    'customfield'
);
  1. Add the field to the template so that it is also displayed in the frontend:
<f:format.html>{data.customfield}</f:format.html>
  1. In order to finally process the custom field, the template must be loaded and the DataProcessor for the field must be activated:
lib.contentElement {
  templateRootPaths {
    20 = EXT:mycustomextension/Resources/Private/Templates/ContentElements/
  }
  dataProcessing {
    3212170002 = Quellenform\Iconpack\DataProcessing\IconpackProcessor
    3212170002 {
      fieldName = customfield
      fieldType = rte
    }
  }
}

Note that fieldType has the value "rte"!


I don't know exactly what your extension is like, but the way shown here works and has been tested by me.

PS: Don't forget to include the TypoScript delivered with the extension, or to take over the parts that are relevant for you!

Important: I wouldn't use the function transformRte or IconpackHtmlParser directly if I were you! This will probably have to be removed in the coming update (v12 compatibility)!

If you really want to use your own ViewHelper, then look at the existing ViewHelper and replace the value native with rte.

lucmuller commented 1 year ago

This is not what I need, as I do not have a proper field in the database. I'm using a flux content element. So the rte content is stored in the pi_flexform field of the content element. RTE content is parsed through f:format.html (by the way a custom on at the moment) viewhelper and icon should be parsed when using the viewhelper (who uses lib.parseFunc_RTE)

lucmuller commented 1 year ago

Any News on this topic ?

What I would need is a viewhelper that would be able to process the RTE content after it has been parsed by Any help on this ?

lucmuller commented 1 year ago

Sorry for the inconvenience...

Didn't remember I'v build my own viewhelper.

Here it is

<?php

declare(strict_types=1);

namespace Ameos\Package\ViewHelpers\Format;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Html\HtmlParser;
use Quellenform\Iconpack\Html\IconpackHtmlParser;
use TYPO3\CMS\Fluid\ViewHelpers\Format\HtmlViewHelper as TYPO3HtmlViewHelper;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;

class HtmlViewHelper extends TYPO3HtmlViewHelper
{
    /**
     * @param array $arguments
     * @param \Closure $renderChildrenClosure
     * @param RenderingContextInterface $renderingContext
     *
     * @return string the parsed string.
     */
    public static function renderStatic(
        array $arguments,
        \Closure $renderChildrenClosure,
        RenderingContextInterface $renderingContext
    ) {
        $htmlParser = GeneralUtility::makeInstance(HtmlParser::class);
        $sContent = parent::renderStatic($arguments, $renderChildrenClosure, $renderingContext);
        return sprintf(
            '<div class="cke_editable">%s</div>',
            GeneralUtility::makeInstance(IconpackHtmlParser::class)
                ->transformRte(html_entity_decode($sContent), $htmlParser)
        );
    }
}
stephankellermayr commented 1 year ago

Note: IconpackHtmlParser::transformRte() has been renamed to IconpackHtmlParser::transformIconsForOutput() in the new version (1.0.0)!

https://github.com/quellenform/t3x-iconpack/blob/main/Classes/Html/IconpackHtmlParser.php