ViewHelper to Access GET and POST Parameters from Any Fluid Template

In TYPO3 Fluid templates, accessing GET or POST parameters directly isn’t straightforward. However, creating a custom ViewHelper to fetch these values can unlock dynamic template rendering based on URL or form data. This article guides you through building a ViewHelper to retrieve GET/POST parameters and demonstrates practical use cases, such as conditionally rendering content on a news detail page.

Why Access GET/POST Parameters in Fluid?

While TYPO3 discourages direct access to superglobals (e.g., $_GET) in templates for security reasons, there are valid scenarios where you might need them:

  1. Dynamic Content Rendering: Show/hide template sections based on URL parameters (e.g., ?tx_news_pi1[news]=123).
  2. Form Handling: Display success messages after form submissions via POST.
  3. Filtering: Build UIs that react to query parameters (e.g., search filters).

A ViewHelper provides a clean, reusable way to handle such cases while keeping logic decoupled from templates.

Writing custom ViewHelper

Create a custom ViewHelper in your TYPO3 extension to fetch GET/POST parameters.

// .../extension/Classes/ViewHelpers/QueryParamsViewHelper.php

<?php

namespace Vendor\Extension\ViewHelpers;

use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3\CMS\Core\Http\ServerRequest;

class QueryParamsViewHelper extends AbstractViewHelper
{
    public function initializeArguments(): void
    {
        parent::initializeArguments();
        $this->registerArgument('parameter', 'string', 'Name of the parameter to fetch (use | for nested parameters)', true);
        $this->registerArgument('method', 'string', 'Request method (GET or POST)', false, 'GET');
    }

    public function render()
    {
        $parameterPath = $this->arguments['parameter'];
        $method = strtoupper($this->arguments['method']);

        if (($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequest) {
            $request = $GLOBALS['TYPO3_REQUEST'];

            if ($method === 'POST') {
                $params = $request->getParsedBody();
            } else {
                $params = $request->getQueryParams();
            }
        } else {
            $params = ($method === 'POST') ? $_POST : $_GET;
        }

        if (str_contains($parameterPath, '|')) {
            $parts = explode('|', $parameterPath);
            $value = $params;

            foreach ($parts as $part) {
                if (is_array($value) && isset($value[$part])) {
                    $value = $value[$part];
                } else {
                    return null;
                }
            }

            return $value;
        }

        return $params[$parameterPath] ?? null;
    }
}

Use the ViewHelper in Fluid Templates

First of all, lets declare or ViewHelper’s namespace in the template:

<html
    xmlns:e="http://typo3.org/ns/Vendor/Extension/ViewHelpers"
    data-namespace-typo3-fluid="true"
>

An now we can access it as we need:

<f:if condition="{e:queryParams(parameter: 'tx_news_pi1|news')} > 0">
  <f:then>
    <!-- Render detail view -->
    <h1>News ID: {e:queryParams(parameter: 'tx_news_pi1|news')}</h1>
    .....
  </f:then>
  <f:else>
    <!-- Render list view -->
    <f:render partial="NewsList" />
    .......
  </f:else>
</f:if>

Leave a Reply

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

Proudly powered by WordPress | Theme: Code Blog by Crimson Themes.