【问题标题】:Drupal AJAX button on user profile用户配置文件上的 Drupal AJAX 按钮
【发布时间】:2014-01-19 08:23:49
【问题描述】:

我一直在尝试添加 AJAX 按钮。我可以在表单上执行此操作,如下所示:

function hook_form_alter(&$form, &$form_state, $form_id) {
$form['suspend'] = array(
            '#type' => 'button',
            '#name' => 'foo',
            '#value' => t('bar'),
            '#ajax'  => array('callback' => '_foo_bar'),
        );

return $form;
}

工作正常。但是我无法让它在用户配置文件或非表单上工作,如下所示:

function hook_user_view_alter(&$build) {
$build['suspend'] = array(
            '#type' => 'button',
            '#name' => 'foo',
            '#value' => t('bar'),
            '#ajax'  => array('callback' => '_foo_bar'),
        );

return $build;
}

有没有简单的方法来做到这一点?我在这个网站上使用块和视图,如果可能的话,我宁愿不用安装面板(:

谢谢!

【问题讨论】:

    标签: ajax forms button drupal


    【解决方案1】:

    如果不在表单中,则将其包装到表单中:

    function example_suspend_form($form, &$form_state) {
      $form['suspend'] = array(
        '#type' => 'button',
        '#name' => 'foo',
        '#value' => t('bar'),
        '#ajax'  => array('callback' => '_foo_bar'),
      );
    
      return $form;
    }
    
    function example_user_view_alter(&$build) {
      $build['example_suspend_form'] = drupal_get_form('example_suspend_form');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多