3

I'm working on my own component and i'm trying to add a custom button to my admin view that calls a task.

In view.html.php 

protected function addToolBar() {
JToolBarHelper::custom('addApplicationToDB', 'generic.png', 'generic.png', 'Add Test Application'); 
JToolBarHelper::custom('jobApplications.addApplicationToDB', 'generic.png', 'generic.png', 'Add Test Application'); 
}

At the bottom of my default.php i've added

<input type="hidden" name="task" value="" /> 
<input type="hidden" name="boxchecked" value="0" />     
<input type="hidden" name="controller" value="jobApplications" /> 
<?php echo JHtml::_('form.token'); ?>

Although I don't really understand what it does.

My controller is in admin/controllers/jobApplications.php

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

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

class jobapplicationsControllerjobapplications extends JControllerAdmin {
public function addApplicationToDB() {
    echo "Add Application pressed!!!";
    $mainframe = JFactory::getApplication();
            echo "Add Application pressed!!!";
    $db = JFactory::getDBo();
...

The errors i keep getting is " Uncaught TypeError: Cannot read property 'boxchecked' of undefined "

I'm stumped... I have no idea what is wrong or where to go about fixing it.

You can find the full code : https://github.com/gicadin/com_jobApplications

  • Where are you seeing the error? I just ran the code you have on github on my local 3.4 dev copy and I'm not seeing anything. – Craig Aug 25 '15 at 0:36
  • Sorry I did not commit the faulty code. I was getting the error in the chrome console log. – Gicadin Aug 27 '15 at 7:09
1

Okay a custom button only needs this string in the form.

<input type="hidden" name="task" value="" /> 

The button posts to the controller and there you need to setup the method that this button targets.

So your code code for the addToolbBar method should look like this

JToolBarHelper::custom('controllername.taskname', 'generic.png', 'generic.png', 'Add Test Application'); 

So lets say your controller name is "jobApplications" and your task in the controller is "addApplicationToDB" then it should look like this

JToolBarHelper::custom('jobApplications.addApplicationToDB', 'generic.png', 'generic.png', 'Add Test Application');

In the controller file that is in the controllers folder you need to add the following method

    public function addApplicationToDB()
    {
        // Check for request forgeries
        JSession::checkToken() or die(JText::_('JINVALID_TOKEN'));

        // make sure it is a user
        $user = JFactory::getUser();
        if ($user->id)
        {
            // do your thing
        }
        return;
    }

Here I have a demo component on github that has a custom button implementation take a look also at the controller and other scripts related.

Check the following url for more info, hope this helps

  • I apologize for not answering sooner. I've tried your solition, but i'm getting the exact same error as before. "Uncaught TypeError: Cannot read property 'value' of undefined" So i've decided to try add a custom button in the core joomla file just to see if it works. I've added a JToolbarHelper::custom('article.testButton, '', '', 'test button'); to administrator/components/com_content/views/view.html.php and public function testButton() { .. } to administrator/components/com_content/controllers/article.php Still no luck!!! I feel like im nearly there but missing something – Gicadin Aug 23 '15 at 0:12
  • 1
    Here I have a demo component on github that has a custom button implementation take a look also at the controller and other scripts related. – Llewellyn Aug 24 '15 at 20:32
  • 1
    I was still missing the <form action="..." id="adminForm"> after adding <input type="hidden" name="task" value="" />. I also had another form that used the admin id which was why it took so long to debug. Your demo application helped a lot, with more than just this problem but I've also picked up a few more things from it. Thanks for putting it together, i appreciate the effort!!!! – Gicadin Aug 27 '15 at 7:08
1

I believe here you need to register the task. In your components controller class, the one located in either the front or back end folder root, add the following line inside your display method:

$this->registerTask('jobApplications', 'jobapplications.addApplicationToDB');

This should register the method with Joomla core. I'd also remove the hidden input field with the name attribute "controller" and value of "jobApplications."

Hope this helps!

0

With custom toolbar buttons in Joomla 3, try adding: or in your adminForm (edit view) to avoid javascript error. As always, hit control+u to view the markup and activate your firebug console to watch for errors.

  • {input name=boxchecked value=0 or value=1} – chris Mar 21 '17 at 5:01

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.