jsonhyperlinkheadlessrtetypo3-11.x

TYPO3 render full t3:// links from bodytext in utility files


I have a headless TYPO3 installation and i need to render all the content elements as json. So far so good. The only problem i have right now is to replace the t3:// links with the full URLs since i do not have the frontend to take care of it. So the question is:

How do i replace the intern TYPO3 links in a bodytext (RTE) to full URLs?

I have tried the following but it dint really help:

$cObj->stdWrap_HTMLparser($element['bodytext']

Best regards


Solution

  • So i examined the TYPO3\CMS\Fluid\ViewHelpers\Format\HtmlViewHelper to get some ideas. It is actually very easy to do it.

    Note: I have only tested this with t3://page?uid= and it works. The moment i find other use cases as well, i will update this answer

    This code happens on my Utility class under the my_ext/Classes/Utility/MyUtility.

    I first inject the TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer with DI (Dependency Injection)

    /**
     * @var ContentObjectRenderer
     */
    protected ContentObjectRenderer $cObj;
    
    /**
     * @param ContentObjectRenderer $cObj
     */
    public function __construct(
        ContentObjectRenderer $cObj
    ) {
        $this->cObj = $cObj;
    }
    

    Now i can use the parseFunc function to replace the links, with full URls (without the base url). BUT we have to define what kind of reference this function will get, which in my case would be the lib.parseFunc. So this is how it looks like:

    Sometimes you only need the first and second parameters and you can leave the reference empty. You have to experiment a bit and make it work according to your needs.

    public function my_module(array $teaserHomepage): array
    {
      $parseFuncTSPath = 'lib.parseFunc';
      $newConstructedArray = [];
      foreach ($teaserHomepage['fields'] as $element) {
        ...
        $newConstructedArray['fields']['bodytext'] = $this->cObj->parseFunc($element['bodytext'], [], '< ' . $parseFuncTSPath);
      }
    
      return $newConstructedArray;
    }
    

    The before text:

    <p>sed diam nonumy eirmod tempor invidunt ut <a href=\"t3://page?uid=2\"> labore et dolore magna aliquyam erat, sed diam.</a></span><br /> <br /> &nbsp;</p>
    

    The after text:

    <p>sed diam nonumy eirmod tempor invidunt ut<a href=\"/our-jobs\"> labore et dolore magna aliquyam erat, sed diam.</a></span><br> <br> &nbsp;</p>
    

    Hopefully it helped someone

    Best regards