Hello fellow Drupalers,
I need help.
I am writing a module called "ActiveField". In summary, the purpose of this module is to (eventually) be able to define source fields in a CCK content type (e.g. "Location") and specify the target fields in another CCK content type (e.g. "Field Trip") that are updated based on the a user defined list or .sql query. The goal is to have this be done via Drupal Admin web interface
and to support a many to many relationship between source fields and target fields. The original blog article that
I wrote on this is located at: http://www.sageofcode.com/?p=59
I have ported this module over from Drupal 5.x to Drupal 6.x based on some great resources:
1. Pro Drupal Development, 2nd Ed. (http://www.drupalbook.com/)
2. Drupal 6 JavaScript and jQuery (http://www.packtpub.com/drupal-6-javascript-and-jquery/book)
3. Learning jQuery (http://www.packtpub.com/jQuery/book/mid/100407j4kh3d)
4. jQuery in Action (http://www.manning.com/bibeault/)
5. "Using Ajax in Drupal" 6 by Caroline Schnapp. (http://11heavens.com/using-ajax-in-Drupal-6)
Below is the source code. In short, the slippery bug that I am trying to squash has to do with the fact that the
$_GET[] array is not being updated with the values that are being sent down by the $.get() AJAX request
(which I can see via FireBug). I know the array is empty based on some code-level tests that I ran and the fact that when I append "/ActiveField/field-trip" to the site's url, the following is returned:
returns: { "one": null, "two": null }
Yes, I have a QA back ground ;-).
Once I get the basic version working (i.e. code level only), I will create a Contrib module project.
I have actually spent more hours debugging and than porting the code. :-(.
<<ActiveField.module>>
<?php
//$id$
/**
* @file
* A dynamic source-to-target field module via Drupal, Ajax, and jQuery
*
*/
/**
*
* Implementation of hook_perm().
* Adding the "Use ActiveField" permission to Drupal's role-based access control page.
* Prevents Anonymous use.
*
*/
function ActiveField_perm()
{
return array('Use ActiveField');
}
/**
* Implementation of hook_menu()
* Allows for the mapping for jQuery intercepted URL to a Drupal PHP function
* The Drupal PHP function will return the child field's value(s) to jQuery in JSON
*/
function ActiveField_menu()
{
$items['ActiveField/field-trip'] = array(
'page callback' => 'ActiveField_getTargets',
'access arguments' => array('Use ActiveField'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
*
* Implementation of hook_form_alter()
*
*/
function ActiveField_form_alter(&$form, $form_state, $form_id)
{
if ($form_id == 'field_trip_type_node_form')
{
drupal_add_js(drupal_get_path('module', 'ActiveField') .'/ActiveField.js');
}
}
/**
*
* Called by jQuery
* Submits the source field value and returns the target field's value(s) as a JSON
*/
function ActiveField_getTargets()
{
// Getting the value of the Date source feild
// NOTE: .sql queries are not being generated based on this field yet.
$field_date_1_value = check_plain($_GET['sDate']);
// Getting the value of the location source feild and the node id of the corresponding content type
$location_value = check_plain($_GET['sLocation']);
$sql_location ="SELECT nid FROM {node} WHERE title='$location_value'";
$nid_location = db_result(db_query($sql_location));
// SQL query for getting target field one's value (i.e. sunrise)
$sql_sunrise="SELECT field_sun_rise_value FROM {content_field_sunrise} WHERE nid = '$nid_location'";
// SQL query for getting target field two's value (i.e. sunset)
$sql_sunset="SELECT field_sun_set_value FROM {content_field_sunset} WHERE nid = '$nid_location'";
// Getting Target Field one's value (i.e. sunrise)
$results_sunrise = db_query($sql_sunrise);
while ($data = db_fetch_object($results_sunrise))
{
$result1 = $data-> field_sun_rise_value;
}
// Getting Target Fields two's value (i.e. sunset)
$results_sunset = db_query($sql_sunset);
while ($data = db_fetch_object($results_sunset))
{
$result2 = $data-> field_sun_set_value;
}
// Returning Target Fields' values to jQuery as a JSON
print drupal_json(array (
'one' => $result1,
'two' => $result2
)
);
exit();
}
<<ActiveField.js>>
/** ActiveField.js
*
* JavaScript file with jQuery
* Used by ActiveField.module
*
*/
//$Id$
if(Drupal.jsEnabled)
{
$(document).ready(function()
{
// Source field & respective event
// Adding change event to source Field
$('#edit-field-location-value').change(function(event)
{
// Getting source fields' values
var SourceDate = $('#edit-field-date-0-value-datepicker-popup-0').val();
var SourceLocation = $('#edit-field-location-value').val();
//AJAX call
$.get('ActiveField/field-trip',
{sDate: SourceDate, sLocation: SourceLocation},
function (json)
{
// Setting tareget fields' values
$('#edit-field-sun-rise-0-value').val(json.one);
$('#edit-field-sun-set-0-value').val(json.two);
}
);
// preventing entire page from reloading
return false;
}
);
}
);
}
Comments
a wild guess.... access
a wild guess....
access control
good luck
---------------------------------------------------------
toll free is good, but pay toll may go faster ...
we go the drupalway. how about u?
www.drupalway.com - pay toll to go faster
Access permissions set
mm167,
Thanks for the feedback!
I set the access permissions for a user role that I created.
Logged in a user with the respective user role, but the result is the same.
Do you have any other thoughts?
I will continue debugging and post back once I have more information.
I dont have all of your codes
I dont have all of your codes ..so, I just can give u the following little suggestion.
try temporary remove the access control from your hook menu section.
i.e. allow everyone to call the callback function.
use this : 'access callback' => true,
then re-do the test. see if u can get what u expected in the ajax get().
if the result is Negative, forget my suggestion.
good luck.
---------------------------------------------------------------------------------
The bartender says, There's no free beer. Will u buy me a beer?