Project

General

Profile

Actions

Feature #102261

closed

Allow <f:be.pageRenderer> to unregister CSS files

Added by Garvin Hicking about 1 year ago. Updated about 1 year ago.

Status:
Closed
Priority:
Should have
Assignee:
-
Category:
Backend User Interface
Target version:
Start date:
2023-10-26
Due date:
% Done:

0%

Estimated time:
PHP Version:
Tags:
Complexity:
Sprint Focus:

Description

Backend Module authors may want to deliver a fully customized
backend without the CSS framework of TYPO3.

Currently the pageRenderer is only able to register
additional CSS files for output, but the access to
the internal $cssFiles property is gated and no
event or get/setter allows to access it.

This patch adds the ability to both retrieve the
current list of $cssFiles and also a removeCssFile()
method.

The ViewHelper is enhanced to allow access to these
methods, because when Extbase actions are executed,
accessing the PageRenderer singleton instance does
not yet have the internal stylesheets assigned.

A modules Fluid template could then do this:

    <f:be.pageRenderer
        includeJavaScriptModules="{
            0: '@typo3/backend/global-event-handler.js',
            1: '@typo3/belog/backend-log.js'
        }" 
        removeCssFiles="{
            0: 'backend.css'
        }" 
    />

The array passed to removeCssFiles can contain
any string to match against the registered files,
so whole paths or specific files can be removed,
no matter if EXT:xxx syntax or a absolute file
(like for backend.css) is used.

(The Fluid viewhelper will also work for Backend modules
outside Extbase context.)

Actions #1

Updated by Gerrit Code Review about 1 year ago

  • Status changed from New to Under Review

Patch set 1 for branch main of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/81575

Actions #2

Updated by Garvin Hicking about 1 year ago

Turns out there IS an event that can be utilized, making the whole issue obsolete. Documented here, in case anyone ever wants to do that (HINT: Use your time to convert this hook to a PSR-14 event instead!).

So this hook is doing the magic:

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_pagerenderer.php']['render-postProcess']

It is executed as a last step within the PageRenderer class, passing all rendered parts of a page to the event.

So you can provide an extension that does the following.

Register the hook, in ext_localconf.php

<?php
        $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_pagerenderer.php']['render-postProcess'][] =
            \YourVendor\YourExtension\Hooks\PostRenderHook::class . '->postProcessKillBackendCSS';

Create a hook class in Classes/Hooks/PostRenderHook.php

<?php
namespace YourVendor\YourExtension\Hooks;

class PostRenderHook
{
    public function postProcessKillBackendCSS(&$params, $object) {
        if (isset($GLOBALS['MY_EXTENSION']['removeBackendCss'])) {
            $params['cssFiles'] =
                preg_replace(
                    '@<link rel="stylesheet" href="[^"]+backend.css[^>]+>@imsU',
                    '<!-- removed TYPO3 Backend via popular demand -->',
                    $params['cssFiles']
                );

            // You could do other hacks here as well, i.e. operate on $params['bodyContent']
        }
    }
}

Adjust your backend Controller, i.e. in a global initializeAction method:

$GLOBALS['MY_EXTENSION']['removeBackendCss'] = true;

The last step is important to instruct the hook that you only want to remove the backend CSS on your specific backend module. Else, you'd kill the backend.css file in EVERY backend module.

Abusing a global variable for this is the easiest approach, but a bit dirty. You might create a singleton object at that place which you later query, if you want to solve it "properly". Or you might let the hook introspect the body of the rendered page and get triggered by specific classes or whatnot.

Actions #3

Updated by Christian Kuhn about 1 year ago

  • Status changed from Under Review to Closed

Thanks for documenting a solution!

Actions

Also available in: Atom PDF