Categories
TYPO3

Developing TYPO3 ViewHelper, that finds the root of page

Step 01

Create a PHP class & inherit from the TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper

<?php
declare(strict_types=1);

use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

final class FindRootOfPageViewHelper extends AbstractViewHelper
{

}

Path: MyExtension/Classes/ViewHelpers/FindRootOfPageViewHelper.php

Step 02

Add the PHP trait TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic & the method renderStatic()

    use CompileWithRenderStatic;

    /**
     * @param array $arguments
     * @param \Closure $renderChildrenClosure
     * @param RenderingContextInterface $renderingContext
     * @return string
     */
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
    {

    }

Step 03

Prevent escaping of the ViewHelper output with the property $escapeOutput

    /**
     * @var bool
     */
    protected $escapeOutput = false;

Step 04

Register the argument pageIdentifier inside initializeArguments()

    /**
     *
     */
    public function initializeArguments()
    {
        $this->registerArgument('pageIdentifier', 'int', 'The page identifier', true);
    }

Step 05

Create function to find the root of page findRootOfPage()

    /**
     * @param int $pageIdentifier
     * @return array
     */
    public static function findRootOfPage(int $pageIdentifier): array
    {
        $pageRepository = GeneralUtility::makeInstance(PageRepository::class);
        $currentPage = $pageRepository->getPage($pageIdentifier);

        if($currentPage['is_siteroot'] === 1) {
            return $currentPage;
        }

        return self::findRootOfPage($currentPage['pid']);
    }

Step 06

Use the function findRootOfPage() & return the ViewHelper output

    /**
     * @param array $arguments
     * @param \Closure $renderChildrenClosure
     * @param RenderingContextInterface $renderingContext
     * @return string
     */
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
    {
        $root = self::findRootOfPage($arguments['pageIdentifier']);
        $output = 'The root of this page is <strong>'. $root['title'] .'</strong>';

        return $output;
    }

Step 07

The whole ViewHelper code

<?php
declare(strict_types=1);

namespace MyVendor\MyExtension\ViewHelpers;

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

final class FindRootOfPageViewHelper extends AbstractViewHelper
{
    use CompileWithRenderStatic;

    /**
     * @var bool
     */
    protected $escapeOutput = false;

    /**
     *
     */
    public function initializeArguments()
    {
        $this->registerArgument('pageIdentifier', 'int', 'The page identifier', true);
    }

    /**
     * @param array $arguments
     * @param \Closure $renderChildrenClosure
     * @param RenderingContextInterface $renderingContext
     * @return string
     */
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
    {
        $root = self::findRootOfPage($arguments['pageIdentifier']);
        $output = 'The root of this page is <strong>'. $root['title'] .'</strong>';

        return $output;
    }

    /**
     * @param int $pageIdentifier
     * @return array
     */
    public static function findRootOfPage(int $pageIdentifier): array
    {
        $pageRepository = GeneralUtility::makeInstance(PageRepository::class);
        $currentPage = $pageRepository->getPage($pageIdentifier);

        if($currentPage['is_siteroot'] === 1) {
            return $currentPage;
        }

        return self::findRootOfPage($currentPage['pid']);
    }
}

Step 08

Import the ViewHelper namespace

{namespace example=MyVendor\MyExtension\ViewHelpers}

Path: MyExtension/Resources/Private/Templates/Default.html

Step 09

Use the ViewHelper

<example:FindRootOfPage pageIdentifier="{data.uid}" />

Output: The root of this page is RootTitle

Leave a Reply

Your email address will not be published. Required fields are marked *