Using the JToolbarButton class in the frontend

700 views
Skip to first unread message

Erik Jorgensen

unread,
Dec 7, 2013, 10:11:24 PM12/7/13
to joomla-de...@googlegroups.com
Hi,

I'm developing my first Joomla 3.x component (new to OOP) and I need to have full CRUD functionality available from the front end. There is documentation that claims that it is now easy to use the JToolBar in the front end, however the it says you should make a JButtonFrontend class extending JButton.
http://docs.joomla.org/J3.2:Using_the_JToolBar_class_in_the_frontend

Unfortunately, it turns out that JButton is deprecated and I was unable to get the buttons working using the new JToolbarButton class that replaces it. It says to copy over the content of joomla.html.toolbar.button standard.php but that doesn't seem to exist anymore. This toolbar would be really useful for components that need front end CRUD. Has anyone figured out how to do this in Joomla 3.2? I'd love to see some example code!
Does anyone know of a 3.x component doing this that I could look at?

Thanks!

Erik

Cristiano Benjamin

unread,
Dec 8, 2013, 11:04:49 AM12/8/13
to joomla-de...@googlegroups.com
In http://docs.joomla.org/J3.2:Using_the_JToolBar_class_in_the_frontend says:

jimport('joomla.html.toolbar');

Just replace this for:


jimport('cms.html.toolbar');




 


2013/12/8 Erik Jorgensen <er...@sci.utah.edu>

Erik

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/groups/opt_out.



--
Cristiano Benjamin
www.vcmat.com
VCMAT - O seu portal da matemática

Erik Jorgensen

unread,
Dec 8, 2013, 9:31:48 PM12/8/13
to joomla-de...@googlegroups.com
Thanks Cristiano,

I think I'm getting closer. The turorial for 3.2 looks like it's just a copy of the 2.5 one and hasn't been updated. I modified the code from the tutorial in a way that seems closer to what 3.x would need but I'm guessing to a large extent. Since JButton is replaced by JToolbarButton in my frontend.php I changed it to JToolbarButtonFrontend extends JToolbarButton. I modified the fetchButton code to construct the right <button> html for how the new buttons are. I also included fetchId and _getCommand methods I stole from Joomla's button class files since it was complaining that they were needed.

Now it's displaying nice pretty frontend buttons, however when I click on them I get a red Joomla error box "0 - Table person not supported. File not found." I have properly included the path to the tables directory so I'm not sure what the problem is - still looking at that. Here's my version of the frontend.php code. Let me know if I'm headed in the right direction.

Thanks!

Erik

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

// import Joomla view library
jimport('cms.html.toolbar');

/**
 * Search Engine Friendly Frontend Buttons
 */
class JToolbarButtonFrontend extends JToolbarButton {
       
        public function fetchButton($type = 'Frontend', $name = '', $text = '', $task = '', $list = true)
        {
                $i18n_text = JText::_($text);
                $class = $this->fetchIconClass($name);
                $doTask = $this->_getCommand($text, $task, $list);
 
                $html = "<button onclick=\"$doTask\" class=\"btn btn-small\">\n";
                $html .= "<span class=\"$class\">\n";
                $html .= "</span>\n";
                $html .= "$i18n_text\n";
                $html .= "</button>\n";
 
                return $html;
        }
       
        /**
     * Get the button CSS Id
     *
     * @param   string  $type  Not used.
     * @param   string  $html  Not used.
     * @param   string  $id    The id prefix for the button.
     *
     * @return  string  Button CSS Id
     *
     * @since   3.0
     */
    public function fetchId($type = 'Frontend', $html = '', $id = 'frontend')
    {
        return $this->_parent->getName() . '-' . $id;
    }
       
