7

I can't find any tutorials on how to use the JHtmlSidebar Utility Class.

This comes close to it: Creating a toolbar for your component, but it is very vague. It mentions $this->sidebar, which suggest that the View class should have an attribute sidebar. But sidebar attribute doesn't exist in the View class.

I tried using this:

JHtmlSidebar::addEntry('test','test',0);
echo $this->sidebar = JHtmlSidebar::render();

It does indeed echo the sidebar, but it sits at top or bottom of my view, not on the side. I'm just wondering if there is a way to let Joomla do the rendering for me, instead of creating my own layout.

Some help or links to useful tutorial on the use of admin side bar would be much appreciated. Thank you.

com_helloworld/views/helloworld/view.html.php:

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

class HelloWorldViewHelloWorld extends JViewLegacy {
    public function display($tpl = NULL){
        $this->addToolbar();
        $this->sidebar = JHtmlSidebar::render();
        }
    }

    protected function addToolbar(){
        require_once JPATH_COMPONENT . '/helpers/helloworld .php';
        JToolBarHelper::title( 'Hello World', 'helloworld' );
        JToolBarHelper::addNew();
        JToolBarHelper::deleteList();
        JToolBarHelper::editList();
        JToolBarHelper::preferences('com_helloworld', '500');
        xProfileHelper::addSubmenu('helloworld');
    }
}

com_helloworld/helpers/helloworld.php:

class HelloWorldHelper extends JHelperContent
{
    public static function addSubmenu($vName)
    {
        JHtmlSidebar::addEntry(
            'test',
            'index.php?option=com_helloworld&view=helloworld',
            $vName == 'profile'
        );
    }
}
0

1 Answer 1

11

You firstly need to create a helper file, for example:

administrator/components/com_helloworld/helpers/helloworld.php

and in this file, add the following:

class HelloWorldHelper extends JHelperContent
{
    public static function addSubmenu($vName)
    {
        JHtmlSidebar::addEntry(
            'test',
            'index.php?option=com_hellowworld&view=VIEWNAME',
            $vName == 'banners'
        );
    }
}

Change VIEWNAME and the value for $vName to the view for your component that you wish to add the sidebar to.

Then, in your view.html.php file, call it using the following:

HelloWorldHelper::addSubmenu('VIEWNAME');

and of course, the following is required to render the sidebar which you can put below the code above:

$this->sidebar = JHtmlSidebar::render();

If you have a look at the Banners component that comes packaged with Joomla, it will provide a good example.

Update:

If forgot to mention, inside your addToolbar() function in the view.html.php file, you will need to call the helper file like so:

require_once JPATH_COMPONENT . '/helpers/hellowworld.php';

Update 2:

Sorry, I'm not on the ball today. You will need to add the following to your default.php in views/VIEWNAME/tmpl:

<div id="j-sidebar-container" class="span2">
    <?php echo $this->sidebar; ?>
</div>
<div id="j-main-container" class="span10">
     // Main part of the component view
</div>
9
  • Any thoughts on how easy/difficult it would be to use the sidebar for a component's F/E?
    – GDP
    Aug 18, 2014 at 14:29
  • @GDP, by "F/E", are you referring to the frontend?
    – Lodder
    Aug 18, 2014 at 14:41
  • Thanks, I used your code and followed the banners example. But now it's telling me "Fatal error: Class 'HelloWorldHelper' not found in ..\com_helloworld\views\myview\view.html.php on" Do I need to instantiate the helper somewhere? I've never used one before... Aug 18, 2014 at 14:43
  • I made sure it's the same by copying. PHPStorm is even able to link the file. So I don't understand why it's not in scope when I test it. Aug 18, 2014 at 14:51
  • @user3804927 - See my updated answer. Sorry I forgot about that :/
    – Lodder
    Aug 18, 2014 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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