سؤال

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?

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

If you are following Joomla MVC guidance your frontend should be able to pick-up the forms automatically.

In your view (though it should request it from the model actually) you can write:

$formsPath = JPATH_ADMINISTRATOR.DS.'components'.DS.'com_mycom'.DS.'models'.DS.'forms';
$this->form = JForm::getInstance('myform', $formsPath.DS.'myform.xml');

You can also look at the summer of code cm_config project which pulls the config forms and the templateDetails form to the front end using JSON. https://github.com/Buddhima/joomla-cms/tree/gsoc_com_config or the com_services branch.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top