1

The only source I found about this is from this post: Replacing JSubmenuHelper with JHtmlSidebar / Administrative menu in Joomla 4

The issue here is that I tried it, and it doesn't show the Sidebar if I try it with JHtmlSidebar.

Here is the Code:

public function render($config = array())
{
    $config = new KObjectConfigJson($config);
    $config->append(array(
        'toolbar' => null
    ));

    foreach ($config->toolbar->getCommands() as $command)
    {
        if(!empty($command->href)) {
            $command->href = $this->getTemplate()->route($command->href);
        }

        JSubmenuHelper::addEntry($this->getObject('translator')->translate($command->label), $command->href, $command->active);

    }

    return '';
}

In this, JSubmenuHelper::addEntry should be replaced with the JHtmlSidebar function.

I tried to change the line to

JHtmlSidebar::addEntry($this->getObject('translator')->translate($command->label), $command->href, $command->active);

and to

JHtmlSidebar::render($this->getObject('translator')->translate($command->label), $command->href, $command->active);

but on with addEntry the sidebar is hidden and with render() there is no content in it.

What is wrong with the attempts that I already tried?

1

You will firstly need to create a helper class that extends ContentHelper, like so:

administrator/components/com_mycomponent/Helper/MyComponentHelper.php

use Joomla\CMS\Helper\ContentHelper;

class MyComponentHelper extends ContentHelper
{
    protected $config = null;

    public function __construct($config)
    {
        $this->config = new KObjectConfigJson($config);
        $this->config->append(array(
            'toolbar' => null
        ));
    }

    public static function addSubmenu()
    {
        foreach ($this->config->toolbar->getCommands() as $command)
        {
            if (!empty($command->href))
            {
                $command->href = $this->getTemplate()->route($command->href);
            }

            \JHtmlSidebar::addEntry(
                $this->getObject('translator')->translate($command->label),
                $command->href,
                $command->active
            );

        }
    }
}

Then in the following directory:

administrator/components/com_mycomponent/controller.php

You'll want to load the class, using JLoader, like so:

JLoader::register('MyComponentHelper', JPATH_ADMINISTRATOR . '/components/com_mycomponent/helpers/contact.php');

Once done, you will need to instantiate the class in your view.html.php:

MyComponentHelper::addSubmenu();

Notes:

  • As far as I know, there is no namespaced version of \JHtmlSidebar so I'm not sure if it's going to be supported or replaced in J4.
  • I haven't tested this as I don't really work with components
  • You will need to add the getObject() method to the class or change the $this->getObject() reference if it's coming from a different class.

Hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.