Question

I am trying to create a module that analyzes user content and score it using third party 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. So far this works fine.

Now I am trying to get this done via AHAH. When user clicks a button it should display the API result right away. Problem is how can I pass title and content to API via AHAH ? Before I could do this by calling another php function and passes title and article content as functions parameter.

Here is what I have so far.

hook_form_alter that edits the node form and adds the button to triggers 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. (how to pass article content from hook_form_alter function ?

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.

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.

Please advise.

Was it helpful?

Solution 2

Here is the solution:

From hook_form_alter()

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

From 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.

And then simply recieves the argument as

ar_test($var=0)

Cheers

OTHER TIPS

You should be able to simply access the form values from the $_POST superglobal variable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top