Skip to content

Instantly share code, notes, and snippets.

@dmitryd
Created August 17, 2020 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitryd/d280f55d43eede8e5e72150423b542d3 to your computer and use it in GitHub Desktop.
Save dmitryd/d280f55d43eede8e5e72150423b542d3 to your computer and use it in GitHub Desktop.
Restore split button for TYPO3 v9 forms
<?php
namespace Vendor\Extension\Hook;
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
use TYPO3\CMS\Backend\Template\Components\Buttons\InputButton;
use TYPO3\CMS\Backend\Template\Components\Buttons\LinkButton;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* This class contains a hook to add a "Save & Close" action to the toolbar.
*/
class ContentButtonBarHook
{
/** @var ServerRequest */
protected $request;
/**
* Creates an instance of this class.
*
* @param ServerRequest $request
*/
public function __construct(?ServerRequest $request = null)
{
$this->request = $request ?? $GLOBALS['TYPO3_REQUEST'];
}
/**
* Changes the button bar
*
* @param array $parameters
* @param ButtonBar $buttonBar
* @return array
*/
public function getButtons(array $parameters, ButtonBar $buttonBar): array
{
if ($this->getTableName() === 'tt_content') {
$this->updateButtons($parameters['buttons'], $buttonBar);
}
return $parameters['buttons'];
}
/**
* Updates buttons.
*
* @param array $buttonGroups
* @param ButtonBar $buttonBar
*/
protected function updateButtons(array &$buttonGroups, ButtonBar $buttonBar)
{
foreach ($buttonGroups as $sideName => $buttonGroupSide) {
foreach ($buttonGroupSide as $posiition => $buttonGroup) {
foreach ($buttonGroup as $key => $button) {
$type = $button->getType();
switch ($type) {
// Uncomment below if you want to have "icon-only" close & delete buttons like in v8
// case LinkButton::class:
// switch ($button->getClasses()) {
// case 't3js-editform-close':
// case 't3js-editform-delete-record':
// $button->setShowLabelText(false);
// break;
// }
// break;
case InputButton::class:
if ($button->getName() === '_savedok') {
$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
$splitSaveButton = $buttonBar->makeSplitButton();
$splitSaveButton->addItem($button, true);
$splitSaveButton->addItem(
$buttonBar->makeInputButton()
->setName('_savedoknew')
->setValue('1')
->setForm($button->getForm())
->setTitle($GLOBALS['LANG']->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:rm.saveNewDoc'))
->setIcon(
$iconFactory->getIcon(
'actions-document-save-new',
Icon::SIZE_SMALL
)
)
);
$splitSaveButton->addItem(
$buttonBar->makeInputButton()
->setName('_savedokview')
->setValue('1')
->setForm($button->getForm())
->setTitle($GLOBALS['LANG']->sL('LLL:EXT:core/Resources/Private/Language/locallang_common.xlf:saveAndViewPage'))
->setIcon(
$iconFactory->getIcon(
'actions-document-save-close',
Icon::SIZE_SMALL
)
)
);
$splitSaveButton->addItem(
$buttonBar->makeInputButton()
->setName('_saveandclosedok')
->setValue('1')
->setForm($button->getForm())
->setTitle($GLOBALS['LANG']->sL('LLL:EXT:core/Resources/Private/Language/locallang_common.xlf:saveAndClose'))
->setIcon(
$iconFactory->getIcon(
'actions-document-save-close',
Icon::SIZE_SMALL
)
)
);
$buttonGroups[$sideName][$posiition][$key] = $splitSaveButton;
}
break;
}
}
}
}
}
/**
* Fetches table name from the request.
*
* @return string|null
*/
protected function getTableName(): ?string
{
$table = array_key_first($this->request->getQueryParams()['edit'] ?? []);
if (!is_string($table) || empty($table)) {
return null;
}
return $table;
}
}
<?php
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['Backend\Template\Components\ButtonBar']['getButtonsHook'][1597666436]
= \Vendor\Extension\Hook\ContentButtonBarHook::class . '->getButtons';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment