2011-11-14 13 views
5

tengo que hacer esto, pero con las formas de Drupal:¿Cómo hago un botón de retroceso con la API de formulario de Drupal?

<input type="button" class="button-user" value="Back" onclick="location.href='edit'"/> 

He intentado hacer esto, pero no funcionó:

$form['back']['#prefix'] = "<input type='button' class='button-user' value='Back' onclick='location.href='edit''/>; 

y también:

$form['back'] = array(
    '#type' => 'button', 
    '#value' => 'Back', 
    '#attributes' => array(
    'class' => 'button-user', 
    'onclick' => 'location.href=edit',   
    )  
); 
+0

nunca se cierre la primera con comillas '" ' – switz

Respuesta

4
$form['back']['#markup'] = "<input type='button' class='button-user' value='Back' onclick='location.href=\'edit\''/>"; 
+0

funciona para mí:. D THX –

+0

eres :) recepción –

+0

También, no se olvide de establecer esto como la respuesta aceptada :) –

0

Otro La solución sería esta:

function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id) { 
    switch($form_id) { 
    case "YOUR_FORM_ID": 

     unset($form['#validate']); //Maybe necessary 

     $form['actions']['back'] = array(
     "#type" => "button", 
     "#value" => t('Back'), 
     "#weight" => 15, 
     "#executes_submit_callback" => FALSE, 
     '#limit_validation_errors' => array(), 
     '#ajax' => array(
      'callback' => 'YOUR_MODULE_go_back_callback' 
     ) 
    ); 

     break; 
    default: 
     break; 
    } 
} 

function YOUR_MODULE_go_back_callback() { 
    $html = ' 
    <script type"text/javascript"> 
    window.history.go(-1); 
    </script> 
    '; 
    return $html; 
} 
3
$form['back']['#markup'] = "<input type='button' class='button-user' value='Back' onclick='window.history.go(-1)'/>"; 

Esto funciona para cualquier página.

1

Simplemente agregando mi versión, que parece funcionar muy bien en 7, solo póngala en el ciclo de reconstrucción y redirija en su lugar. Extensible, puede agregar cualquier otro botón para hacer cosas, tenga en cuenta la ortografía del valor "Atrás" es el nombre de la "operación" (op) ... algo que me confundió y molestó hasta que lo descubrí.

function mymodule_something_form($form,&$form_state){ 

    //... Rest of form 

    $form['unused_form_id_back'] = array(
     '#type' => 'button', 
     '#value' => 'Back', 
    ); 
    $form['submit'] = array(
     '#type' => 'submit', 
     '#value' => 'Do Stuff!' 
    ); 
    return $form; 
} 

function mymodule_something_form_validate($form, &$form_state) 
{ 
    if($form_state['values']['op'] == 'Back'){ 
     drupal_goto('something/that_page'); 
    } 
} 
+0

Gracias por aclarar el problema "op". Eso también me molestó. – zkent

0

Preferí una solución que no requería JavaScript. Similar a la respuesta de Grizly, pero sin ponerlo en la validación de la forma, que se sentía feo. Pero el siguiente código ofrece un botón de enlace. opción más simple

function my_form(&$form, &$form_state) { 
    // Some form elements 

    // Regular submit button 
    $form['actions']['submit'] = array(
    '#type' => 'submit', 
    '#value' => t('Submit'), 
); 

    // Back button (different submit handler prevent the standard submit and takes us 
    // to the redirect-submit). 
    $form['actions']['back'] = array(
    '#type' => 'submit', 
    '#value' => t('Go back'), 
    '#submit' => array('custom_back_button'), 
); 
} 

// Custom form callback for redirection. 
function custom_back_button($form, &$form_state) { 
    $form_state['redirect'] = '/edit'; 
} 
1

en la API de Drupal Forma, Uso de #attributes opción.

$form['back-btn'] = array(
    '#type'     => 'button', 
    '#value'    => t('Back'), 
    '#attributes'   => array('onclick' => onclick='window.history.back();'), 
); 
+1

Mi pregunta era originalmente para drupal 6 hace 4 años, pero creo que podría ser útil para otros, gracias. –

+0

$ form ['back-btn'] = array ( '#type' => 'botón', \t \t '#value' => t ('Atrás'), '#attributes' => array ('onclick' => 'window.history.back();'), ); – SushilKumar

0

Este es el botón de retroceso que estoy usando. El "return false" evita el envío del formulario.

$form['back'] = array(
    '#type' => 'button', 
    '#value' => t('<< Back'), 
    '#attributes' => array(
    'onclick' => 'window.history.back();return false;', 
), 
); 
Cuestiones relacionadas