The getInstance method of Joomla\CMS\ToolbarToolbar is depreciated in 5.0 in favour of using the Container to obtain the toolbar object however mod_toolbar.php still uses getInstance() which is incompatible if the Toolbar as already been obtained from the container.
In any admin view that adds a toolbar to the display, replace the line
$toolbar = Toolbar::getInstance('toolbar');
with
$toolbar = Factory::getContainer()->get(ToolbarFactoryInterface::class)->createToolbar('toolbar');
Refresh the admin view and the Toolbar should appear as it normally would.
An empty toolbar is rendered, nothing is displayed.
The getInstance method of Joomla\CMS\ToolbarToolbar checks for the existence of an $instance
of a toolbar object and if not found it will will create a new $instance
object from the container. However if you have already created the toolbar object from the container and primed it with your buttons, say in a HtmlView, then the getInstance() doesn't recognise that you already have a toolbar, because it is not in $instance
and thus it creates a new empty toolbar object from the container.
The new empty toolbar is then what is rendered by mod_toolbar.php and similar processing is found in a couple of core template files.
Any situation where a subsequent reference to getInstance() occurs to check for an $instance
of a toolbar object would also create an new object if the original toolbar was created from the container.
public static function getInstance($name = 'toolbar')
{
if (empty(self::$instances[$name])) {
self::$instances[$name] = Factory::getContainer()->get(ToolbarFactoryInterface::class)->createToolbar($name);
}
return self::$instances[$name];
}
Once I found the cause of my issue and knew what I was looking for I was then able to find other references to problem therefore it is known but I couldn't find any specific issue raised to address fixing the problem. By raising this issue other people who are paying attention to their code and the depreciated methods may find this and quickly understand why their toolbar(s) aren't displaying.
This problem gets mentioned here, https://issues.joomla.org/tracker/joomla-cms/39537#event-717209
and here at the Toolbar::getInstance() three quarters the way down the page, https://docs.joomla.org/J3.x:Developing_an_MVC_Component/Upgrading_to_Joomla4
A temporary solution is to render the toolbar yourself in the the /tmpl file with
echo $this->toolbar->render();
and if placed inside an element with an id="subhead"
it should be displayed in the same style as the mod_toolbar.php rendering.
Labels |
Removed:
?
|
Labels |
Added:
No Code Attached Yet
|
Title |
|
Labels |
Added:
bug
|
@HLeithner did you do some work on this recently?
Actually it's working in 5.0 but the solution to get an existing instance is wrong, the Document Object holdes the toolbars. So the correct way is to use $this(view)->getDocument()->getToolbar() (the default value is toolbar
).
So yes all calls to Toolbar::getInstance();
need to be replaced by $this->getDocument()->getToolbar();
, if the document
is not available in the current context, it's questionable why it is needed at this place and how to get it there. As bad workaround the document can beloaded thru the application.
so what to do with this issue?
close it since 5.0.0 has it saved in the document and create a better documentation, sadly didn't had the time to write a nice documentation for the toolbar (also did my own fork of it to allow me to insert buttons at a specific position, PR will come later for core)
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2024-07-26 04:27:18 |
Closed_By | ⇒ | Quy |
Still in Joomla 4.3.2
This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/39814.