Search code examples
formsmodel-view-controllerjoomlareusability

Joomla: how can I use one form for frontend and backend view?


I am creating a Joomla 2.5 component. In the backend I created a model/view/controller 'Members' which shows a grid. I also created an MVC 'Member' which is used to add or edit a member from the grid. So far so good.

Now, I would like to add a frontend view that is very similar to the 'Member' view in the backend, but this one is meant for visitors so they can subscribe themselves. It has to look more user friendly than the backend form, so I will create a slightly different 'Member' view in the frontend, but I would really like to reuse the form file (/administrator/components/mycomponent/models/forms/member.xml) from the backend!

So, my question is how my frontend view can find and use that backend form?


Solution

  • You definitely have to load it in the model. Your model has to extend JModelAdmin and then the getForm function has to load the form

    public function getForm($data = array(), $loadData = true) {
        // Get the form.
        JForm::addFormPath(JPATH_COMPONENT_ADMINISTRATOR . '/models/forms');
        JForm::addFieldPath(JPATH_COMPONENT_ADMINISTRATOR . '/models/fields');
        $form = $this->loadForm('com_dpattachments.attachment', 'attachment', array('control' => 'jform', 'load_data' => $loadData));
        if (empty($form)) {
            return false;
        }
        ....
    }
    

    I'm using the same approach in my DPAttachments component, it is for Joomla 3.1 but the main code, to use the same model and form on the front and back, should also run on Joomla 2.5. Here is the link to the getForm function https://github.com/Digital-Peak/DPAttachments/blob/master/com_dpattachments/admin/models/attachment.php#L102