Skip to content

Commit

Permalink
#160. Brought back in the features for taxonomy term reference field.…
Browse files Browse the repository at this point in the history
… Added a children array property that can be set on form element items to place additional render objects or markup with them.
  • Loading branch information
signalpoint committed Oct 26, 2013
1 parent 6adebe8 commit 2b8a8eb
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 31 deletions.
13 changes: 6 additions & 7 deletions drupalgap.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ var drupalgap = {
'router_path':'', /* The current menu router path. */
'services':{},
'sessid':null,
taxonomy_vocabularies:false, /* holds the services index of vocabularies */
'theme_path':'',
'themes':[],
'theme_registry':{},
Expand Down Expand Up @@ -1273,14 +1274,9 @@ function drupalgap_onload() {
function user_access(permission) {
try {
// Make sure they provided a permission.
if (permission == null) {
alert("user_access - permission not provided");
return false;
}
if (permission == null) { return false; }
// User 1 always has permission.
if (drupalgap.user.uid == 1) {
return true;
}
if (drupalgap.user.uid == 1) { return true; }
// For everyone else, assume they don't have permission. Iterate over
// drupalgap.user.permissions to see if the current user has the given
// permission, then return the result.
Expand Down Expand Up @@ -1356,6 +1352,9 @@ function dpm(data) {
console.log(JSON.stringify(data));
}
}
else {
console.log('<null>');
}
}
catch (error) {
alert('dpm - ' + error);
Expand Down
27 changes: 22 additions & 5 deletions includes/form.inc.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ function _drupalgap_form_render_elements(form) {
// set it, then render the element if access is permitted.
$.each(form.elements, function(name, element){
if (!element.name) { element.name = name; }
dpm(name);
if (drupalgap_form_element_access(element)) {
content += _drupalgap_form_render_element(form, element);
}
Expand Down Expand Up @@ -496,6 +497,7 @@ function _drupalgap_form_render_element(form, element) {
}
if (module) {
field_widget_form_function_name = module + '_field_widget_form';

if (drupalgap_function_exists(field_widget_form_function_name)) {
field_widget_form_function = window[field_widget_form_function_name];
}
Expand Down Expand Up @@ -533,6 +535,10 @@ function _drupalgap_form_render_element(form, element) {
// Attach the item as the element onto variables.
variables.element = item;

// Create an array for the item's children if it doesn't exist already.
// This is used by field widget forms to extend form elements.
if (!items[delta].children) { items[delta].children = []; }

// Add a label to the element, except submit and hidden elements.
if (delta == 0 && element.type != 'submit' && element.type != 'hidden') {
var theme_variables = null;
Expand Down Expand Up @@ -603,10 +609,6 @@ function _drupalgap_form_render_element(form, element) {
function _drupalgap_form_render_element_item(form, element, variables, item) {
try {
var html = '';

// Depending on the element type, if necessary, adjust the variables and/or
// theme function to be used, then render the element by calling its theme
// function.
var theme_function = item.type;

// Make any preprocess modifications to the elements so they will map
Expand All @@ -631,7 +633,8 @@ function _drupalgap_form_render_element_item(form, element, variables, item) {
}
}

// If the item isn't an outlier, run it through the theme system.
// Run the item through the theme system if a theme function exists, or try
// to use the item marku, or let the user know the field isn't supported.
if (drupalgap_function_exists('theme_' + theme_function)) {
html += theme(theme_function, variables);
}
Expand All @@ -644,6 +647,20 @@ function _drupalgap_form_render_element_item(form, element, variables, item) {
}
}

// Render any item children. If the child has markup, just it it, otherwise
// run the child through theme().
if (item.children && item.children.length > 0) {
for (var i = 0; i < item.children.length; i++) {
if (item.children[i].markup) { html += item.children[i].markup; }
else if (item.children[i].type) {
html += theme(item.children[i].type, item.children[i]);
}
else {
console.log('WARNING: _drupalgap_form_render_element_item() - failed to render child ' + i + ' for ' + element.name);
}
}
}

return html;
}
catch (error) { drupalgap_error(error); }
Expand Down
2 changes: 2 additions & 0 deletions modules/entity/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ function drupalgap_entity_render_content(entity_type, entity) {
*/
function drupalgap_entity_render_field(entity_type, entity, field_name, field, display) {
try {
dpm(field_name);
dpm(display);
var content = '';
// Determine module that implements the hook_field_formatter_view,
// then determine the hook's function name, then render the field content.
Expand Down
44 changes: 41 additions & 3 deletions modules/field/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,22 +155,60 @@ function number_field_formatter_view(entity_type, entity, field, instance, langc
*/
function options_field_widget_form(form, form_state, field, instance, langcode, items, delta, element) {
try {
//dpm(items, false);
switch (element.type) {
case 'checkbox':
// If the checkbox has a default value of 1, check the box.
if (items[delta].default_value == 1) { items[delta].checked = true; }
break;
case "radios":
case 'radios':
break;
case "select":
case 'select':
// If the select list is required, add a 'Select' option and set it as
// the default.
if (items[delta].required) {
items[delta].options[-1] = 'Select';
items[delta].value = -1;
}
break;
case 'taxonomy_term_reference':
// Change the item type to a hidden input.
items[delta].type = 'hidden';
// What vocabulary are we using?
var machine_name = field.settings.allowed_values[0].vocabulary;
var taxonomy_vocabulary = taxonomy_vocabulary_machine_name_load(machine_name);

var widget_type = false;
if (instance.widget.type == 'options_select') { widget_type = 'select'; }
else {
console.log('WARNING: options_field_widget_form() - ' + instance.widget.type + ' not yet supported for ' + element.type + ' form elements!');
return false;
}
var widget_id = items[delta].id + '-' + widget_type;
items[delta].children.push({
type:widget_type,
attributes:{
id:widget_id,
onchange:"_theme_taxonomy_term_reference_onchange(this, '" + items[delta].id + "');"
}
});
var options = {
'taxonomy_vocabulary':taxonomy_vocabulary,
'widget_id':widget_id
};
items[delta].children.push({markup:"_theme_taxonomy_term_reference_load_items(" + JSON.stringify(options) + ")"});
// Attach a pageshow handler to the current page that will load the terms
// into the widget.
/*var options = {
'page_id':drupalgap_get_page_id(drupalgap_path_get()),
'jqm_page_event':'pageshow',
'jqm_page_event_callback':'_theme_taxonomy_term_reference_load_items',
'jqm_page_event_args':JSON.stringify({
'taxonomy_vocabulary':taxonomy_vocabulary,
'widget_id':widget_id
})
};
html += drupalgap_jqm_page_event_script_code(options);*/
break;
}
}
catch (error) { drupalgap_error(error); }
Expand Down
1 change: 1 addition & 0 deletions modules/services/drupalgap_system.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ drupalgap.services.drupalgap_system = {
drupalgap.entity_info = data.entity_info;
drupalgap.field_info_instances = data.field_info_instances;
drupalgap.field_info_fields = data.field_info_fields;
drupalgap.taxonomy_vocabularies = drupalgap_taxonomy_vocabularies_extract(data.taxonomy_vocabularies);
drupalgap_service_resource_extract_results({
'service':'drupalgap_system',
'resource':'connect',
Expand Down
24 changes: 24 additions & 0 deletions modules/services/taxonomy_vocabulary.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,30 @@ drupalgap.services.taxonomy_vocabulary = {
}
},
}, // <!-- delete -->
'index':{
'options':{
'type':'get',
'path':'taxonomy_vocabulary.json',
'success':function(results){
dpm(results);
alert('index success');
},
},
'call':function(options){
try {
var api_options = drupalgap_chain_callbacks(drupalgap.services.taxonomy_vocabulary.index.options, options);
drupalgap.api.call(api_options);
}
catch (error) {
navigator.notification.alert(
error,
function(){},
'taxonomy_vocabulary Index Error',
'OK'
);
}
},
}, // <!-- index -->
'getTree':{
'options':{
'type':'post',
Expand Down
126 changes: 110 additions & 16 deletions modules/taxonomy/taxonomy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/**
*
*/
function drupalgap_taxonomy_vocabularies_extract(taxonomy_vocabularies) {
try {
var results = false;
if (taxonomy_vocabularies.length > 0) {
results = {};
$.each(taxonomy_vocabularies, function(index, vocabulary){
eval('results.' + vocabulary.machine_name + ' = vocabulary;');
});
}
return results;
}
catch (error) { drupalgap_error(error); }
}

/**
* Implements hook_field_formatter_view().
*/
Expand Down Expand Up @@ -75,22 +92,7 @@ function taxonomy_menu() {
'weight':0,
'type':'MENU_LOCAL_TASK',
'access_arguments':['administer taxonomy'],
},
/*'admin/structure/taxonomy':{
'title':'Taxonomy',
'page_callback':'drupalgap_get_form',
'page_arguments':['taxonomy_overview_vocabularies'],
'access_arguments':['administer taxonomy'],
},
'admin/structure/taxonomy/list':{
'title':'List',
'type':'MENU_DEFAULT_LOCAL_TASK',
'weight':-10,
},
'admin/structure/taxonomy/add':{
'title':'Add vocabulary',
'page_callback':'taxonomy_form_vocabulary',
},*/
}
};
return items;
}
Expand Down Expand Up @@ -347,6 +349,98 @@ function taxonomy_vocabulary_pageshow(vid) {
catch (error) { drupalgap_error(error); }
}

/**
*
*/
function taxonomy_vocabulary_machine_name_load(name) {
try {
if (drupalgap.taxonomy_vocabularies && drupalgap.taxonomy_vocabularies[name]) {
return drupalgap.taxonomy_vocabularies[name];
}
return false;
}
catch (error) { drupalgap_error(error); }
}

/**
*
*/
function theme_taxonomy_term_reference(variables) {
try {
var html = '';

// Make this a hidden field since the widget will just populate a value.
variables.attributes.type = 'hidden';
html += '<input ' + drupalgap_attributes(variables.attributes) + '/>';

// What vocabulary are we using?
var machine_name = variables.field_info_field.settings.allowed_values[0].vocabulary;
var taxonomy_vocabulary = taxonomy_vocabulary_machine_name_load(machine_name);

// Prepare the variables for the widget and render it based on its type.
var widget_type = variables.field_info_instance.widget.type;
if (widget_type == 'options_select') { widget_type = 'select'; }
var widget_function = 'theme_' + widget_type;
var widget_id = variables.attributes.id + '-' + widget_type;
if (drupalgap_function_exists(widget_function)) {
var fn = window[widget_function];
html += fn.call(null, {
attributes:{
id:widget_id,
onchange:"_theme_taxonomy_term_reference_onchange(this, '" + variables.attributes.id + "');"
}
});
// Attach a pageshow handler to the current page that will load the terms
// into the widget.
var options = {
'page_id':drupalgap_get_page_id(drupalgap_path_get()),
'jqm_page_event':'pageshow',
'jqm_page_event_callback':'_theme_taxonomy_term_reference_load_items',
'jqm_page_event_args':JSON.stringify({
'taxonomy_vocabulary':taxonomy_vocabulary,
'widget_id':widget_id
})
};
html += drupalgap_jqm_page_event_script_code(options);
}
else {
console.log('WARNING: theme_taxonomy_term_reference() - unsupported widget type! (' + widget_type + ')');
}
return html;
}
catch (error) { drupalgap_error(error); }
}

/**
*
*/
function _theme_taxonomy_term_reference_load_items(options) {
try {
drupalgap.services.drupalgap_taxonomy.get_terms.call({
vid:options.taxonomy_vocabulary.vid,
success:function(terms){
if (terms.length > 0) {
$.each(terms, function(index, term){
var option = '<option value="' + term.tid + '">' + term.name + '</option>';
$('#' + options.widget_id).append(option);
});
}
}
});
}
catch (error) { drupalgap_error(error); }
}

/**
*
*/
function _theme_taxonomy_term_reference_onchange(input, id) {
try {
$('#' + id).val($(input).val());
}
catch (error) { drupalgap_error(error); }
}

/**
* Return the vocabulary object matching a vocabulary ID, otherwise it returns
* false.
Expand Down

0 comments on commit 2b8a8eb

Please sign in to comment.