weixin_33720078 2012-11-26 05:58 采纳率: 0%
浏览 37

Drupal 7 Block配置AJAX

I'm building a custom block programmatically that has two select boxes. The first one is populated automatically and the second is populated based on the value of the first. Ideally I'd like to use AJAX and I have experience integrating AJAX in a standard form, however the block configuration form doesn't include a $form_state variable and seems to function differently. Can the standard method be used?

'#ajax'               => array(
  'callback'          => 'my_callback',
  'wrapper'           => 'the-id',
  'method'            => 'replace',
  'effect'            => 'fade',
),

How would the callback work?

Thanks, Howie

  • 写回答

1条回答 默认 最新

  • weixin_33738555 2014-04-15 17:28
    关注

    I didn't try this but I bet it works: use hook_form_alter() to access your block configuration form. There, you will have a &$form_state and be able to do fancy AJAX stuff (see this). The hard part is to ONLY alter YOUR form at the alter-hook. Possible ways:

    Not sure if this works (most elegant way):

     function mymodule_form_alter(&$form,&$form_state,$form_id) {
          if ($form_id == 'block_admin_configure' ) {
            // Find the delta in the $form variable
            if ($form['delta'] == 'the_delta_you_are_looking_for') {
             //do fancy ajax stuff
            }
          }
        }
    

    Ugly but definitely possible:

    function mymodule_form_alter(&$form,&$form_state,$form_id) {
      if ($form_id == 'block_admin_configure' && arg(4) == 'mymodule') {
           //do fancy ajax stuff
        }
      }
    }
    

    Even uglier but also possible:

    function mymodule_block_configure($delta = '') {      
        $form = array();
        if ($delta == 'my_block') {
            $form["my_block_change_this"] = array(
                "#type" => "hidden",
                "#value" => "lalala",
            )
        }
    }
    
    function mymodule_form_alter(&$form,&$form_state,$form_id) {
        if ($form_id == 'block_admin_configure' ) {
            if (!empty($form['my_block_change_this'])) {
                //do fancy ajax stuff
            }
        }
    }
    

    Tip: Print out the form_state-array (at the alter hook) and see what's there (That's always the first thing I do when I run into FAPI-Issues). Hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配