ModuleTemplate 

Backend controllers should use ModuleTemplateFactory::create() to create instances of a \TYPO3\CMS\Backend\Template\ModuleTemplate .

API functions of the ModuleTemplate can be used to add buttons to the button bar. It also implements the \TYPO3\CMS\Core\View\ViewInterface so values can be assigned to it in the actions.

final class ModuleTemplate
Fully qualified name
\TYPO3\CMS\Backend\Template\ModuleTemplate

A class taking care of the "outer" HTML of a module, especially the doc header and other related parts.

assign(string $key, mixed $value)

Add a variable to the view data collection.

param $key

the key

param $value

the value

Returns
\TYPO3\CMS\Backend\Template\ModuleTemplate
assignMultiple(array $values)

Add multiple variables to the view data collection.

param $values

Array of string keys with mixed-type values.

Returns
\TYPO3\CMS\Backend\Template\ModuleTemplate
render(string $templateFileName = '')

Render the module.

param $templateFileName

the templateFileName, default: ''

Returns
string
renderResponse(string $templateFileName = '')

Render the module and create an HTML 200 response from it. This is a lazy shortcut so controllers don't need to take care of this in the backend.

param $templateFileName

the templateFileName, default: ''

Returns
\Psr\Http\Message\ResponseInterface
setBodyTag(string $bodyTag)

Set to something like '<body id="foo">' when a special body tag is needed.

param $bodyTag

the bodyTag

Returns
\TYPO3\CMS\Backend\Template\ModuleTemplate
setTitle(string $title, string $context = '')

Title string of the module: "My module · Edit view"

param $title

the title

param $context

the context, default: ''

Returns
\TYPO3\CMS\Backend\Template\ModuleTemplate
getDocHeaderComponent()

Get the DocHeader. Can be used in controllers to add custom buttons / menus / ... to the doc header.

Returns
\TYPO3\CMS\Backend\Template\Components\DocHeaderComponent
setForm(string $formTag = '')

A "<form>" tag encapsulating the entire module, including doc-header.

param $formTag

the formTag, default: ''

Returns
\TYPO3\CMS\Backend\Template\ModuleTemplate
setModuleId(string $moduleId)

Optional 'data-module-id="{moduleId}"' on first <div> in body.

Can be helpful in JavaScript.

param $moduleId

the moduleId

Returns
\TYPO3\CMS\Backend\Template\ModuleTemplate
setModuleName(string $moduleName)

Optional 'data-module-name="{moduleName}"' on first <div> in body.

Can be helpful in JavaScript.

param $moduleName

the moduleName

Returns
\TYPO3\CMS\Backend\Template\ModuleTemplate
setModuleClass(string $moduleClass)

Optional 'class="module {moduleClass}"' on first <div> in body.

Can be helpful styling modules.

param $moduleClass

the moduleClass

Returns
\TYPO3\CMS\Backend\Template\ModuleTemplate
addFlashMessage(string $messageBody, string $messageTitle = '', \TYPO3\CMS\Core\Type\ContextualFeedbackSeverity $severity = \TYPO3\CMS\Core\Type\ContextualFeedbackSeverity::OK, bool $storeInSession = true)

Creates a message object and adds it to the FlashMessageQueue.

These messages are automatically rendered when the view is rendered.

param $messageBody

the messageBody

param $messageTitle

the messageTitle, default: ''

param $severity

the severity, default: TYPO3CMSCoreTypeContextualFeedbackSeverity::OK

param $storeInSession

the storeInSession, default: true

Returns
\TYPO3\CMS\Backend\Template\ModuleTemplate
setFlashMessageQueue(\TYPO3\CMS\Core\Messaging\FlashMessageQueue $flashMessageQueue)

ModuleTemplate by default uses queue 'core.template.flashMessages'. Modules may want to maintain an own queue. Use this method to render flash messages of a non-default queue at the default position in module HTML output. Call this method before adding single messages with addFlashMessage().

param $flashMessageQueue

the flashMessageQueue

Returns
\TYPO3\CMS\Backend\Template\ModuleTemplate
setUiBlock(bool $uiBlock)

UI block is a spinner shown during browser rendering phase of the module, automatically removed when rendering finished. This is done by default, but the UI block can be turned off when needed for whatever reason.

While the UI block is active, the doc header button bar is rendered "inert" as well, so that its buttons stay visible but can not be operated. It is released together with the spinner.

param $uiBlock

the uiBlock

Returns
\TYPO3\CMS\Backend\Template\ModuleTemplate
makeDocHeaderModuleMenu(array $additionalQueryParams = [])

Generates a module actions dropdown in the docheader button bar.

Creates a dropdown button on the LEFT side (group 0) containing navigation to submodules or module actions. The button label shows the currently active module/action.

param $additionalQueryParams

the additionalQueryParams, default: []

Returns
\TYPO3\CMS\Backend\Template\ModuleTemplate
addButtonToButtonBar(\TYPO3\CMS\Backend\Template\Components\Buttons\ButtonInterface $button, string $buttonPosition = \TYPO3\CMS\Backend\Template\Components\ButtonBar::BUTTON_POSITION_LEFT, int $buttonGroup = 1)

Shorthand method to add a new button to the button bar

param $button

the button

param $buttonPosition

the buttonPosition, default: TYPO3CMSBackendTemplateComponentsButtonBar::BUTTON_POSITION_LEFT

param $buttonGroup

the buttonGroup, default: 1

Returns
\TYPO3\CMS\Backend\Template\ModuleTemplate

Example: create and use a ModuleTemplate in an Extbase controller 

Class MyVendor\MyExtension\Controller\BackendController
<?php

use MyVendor\MyExtension\Domain\Model\Post;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Backend\Template\ModuleTemplate;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

class BackendController extends ActionController
{
  protected function initializeModuleTemplate(
    ServerRequestInterface $request,
  ): ModuleTemplate {
    $view = $this->moduleTemplateFactory->create($request);

    $context = '';
    $this->modifyDocHeaderComponent($view, $context);
    $view->setFlashMessageQueue($this->getFlashMessageQueue());
    $view->setTitle(
      $this->getLanguageService()->sL('my_extension.module.mod:mlang_tabs_tab'),
      $context,
    );

    return $view;
  }

  public function showPostAction(Post $post): ResponseInterface
  {
    if (!$this->isCurrentPageSysfolder()) {
      return $this->redirect('index');
    }
    $view = $this->initializeModuleTemplate($this->request);
    $view->assign('post', $post);
    return $view->renderResponse('ShowPost');
  }
}
Copied!