        /**
     * Get the JavaScript command for the button
     *
     * @param   string   $name  The task name as seen by the user
     * @param   string   $task  The task used by the application
     * @param   boolean  $list  True is requires a list confirmation.
     *
     * @return  string   JavaScript command string
     *
     * @since   3.0
     */
    protected function _getCommand($name, $task, $list)
    {
        JHtml::_('behavior.framework');
        $message = JText::_('JLIB_HTML_PLEASE_MAKE_A_SELECTION_FROM_THE_LIST');
        $message = addslashes($message);

        if ($list)
        {
            $cmd = "if (document.adminForm.boxchecked.value==0){alert('$message');}else{ Joomla.submitbutton('$task')}";
        }
        else
        {
            $cmd = "Joomla.submitbutton('$task')";
        }

        return $cmd;

Adam Rifat

unread,
Dec 9, 2013, 6:02:26 AM12/9/13
to joomla-de...@googlegroups.com
It will depend on where your controller and model are looking for the /tables folder. 

I think by default it will look in the component so if you are using a front-end component perhaps you need to import the tables folder from an admin component? You probably need to update the model associated with the component, at a guess.

What do you have in your model and where are the table definitions?

Erik Jorgensen

unread,
Dec 9, 2013, 2:30:46 PM12/9/13
to joomla-de...@googlegroups.com
In my main component projects.php file I have:

JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables');

I would expect this to set the correct path to the tables used by the model and controller. Is there something else I need?

Erik
Message has been deleted

Erik Jorgensen

unread,
Dec 9, 2013, 8:00:30 PM12/9/13
to joomla-de...@googlegroups.com
I think there's also some changes needed to the getToolbar() method from what is in the tutorial since the bluestork template is no longer used for the 3.x back end. Also isis lacks the extra style sheets so there's no need for an if-then statement. Here's what I came up with:

    /**
     * Setting the toolbar
     */
        function getToolbar() {
                // add required stylesheets from admin template
                $document = JFactory::getDocument();
                $document->addStyleSheet('administrator/templates/isis/css/template.css');
                //now we add the necessary stylesheets from the administrator template
                //in this case i make reference to the isis default administrator template in joomla 3.2
                $document->addCustomTag( '<link href="administrator/templates/isis/css/template.css" rel="stylesheet" type="text/css" />'."\n\n" );
                //load the JToolBar library and create a toolbar
                jimport('cms.html.toolbar');
                $this->bar = new JToolBar( 'toolbar' );
                //and make whatever calls you require
                $this->bar->appendButton( 'Frontend', 'new', 'New', 'project.add', false );
                $this->bar->appendButton( 'Separator' );
                $this->bar->appendButton( 'Frontend', 'edit', 'Edit', 'project.edit', false );
                $this->bar->appendButton( 'Separator' );
                $this->bar->appendButton( 'Frontend', 'cancel', 'Cancel', 'project.cancel', false );
                $this->bar->appendButton( 'Separator' );
                $this->bar->appendButton( 'Frontend', 'delete', 'Delete', 'project.delete', false );
                //generate the html and return
                return $this->bar->render();

Chris Davenport

unread,
Dec 10, 2013, 10:26:46 AM12/10/13
to joomla-de...@googlegroups.com
I think it would be really helpful and a lot of people would be very grateful if you could update the wiki page with your findings.

Thank you.

Chris.


--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/groups/opt_out.



--
Chris Davenport
Joomla Production Leadership Team

Erik Jorgensen

unread,
Dec 10, 2013, 2:04:08 PM12/10/13
to joomla-de...@googlegroups.com
Chris,

While I still don't have it functioning myself I did update the documentation with what I have so far. Accurate working documentation for this critical functionality would be a huge asset for Joomla extension developers. I find it surprising that Joomla seems to be designed to provide CRUD capability only in the back end. It's seems a major challenge to get it working in the front end, at least using Joomla's native components.

Thanks!

Erik

Erik Jorgensen

unread,
Jan 24, 2014, 7:37:07 PM1/24/14
to joomla-de...@googlegroups.com
After going down this rabbit hole a long way I ultimately changed tracks and had more success with a different example. I think I was pretty close so if someone figures out the last bits please add it to this thread.
I think the best example component with basic front end CRUD (at least partial) is the PACKT Folio component which accompanies the "Learning Joomla! 3 Exension Develpment third edition" book. This book was VERY helpful to me. Go here and click the "Support" tab where you can download the codes. The final version of the Folio component has a rudimentary example of front end CRUD. I was able to build from there to get what I needed.
http://www.packtpub.com/learning-joomla-3-extension-development/book#support

Good luck folks!

Erik
Reply all
Reply to author
Forward
0 new messages