简体   繁体   English

如何通过AHAH Drupal 6传递辩论?

[英]how to Pass arguements via AHAH Drupal 6?

I am trying to create a module that analyzes user content and score it using third party API. 我正在尝试创建一个模块,该模块可以分析用户内容并使用第三方API对其进行评分。 Here is the basic logic: Module passes article title and content to the API and API returns the result which is displays on the edit node page. 这是基本逻辑:模块将文章标题和内容传递给API,API返回结果,该结果显示在编辑节点页面上。 So far this works fine. 到目前为止,这很好。

Now I am trying to get this done via AHAH. 现在,我正在尝试通过AHAH完成此操作。 When user clicks a button it should display the API result right away. 用户单击按钮时,应立即显示API结果。 Problem is how can I pass title and content to API via AHAH ? 问题是如何通过AHAH将标题和内容传递给API? Before I could do this by calling another php function and passes title and article content as functions parameter. 在我可以通过调用另一个php函数并将标题和文章内容作为函数参数传递之前,执行此操作。

Here is what I have so far. 这是我到目前为止所拥有的。

hook_form_alter that edits the node form and adds the button to triggers AHAH. hook_form_alter,用于编辑节点表单并添加按钮以触发AHAH。

function ar_form_alter(&$form, &$form_state, $form_id) {
    $title = $form['title']['#default_value']; 
    $content = $form['body_field']['body']['#default_value'];

    $form['arPost']['aranalyze'] = array(
                '#type' => 'image_button',
                '#src' => drupal_get_path('module', 'ar') . '/inc/images/analyze-button.png',
                '#description' => t("Click to score this content."),
                '#prefix' => '<div id="ax">',
                '#suffix' => '</div>',
                '#weight' => 30,
                '#ahah' => array(
                    'path' => 'admin/settings/getscore',
                    'method' => 'after',
                    'wrapper' => 'ax',
                ),
            );

}

Hook_menu that is the middle man for this AHAH call. Hook_menu是此AHAH呼叫的中间人。 (how to pass article content from hook_form_alter function ? (如何通过hook_form_alter函数传递文章内容?

function ar_menu() {
$items['admin/settings/getscore'] = array(
        'page callback' => 'ar_test',
        'access arguments' => array('access content'),
         'type' => MENU_CALLBACK,
    );

    return $items;
}

This is where Module make the call to API and sends back the HTML result to display. 这是Module调用API并发回HTML结果进行显示的地方。

function ar_test() {
    // should be function ar_test($title, $content){
    // Should pass $article and $content to another php function to initiate the call
    // $output = ar_api_call($title, $content);

    print drupal_json(array('status' => TRUE, 'data' => $output));
}

Now I need to find a way to pass the data(as arguments) from hook_form_alter to ar_test function. 现在,我需要找到一种将数据(作为参数)从hook_form_alter传递到ar_test函数的方法。

Please advise. 请指教。

您应该能够仅通过$_POST超全局变量访问表单值。

Here is the solution: 解决方法如下:

From hook_form_alter() 从hook_form_alter()

'#ahah' => array(
                    'path' => 'getscore/'.$value_variable,

From hook_menu() 从hook_menu()

 $items['getscore/%'] = array(
        'page callback' => 'ar_test',
        'access arguments' => array('access content'),
        'page arguments' => array(1),
         'type' => MENU_CALLBACK,
    );

    return $items;

getsore/% The % sign takes the value passed be the hook_form_alter and sends it to page_callback via page argumet. getsore /%%符号采用通过hook_form_alter传递的值,并将其通过页面argumet发送到page_callback。

And then simply recieves the argument as 然后简单地将参数接收为

ar_test($var=0)

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM