Bug #102922
closedMemory limit php error when trying to add a button with addButton() in an event listener
0%
Description
When I try to add a button in an event listener, I get a php memory limit error.
I am using this one: https://docs.typo3.org/m/typo3/reference-coreapi/12.4/en-us/ApiOverview/Events/Events/Backend/ModifyButtonBarEvent.html
This is the code. Everything is working till the line with addButton()
.
$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
$buttonBar = $event->getButtonBar();
$button = $buttonBar
->makeLinkButton()
->setHref('#')
->setTitle('TEST')
->setIcon($iconFactory->getIcon('actions-approve', Icon::SIZE_SMALL))
->setShowLabelText(true)
;
$buttonBar->addButton($button, ButtonBar::BUTTON_POSITION_LEFT, 1);
$event->setButtons($buttonBar->getButtons());
Updated by TimoF no-lastname-given 10 months ago
- Subject changed from Memory limit php error when trying to add a button in an event listener to Memory limit php error when trying to add a button with addButton() in an event listener
Updated by Oliver Bartsch 10 months ago
- Status changed from New to Needs Feedback
Hi, thanks for creating an issue.
The reason is the last line of your code snippet $event->setButtons($buttonBar->getButtons());
. By calling $buttonBar->getButtons()
you will dispatch the same event again, which leads to an endless loop. Could you please try removing the last line? This should resolve your issue.
Best, Oli
Updated by TimoF no-lastname-given 10 months ago
If i delete the last line, then no button is there.
But i found a working solution.
This works:
$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
$buttons = $event->getButtons();
$buttons[ButtonBar::BUTTON_POSITION_LEFT][1][] = $event->getButtonBar()
->makeLinkButton()
->setTitle('Test')
->setIcon($iconFactory->getIcon('actions-approve', Icon::SIZE_SMALL))
->setShowLabelText(true)
->setHref('#')
$event->setButtons($buttons);
Updated by Oliver Bartsch 10 months ago
- Status changed from Needs Feedback to Closed
Hi,
thanks for checking again. Because it seems that the issue is solved for you, I'll close this issue. In case you encounter similar problems or think this issue is not solved, please contact me or just create a new issue with reference to this one.
Best, Oli