Skip to content

Commit

Permalink
#178. Changed _drupalgap_form_render_element() to iterate over field …
Browse files Browse the repository at this point in the history
…element item deltas and pass them along to hook_field_widget_form(). Added start to text_field_widget_form().
  • Loading branch information
signalpoint committed Sep 10, 2013
1 parent d9e0824 commit 4985e94
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 22 deletions.
79 changes: 57 additions & 22 deletions includes/form.inc.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,19 +378,67 @@ function _drupalgap_form_render_element(form, element) {
// Extract the element name.
var name = element.name;

// Grab the language.
var language = drupalgap.settings.language;

// Generate default variables.
var variables = {
attributes:{
'id':element.id
}
attributes:{}
};

// If this element is a field, grab the info instance and info field for the
// field, then attach them both to the variables object so all theme
// functions will have access to that data.
if (element.is_field) {
variables.field_info_field = element.field_info_field;
variables.field_info_instance = element.field_info_instance;
// Grab the info instance and info field for the field, then attach them
// both to the variables object so all theme functions will have access
// to that data.
variables = {
field_info_field:element.field_info_field,
field_info_instance:element.field_info_instance
};
// What's the cardinatlity of this field?
var cardinality = parseInt(element.field_info_field.cardinality);
if (cardinality == -1) {
cardinality = 1; // we'll just add one element for now, until we
// figure out how to handle the 'add another
// item' feature.
}
// What module handles this element?
var module = element.field_info_instance.widget.module;
dpm(module);
dpm(cardinality);
// Determine the hook_field_widget_form() function for this field, then
// call it. Build the widget arguments into an array so we can apply them
// to the widget function.
var field_widget_form_function = module + '_field_widget_form';
if (drupalgap_function_exists(field_widget_form_function)) {
var fn = window[field_widget_form_function];
var widget_arguments = [];
widget_arguments.push(form);
widget_arguments.push(null);
widget_arguments.push(element.field_info_field);
widget_arguments.push(element.field_info_instance);
widget_arguments.push(language);
widget_arguments.push(form.elements[name][language]);
widget_arguments.push(0);
widget_arguments.push(element);
for (var delta = 0; delta < cardinality; delta++) {
widget_arguments[6] = delta;
fn.apply(null, widget_arguments);
}
}
else {
console.log('WARNING: _drupalgap_form_render_element() - ' + field_widget_form_function + '() does not exist!');
}
/*if (variables.field_info_instance) {
using_field_widget_form = true;
// Grab the field widget implementor function, then call it.
}
}*/
}
else {
variables.attributes.id = element.id;
}

// If there wasn't a default value provided, set one. Then set the default
Expand Down Expand Up @@ -555,19 +603,6 @@ function _drupalgap_form_render_element(form, element) {
delete variables.attributes.value;
break;
default:
// This form element isn't known to DrupalGap core. Does the widget's
// module implement hook_field_widget_form()?
/*if (variables.field_info_instance) {
var module = variables.field_info_instance.widget.module;
var field_widget_form_function = module + '_field_widget_form';
if (drupalgap_function_exists(field_widget_form_function)) {
using_field_widget_form = true;
// Grab the field widget implementor function, then call it.
var fn = window[field_widget_form_function];
// form, form_state, field, instance, langcode, items, delta, element
html += fn.call(form, null, variables.field_info_field, variables.field_info_instance, drupalgap.settings.language, form.elements[name], 0, element);
}
}*/
break;
}

Expand Down
30 changes: 30 additions & 0 deletions modules/field/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,33 @@ function text_field_formatter_view(entity_type, entity, field, instance, langcod
catch (error) { drupalgap_error(error); }
}

/**
* Implements hook_field_widget_form().
*/
function text_field_widget_form(form, form_state, field, instance, langcode, items, delta, element) {
try {
dpm('text_field_widget_form');
//dpm(form);
dpm(form.elements[element.name][langcode][delta]);
//dpm(arguments);
// Determine the widget type, then set the delta item's type property.
var type = null;
switch (element.type) {
case "text":
type = 'textfield';
break;
case 'textarea':
case 'text_long':
case "text_with_summary":
case 'text_textarea':
type = 'textarea';
// Add value to variables and remove the value attribute.
//variables.value = element.default_value;
//delete variables.attributes.value;
}
form.elements[element.name][langcode][delta].type = type;
dpm(form.elements[element.name][langcode][delta]);
}
catch (error) { drupalgap_error(error); }
}

0 comments on commit 4985e94

Please sign in to comment.