I had similar situation this week.
I've made two functions - one for the form template tag and second is action callback that works with form data.
In this manner I could output information with ee()->output->show_message()
. Like this:
$data = array(
'title' => 'Search Results',
'heading' => 'Search Results',
'content' => "content as a string. wrapped as system message"
);
ee()->output->show_message($data);
That was not what I was looking for :( So I've switched to post->redirect->get pattern and added another template tag for showing search results.
In general, now I have 3 methods inside base module class:
search_form
constructs html for the form and returns it
search_submit
is processing data passed after form submit, saves results into $_SESSION
variable and redirects user to template with search results tag.
search_results
reads data from $_SESSION
, creates $variables
array and returns it with ee()->TMPL->parse_variables(ee()->TMPL->tagdata, $variables)
.
class Custom_search {
function search_form() {
$action_id = ee()->functions->fetch_action_id('Custom_search', 'search_form_submit');
$form_data = array(
'action' => ee()->functions->create_url(ee()->uri->uri_string),
'hidden_fields' => array(
'ACT' => $action_id,
'return_url' => ee()->functions->create_url('template_group/search_results')
)
);
$tag_data = ee()->TMPL->tagdata;
$form = ee()->functions->form_declaration($form_data) . $tag_data . "</form>";
return $form;
}
function search_form_submit() {
//do something with input
session_start();
$_SESSION['custom_search']['query'] = "search query here";
$_SESSION['custom_search']['results'] = $results;
ee()->functions->redirect(ee()->input->post('return_url'));
}
function search_result() {
session_start();
//create $vars array from your results here
session_unset();
session_destroy();
return ee()->TMPL->parse_variables(ee()->TMPL->tagdata, $vars);
}
}
And you'll have two template files - one with search form template tag
{exp:custom_search:search_form}
<input type="text" placeholder="Search..." name="query" />
<input type="submit" name="submit" value="go" />
{/exp:custom_search:search_form}
and another with results tag
{exp:custom_search:search_result}
<p>Your query: {query}</p>
<p>Results found: {total_results}</p>
{results}
<h4>{title}</h4>
<p>{description}</p>
{/results}
{/exp:custom_search:search_result}