Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By dims971 on
I'm creating a module with differents forms. I have some forms which are modal forms using ctools.
On one of these modal forms i made a button to open a pdf on a new tab in my browser but it doesn't work.
I have a AJAX error.
This is my code:
function liste_apprenant_partenaire($js){
if ($js) {
ctools_include('ajax');
ctools_include('modal');
ctools_add_js('ajax-responder');
$form_state = array(
'ajax' => TRUE,
);
$output = ctools_modal_form_wrapper('liste_apprenant_partenaire_form', $form_state);
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('liste_apprenant_partenaire_form');
}
}
function liste_apprenant_partenaire_form($form, &$form_state){
$form['pv-button'] = array(
'#id' => 'pv-button',
'#type' => 'submit',
'#value' => 'PV',
'#submit'=>array('pv_apprenant_partenaire_button_form_submit'),
'#attributes' => array('onclick' => 'target="_blank";return true;'),
);
return $form;
}
function pv_apprenant_partenaire_button_form_submit(&$form, &$form_state){
require_once("sites/all/modules/print/lib/dompdf/autoload.inc.php");
$node_session=node_load($_SESSION['nid_session']);
$type_formation=$node_session->field_type[LANGUAGE_NONE][0]['value'];
$test=file_get_contents('sites/all/modules/multi_example/test.html');
$test=str_replace('+type_formation',"$type_formation",$test);
$dompdf = new Dompdf();
$dompdf->loadHtml($test);
$dompdf->render();
$dompdf->stream('my.pdf',array('Attachment'=>0));
}
When i put the path of the node in my browser, it's worked fine because the form appears normally not as a modal form.
But i research the same result using a modal form. So i think there is a problem with AJAX but i don't find the real solution.
Comments
This is an ajaxified form
for achieving this target, follow below steps.
1. Provide a link in the form instead of button.
2. After creating callback and function, show the pdf.
3. Done.
Thank you a lot.
Thanks your comment i have found a solution even if (actually) the pdf is just dowloaded when i click on the link i've created.
But i can generate a pdf from my modal form ! It's great !
I follow your step, i have created a new item in my hook_menu like that:
I put in my form['pv-button'] the link type instead of the submit type like that:
And my function callback which generate the pdf looks like that:
I will try to find a solution to display the generated pdf on a new tab after (this will be my ideal solution) but if you have some suggestions about that, you are welcom :-)
But essentially it's working !!! Thank you a lot !!!