Hi there,
I am pulling data from a separate Content Management Framework (CMF). In the action, I call a method on a class:
$forms->process_form_insert(...)
that searches in the body copy for a special syntax that denotes a form insert like so:
[[form name=minimal_business_contact]]
If this is found, it takes the name value and tries to translates it into a Phalcon Form object. The templates for such forms are held in a /app/views/forms folder and the name of the specific template is returned from a method on the Form instance.
The template is processed into the HTML form markup, and injected into the body markup from the CMF. All of this markup is then returned to the action that called it.
The problem is that the calling action seems to stop rendering it's own template? If I don't call:
$forms->process_form_insert(...)
the template (with its unprocessed form inserts) renders perfectly. If I do call:
$forms->process_form_insert(...)
even though all of the correct markup is produced and returned to the action, nothing renders and there are no errors reported.
Here is the code from the action:
/**
* Main index action
*/
public function indexAction()
{
// Get body markup from separate CMF
$body = $this->getPage()->get('body');
// Get instance of my own 'Forms' class
$forms = $this->di->get('forms');
// Look in the $body markup from the CMF and see if there are any HTML form inserts
$form_data = $forms->get_form_inserts($body);
// If there is one called 'MinimalBusinessContactForm', process it
if (isset($form_data['MinimalBusinessContactForm'])) {
$body = $forms->process_form_insert([
'markup' => $body,
'form' => 'MinimalBusinessContactForm',
'insert' => $form_data['MinimalBusinessContactForm'],
'config' => ['business_name' => 'Agency']
]);
}
// Assign the $body markup with process HTML form
$this->view->body = $body;
}
Here is the code from $forms->process_form_insert(...)
/**
* Process the form insert with any associated data
*
* @param array $params
*
* @return string
*/
public function process_form_insert($params)
{
if (isset($params['markup'])) {
$markup = $params['markup'];
}
if (isset($params['form'])) {
$form = $params['form'];
}
if (isset($params['insert'])) {
$insert = $params['insert'];
}
if (isset($params['config'])) {
$config = $params['config'];
} else {
$config = [];
}
if (isset($params['entity'])) {
$entity = $params['entity'];
} else {
$entity = null;
}
$form = new $form($entity, $config);
$view = $this->getDI()->get('view');
$view->form = $form;
$view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_ACTION_VIEW);
$view->start();
$view->render('forms', $form->getTemplate());
$view->finish();
$form_markup = $view->getContent();
$markup = str_replace($insert, $form_markup, $markup);
return $markup;
}
Just to be clear, I get, an empty, blank page. If I do the following in the action:
/**
* Main index action
*/
public function indexAction()
{
// Get body markup from separate CMS
$body = $this->getPage()->get('body');
// Get instance of my own 'Forms' class
$forms = $this->di->get('forms');
// Look in the $body markup from the CMS and see if there are any HTML form inserts
$form_data = $forms->get_form_inserts($body);
// If there is one called 'MinimalBusinessContactForm', process it
// if (isset($form_data['MinimalBusinessContactForm'])) {
//
// $body = $forms->process_form_insert([
// 'markup' => $body,
// 'form' => 'MinimalBusinessContactForm',
// 'insert' => $form_data['MinimalBusinessContactForm'],
// 'config' => ['business_name' => 'Agency']
// ]);
//
// }
// Assign the $body markup with process HTML form
$this->view->body = $body;
}
I get the page from the CMF rendered, apart from the fact that the HTML form insert(s) are unprocessed.
Any ideas why calling the form processing method stops the action from rendering? I've spent days trying to get this to work without success :(
Thanks.