1

Brief

I have a component and have a form in it. And I want to render the same form in a module.

An example can be core Joomla article edit form components/com_content/views/form/tmpl/edit.php which is rendered based on components/com_content/models/forms/article.xml

As far as I can see it's not possible to load a component view in the module in this case. Am I right?

One option

I see is to use an AJAX to get my form from my component loaded like ?option=com_mycomponent&view=myform&tmpl=component and to inject the from HTML into my main page DOM. An ugly way. So I'd not like to use this way. Am I right?

Another way

So I want to be DRY in the module.

  1. I don't want to repeat view part (for example load XML form and bind data to it). How to stay DRY in the module with the view part?

  2. Don't repeat the template part rendering HTML (/views/form/tmpl/edit.php) Here the things are simpler. I can use JLayouts instead of the tmpl/default.php approach in my component. So I can reuse the layouts anywhere (in a module, a plugin).

What I beg for

Please, tell me how to behave in the most proper way in this case. Or give me a component->module or a component->template example which implements such an approach. So I can investigate the code.

P.S. I wonder why mod_login uses own form template instead of reusing the same component view. That would be a nice example for guys like me.

3

This is code I write time ago for Joomla 1.5, you will need adapt it for jooma 3.x (JRequest -> JInput, ...), and surely there is more elegant way to do this.

This is the basic part of module.php

My component is 'com_alojamiento', the controller is 'alojamientoController'

Get current option, view, layout, ... to restore it after render.

$optionant = JRequest::getCmd('option',null);
$view = JRequest::getCmd('view',null);
$layout = JRequest::getCmd('layout',null);
$task = JRequest::getCmd('task',null);

Then set your parameters, option, view, layout and wathever you need, ids,...

$yourcomponent = '';
$yourview = '';
$yourlayout = '';
$yourtask = '';

Set it in JRequest (JInput in 3.x)

JRequest::setVar('option', $yourcomponent); 
JRequest::setVar('view', $yourview);
JRequest::setVar('layout', $yourlayout);
JRequest::setVar('task', 'yourtask');

Set additional parameters like id, customer_id, etc.

Language files

$lang = JFactory::getLanguage();
$lang->load('com_alojamiento', JPATH_SITE);

Load the controller

if (!class_exists('alojamientoController')) {
    require_once (JPATH_SITE .DS. 'components' .DS. 'com_alojamiento' .DS. 'controller.php');
}
$controller = new alojamientoController();
$controller->addModelPath(JPATH_SITE .DS. 'components' .DS. 'com_alojamiento' .DS. 'models' .DS);

$controller->setProperties(array(
    '_basePath' => JPATH_SITE .DS. 'components' .DS. 'com_alojamiento',
    '_path' => array(
        'view' => array(
            JPATH_SITE .DS. 'components' .DS. 'com_alojamiento' .DS. 'views'
            ),
        'model' => array(
            JPATH_SITE .DS. 'components' .DS. 'com_alojamiento' .DS. 'models'
            )
        )
    ));

And render

ob_start();
$controller->execute($task);
$salida = ob_get_contents();
ob_end_clean();

After render, restore current values

JRequest::setVar('option', $optionant);
JRequest::setVar('view', $view);
JRequest::setVar('layout', $layout);
JRequest::setVar('task', $task);

Component view contents are in $salida variable to use in your module tpl

  • perfect, you made my day. – sunny kashyap Jun 5 '17 at 16:08
0

When I want to use a component in a module I use Regular Labs's Component Anywhere extension.

https://www.regularlabs.com/extensions/componentsanywhere

Perhaps that could be an example for you to review.

HTH

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.