Note: Question fully updated with the latest findings:
I want to load a com_contact contact view inside a template override of an article. I have a contact item id and trying to load the record and then trying to find a way to load and render its form.
Following @Irata answer, I did try to use the MVCFactory class, to load/render the contact's view. Unfortunately, I can not find a way to populate the Contacts View with the proper data, so I thought I should try with the Contacts Display Controller. But this also has problems.
So:
$comContactMVCFactory = \Joomla\CMS\Factory::getApplication()
->bootComponent('com_contact')
->getMVCFactory();
So, now it's time for $comContactMVCFactory->createController()
. The method requires 5 parameters. The last one $input
, is of Joomla\Input\Input
type. So, I need to pass that type of data there.
$app->input->set('option', 'com_contact');
$app->input->set('view', 'contact');
$app->input->set('id', 25);
$app->input->set('Itemid', 2091);
$app->input->set('layout', 'default');
$input = $app->input;
$comContactMVCFactory = \Joomla\CMS\Factory::getApplication()
->bootComponent('com_contact')
->getMVCFactory();
$config = [
'name' => 'contact',
"base_path" => JPATH_SITE . "/components/com_contact",
'model_path' => JPATH_SITE . "/components/com_contact/forms",
'view_path' => JPATH_SITE . "/components/com_contact/forms",
];
$controller = $comContactMVCFactory->createController('Display', 'Site', $config, $app, $input);
$controller->display();
Here there is one major problem at first.
Τhe $controller
input data is reverted back to the Factory::getApplication()->input
. I checked the constructor of the Display Controller and it contains the following code, which makes the $input variable that we are enforced to pass, as irrelevant, since it always defaults to the current $app->input
.
In simple words - for the Contacts Controller, it's impossible to feed it with custom $input data.
DisplayController Contructor of Contacts
public function __construct($config = [], MVCFactoryInterface $factory = null, $app = null, $input = null)
{
// Contact frontpage Editor contacts proxying.
$input = Factory::getApplication()->getInput();
But let's say that we comment out that line of code and try to see how to achieve this (for experimental causes).
I commented out that line that sets again the $input variable inside the constructor (just for the shake of experiment).
Then the above code produces:
Form::loadForm could not load file
This is the backtrace:
- JROOT/libraries/src/MVC/Model/FormBehaviorTrait.php:120
- Joomla\CMS\MVC\Model\FormModel->loadForm() JROOT/components/com_contact/src/Model/ContactModel.php:107
The path for the contact.xml form is not set correctly and at the moment I can not find a way to set properly the form paths.