Hi,
for the past few hours i've attempted to do the following:
I've a custom content type in drupal 7. It has a textfield that needs to be unique for each created node of that content type. For example a customer id. Of course there can't be more than one customer with the same id.
So, to avoid that I want to use #ajax['callback'] on the form element.
This callback function simply queries the database table for that field with the entered value. If the value is returned, possibly giving a link & title of the node that already contains that customer id, an error message appears and the form can not be submitted, forcing the user inputting the data to enter a different customer id..
Now, I've written a custom module. It creates a field and can be inserted into the custom content type.
Here is the hook_field_widget_form() I used:
function customer_number_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$widget = $element;
$widget['#delta'] = $delta;
switch ($instance['widget']['type']) {
case 'customer_number_field_text':
// fieldset
$element = array(
'#type' => 'fieldset',
'#tree' => TRUE,
);
// customer number field
$element['customer_number'] = array(
'#type' => 'textfield',
'#default_value' => isset($items[$delta]['customer_number']) ? $items[$delta]['customer_number'] : NULL,
'#size' => 10,
'#maxlength' => 8,
'#title' => t('Customer Number'),
'#tag' => 'span',
'#description' => t('Can only be 8 digits long'),
'#weight' => '2',
'#prefix' => '<div class="customer-number-field-wrapper">',
'#suffix' => '</div>',
'#ajax' => array(
'callback' => '_ajax_customer_number_field_is_unique',
'wrapper' => 'customer-unique-div',
'method' => 'replace',
'effect' => 'fade',
),
);
$element['ajax_fieldset'] = array(
// '#title' => t("Generated Checkboxes"),
// The prefix/suffix provide the div that we're replacing, named by
// #ajax['wrapper'] above.
'#prefix' => '<div id="customer-unique-div">',
'#suffix' => '</div>',
'#type' => 'fieldset',
'#description' => t('This is where we get the ajax response'),
);
$element['#prefix'] = '<div class="clearfix">';
$element['#suffix'] = '</div>';
break;
}
return $element;
}As you can see I want to call the '_ajax_customer_number_field_is_unique()' function when the field is clicked off.
here is the '_ajax_customer_number_field_is_unique()' function:
/* CUSTOM FUNCTION FOR AJAX CALLBACK */
/* THIS CHECKS TO SEE IF THE CUSTOMER NUMBER IS UNIQUE */
function _ajax_customer_number_field_is_unique($form, $form_state) {
// Get field name
$fieldname = $form_state['triggering_element']['#name'];
$fieldvalue = $form_state['triggering_element']['#value'];
if($fieldvalue == '123456') {
return 'This customer number: '.$fieldvalue.' already exists!';
} else {
return 'This customer number: '.$fieldvalue.' does not exists! Continue!';
}
/*
// query the database to see if we already have a database entry with this number
// $query = db_query('SELECT `entity_id` FROM `dpl7_field_data_field_customer_number` WHERE `field_customer_number_customer_number` = ');
*/
}
But I have the following issues (or am too much of a n00b to understand it)
1) It works for the first value entered. Lets say I enter 123456 - with the simple check in the callback - I get the correct response. Then I enter a different value, but the output of the callback stays the same, as if the variable $fieldvalue in the function '_ajax_customer_number_field_is_unique()' is set by the first call and not refreshed for any consecutive callbacks made.
2) How do I get the name of the db_table and column so I do not have to hardcode it?
The table name in the database is dependent on the field name when the field is entered into the content type. Is there not a simple way to get it?
It's stupid enough that one needs to use the '$form_state['triggering_element']['#value'];' just to get the value. Is there not a better way, maybe entities or something? Can't I use something like {field_name} or similar like {node} is used?
I've tried to read through as much of the documentation as possible, without success. The example module doesn't do any db_query callbacks in it's #ajax calls and searching here returns a million questions but not really any answers.
Could any kind soul please point me to the right direction?
Thanks in advance!