【问题标题】:How do I make a back button with Drupal form API?如何使用 Drupal 表单 API 制作后退按钮?
【发布时间】:2011-12-28 23:53:42
【问题描述】:

我需要这样做,但使用 Drupal 表单:

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

我试过这样做,但没有成功:

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

还有:

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

【问题讨论】:

  • 你永远不会用引号"关闭第一个。

标签: forms api drupal button onclick


【解决方案1】:
$form['back']['#markup'] = "<input type='button' class='button-user' value='Back' onclick='location.href=\'edit\''/>";

【讨论】:

  • 使用 Drupal Form API 查看我的答案
【解决方案2】:
$form['back']['#markup'] = "<input type='button' class='button-user' value='Back' onclick='window.history.go(-1)'/>";

这适用于任何页面。

【讨论】:

    【解决方案3】:

    只需添加我的版本,它似乎在 7 中运行良好,只需在重建周期中捕获它并重定向即可。可扩展,可以添加任何其他按钮来做事,注意值“Back”的拼写是“op”(操作)的名称。在我弄明白之前,这让我感到困惑和恼火。

    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');
        }
    }
    

    【讨论】:

    • 感谢您解决“操作”问题。这也困扰着我。
    【解决方案4】:

    Drupal Form API 中最简单的选项,使用 #attributes 选项。

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

    【讨论】:

    • 我的问题最初是 4 年前的 drupal 6,但我认为它可能对其他人有用,谢谢。
    • $form['back-btn'] = array( '#type' => 'button', '#value' => t('Back'), '#attributes' => array ('onclick' => 'window.history.back();'), );
    【解决方案5】:

    另一个解决方案是这个:

    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;
    }
    

    【讨论】:

      【解决方案6】:

      我更喜欢不需要 JavaScript 的解决方案。类似于 Grizly 的答案,但没有将其放入表单验证中,这感觉很难看。但是下面的代码提供了一个链接按钮。

      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';
      }
      

      【讨论】:

        【解决方案7】:

        这是我正在使用的后退按钮。 “return false”避免了表单提交。

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

        【讨论】:

          猜你喜欢
          • 2018-08-02
          • 2014-07-31
          • 1970-01-01
          • 2017-06-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-04-26
          • 1970-01-01
          相关资源
          最近更新 更多