typo3fluid

Link to BE module "page layout" with "close button" in TYPO3


Make it possible to link to the Backend module page layout (/web/layout) in TYPO3, with a return URL and a close button.

In particular, I have a list (e.g. like the list of broken links in linkvalidator) and want to jump to the page layout view, but make it possible to jump back again - otherwise it is very awkward, if you jump to the view and then can't jump back.

enter image description here

The left button will jump to the page layout view, but then there is no button to close or go back.

The right button will open an edit form where we also have a "Close" button. This is what I want for the other one as well.

<f:variable name="link"><be:moduleLink route="web_layout" arguments="{id: element.pid, returnUrl: returnUrl}">layout</be:moduleLink></f:variable>
<a href="{link}" class="btn btn-default" title="page layout">
  <core:icon identifier="actions-document" size="small"/>
</a>
<be:link.editRecord class="btn btn-primary" title="edit"                                                     uid="{element.uid}" table="tt_content" returnUrl="{returnUri}">
  <core:icon identifier="actions-document-edit" size="small"/>
</be:link.editRecord>

The "returnUrl" has already been created correctly in the Controller.

I have investigated: both Viewhelpers will use UriBuilder::buildUriFromRoute which accepts a list of parameters, including returnUrl. So the problem is probably, that the page layout does not consider the possibility it was opened from elsewhere and a close button should be displayed. But I assume, this can be done, just not sure how.


TYPO3 v11 and above.


Solution

  • Assuming you use TYPO3 12. (Please always add informations about you running system when asking questions.)

    Taking a look at the PageLayoutController:
    TYPO3\CMS\Backend\Controller\PageLayoutController

    It seems that there is no Close/Return Link you can use.
    But the Event "ModifyPageLayoutContentEvent" is fired in the mainAction of the controller.
    You could use this event to implement your close/return link.

    For that you need register an event-listener in:
    your-ext/Configuration/Services.yaml
    Like that:

    services:
      VENDOR\YourExt\EventListener\PageLayoutListener:
        tags:
          - name: event.listener
            identifier: your-ext/page-layout-listener
    

    And then you can implement the event-listener like that:

    <?php
    declare(strict_types = 1);
    namespace VENDOR\YourExt\EventListener;
    
    use TYPO3\CMS\Backend\Controller\Event\ModifyPageLayoutContentEvent;
    use TYPO3\CMS\Backend\Template\Components\ButtonBar;
    use TYPO3\CMS\Core\Imaging\Icon;
    use TYPO3\CMS\Core\Imaging\IconFactory;
    use TYPO3\CMS\Core\Utility\GeneralUtility;
    
    class PageLayoutListener
    {
        public function __invoke(ModifyPageLayoutContentEvent $event): void
        {
            // here, we could use the referer URL or get the returnUrl argument which was passed to the ViewHelper 
            //$refererUrl = $_SERVER["HTTP_REFERER"];
            $returnUrl = $event->getRequest()->getQueryParams()['returnUrl'] ?? '';  
    
            // Somehow you need to check from which Page you come,
            // so that the Close-Link is not shown, when opening the web/layout module directly
            // Here its assumed that the referer URL is the linkvalidator-reports page
            // You need at least PHP 8 when using str_contains function. If you run PHP<8 you need to use substr or other function
            // use str_contains to check for specific module or check returnUrl
            //if(str_contains($refererUrl, 'typo3/module/page/link-reports')) {
            if ($returnUrl) {
                $view = $event->getModuleTemplate();
                $buttonBar = $view->getDocHeaderComponent()->getButtonBar();
                $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
    
                $returnButton = $buttonBar->makeLinkButton()
                    //->setHref($refererUrl)
                    ->setHref($returnUrl)  
                    ->setTitle('Close')  // Here you could use translation if needed like this: $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.showPage')
                    ->setIcon($iconFactory->getIcon('actions-close', Icon::SIZE_SMALL))  // Create your custom icon or use any of the alredy created icons
                    ->setShowLabelText(true);
                $buttonBar->addButton($returnButton, ButtonBar::BUTTON_POSITION_LEFT, 0);
            }
        }
    }
    

    EDIT: In TYPO3 < 12 you can use the "drawHeaderHook" to do the same. Did not test it, but the View is also passed in the hook. Should be available by getModuleTemplate function.

    In TYPO3 11.5.36 the hook is implemented in Line 732 of the PageLayoutController:
    https://github.com/TYPO3-CMS/backend/blob/10bdd68cc1b35e71779511192219ba35a287b42b/Classes/Controller/PageLayoutController.php#L732