No Code Attached Yet bug
avatar Irata
Irata
7 Feb 2023

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.

Steps to reproduce the issue

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');

Expected result

Refresh the admin view and the Toolbar should appear as it normally would.

Actual result

An empty toolbar is rendered, nothing is displayed.

System information (as much as possible)

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];
}

Additional comments

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.

avatar Irata Irata - open - 7 Feb 2023
avatar Irata Irata - change - 7 Feb 2023
Labels Removed: ?
avatar joomla-cms-bot joomla-cms-bot - change - 7 Feb 2023
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 7 Feb 2023
avatar Irata Irata - change - 7 Feb 2023
The description was changed
avatar Irata Irata - edited - 7 Feb 2023
avatar Irata Irata - edited - 7 Feb 2023
avatar Irata Irata - change - 7 Feb 2023
Title
Toolbar::getInstance('toolbar') depreciated however replacement does not correctly.
Toolbar::getInstance('toolbar') depreciated however replacement does not work correctly in Admin views.
avatar Irata Irata - edited - 7 Feb 2023
avatar Hackwar Hackwar - change - 22 Feb 2023
Labels Added: bug
avatar Hackwar Hackwar - labeled - 22 Feb 2023
avatar ironbone
ironbone - comment - 15 Jun 2023

Still in Joomla ‎4.3.2


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/39814.

avatar brianteeman
brianteeman - comment - 23 Jul 2024

@HLeithner did you do some work on this recently?

avatar HLeithner
HLeithner - comment - 23 Jul 2024

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.

avatar brianteeman
brianteeman - comment - 23 Jul 2024

so what to do with this issue?

avatar HLeithner
HLeithner - comment - 23 Jul 2024

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)

avatar Quy Quy - change - 26 Jul 2024
Status New Closed
Closed_Date 0000-00-00 00:00:00 2024-07-26 04:27:18
Closed_By Quy
avatar Quy Quy - close - 26 Jul 2024

Add a Comment

Login with GitHub to post a comment