Como crear un formulario dentro de un popup usando ctools_modal


Contextualización:

Se necesita un formulario normal con botones Enviar y Cancelar. Pero este formulario debe abrirse de un link externo y aplicando el módulo o funcionalidad Ctools.

Además los botones Enviar (Submit) y Calcelar, deben hacerse con Ajax su funcionalidad.

Manos a la obra:


1. ejemplo.module
//Hook Menú
$menu_items['change_sim'] = array(
    'title' => t('Change SIM'),
'href' => 'ejemplo/nojs/',
'attributes' => array(
'class' => array('ctools-use-modal'),
),
   );



$items['ejemplo/%ctools_js'] = array(
'page callback' => 'ejemplo_changesim_callback',
'page arguments' => array(1),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);

/**
* Ajax menu callback.
*/
function ejemplo_changesim_callback($ajax) {
if ($ajax) {
ctools_include('ajax');
ctools_include('modal');

$form_state = array(
'ajax' => TRUE,
'title' => t('Ejemplo Modal Form'),
);

$output = ctools_modal_form_wrapper('ejemplo_changesim_form', $form_state);

// If the form has been submitted, there may be additional instructions
// such as dismissing the modal popup.
if (!empty($form_state['ajax_commands'])) {
   $output = $form_state['ajax_commands'];
}

// Return the ajax instructions to the browser via ajax_render().
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('ejemplo_changesim_form');
}
}

/**
* Drupal form to be put in a modal.
*/
function tbo3_transactions_changesim_form($form, $form_state) {

global $base_url;

$form = array();
        $form['imsi'] = array(
'#type' => 'textfield',
        '#title' => t('IMSI'),
'#description' => t('Description: You will have a chance to review before submitting.'),
'#required' => TRUE,
'#default_value' => $form_state['values']['imsi'],
'#size' => 15,
'#maxlength' => 15,
'#attributes' => array(
'title' => t("Item's name, no longer than 45 characters."),
'description' => t("Demostracion tooltip"),
'img' => $img_imsi,
),
);
//$form['actions'] = array('#type' => 'actions');
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
$form['cancel'] = array(
'#type' => 'button',
'#value' => t('Cancel'),
'#prefix' => ' ',
);
$form['#submit'][] = 'user_register_submit';

return $form;
}

function ejemplo_changesim_form_validate($form, $form_state){
switch ($form_state['input']['op']){
case t('Save'):
$imsi = $form_state['values']['imsi'];
if ($imsi !== '' && (!is_numeric($imsi) || intval($imsi) != $imsi || $imsi < 0) ) {
form_set_error($imsi, t('The field IMSI must be numeric'));
}
break;
}

function tbo3_transactions_changesim_form_submit($form, $form_state){
global $user;
        // Getting variable
$language = language_default();
$imsi = $form_state['values']['imsi'];
//saveLog();
        // Aquí llamar el metodo o función que desee, insert o guardar cualquier cosa
drupal_set_message(t('Change has been done sucessfully.'));
}

Comentarios

Entradas populares