Skip to content

Commit

Permalink
Fix #28. Fix #239. Change drupalgap_entity_render_field() to fall bac…
Browse files Browse the repository at this point in the history
…k to the 'und' language code if the entity's language code doesn't exist on the field(s).
  • Loading branch information
signalpoint committed Apr 27, 2014
1 parent c4a7f6e commit 2306255
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/modules/entity/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,18 +222,27 @@ function drupalgap_entity_render_field(entity_type, entity, field_name,
var function_name = module + '_field_formatter_view';
if (drupalgap_function_exists(function_name)) {
// Grab the field formatter function, then grab the field items
// from the entity, then call the formatter function it and append
// its result to the entity's content.
// from the entity, then call the formatter function and append its result
// to the entity's content.
var fn = window[function_name];
var items = null;
// Determine the language code. Note, multi lingual sites may have a
// language code on the entity, but still have 'und' on the field, so
// fall back to 'und' if the field's language code doesn't match the
// entity's language code.
var language = entity.language;
if (entity[field_name]) {
if (entity[field_name][entity.language]) {
items = entity[field_name][entity.language];
if (entity[field_name][language]) {
items = entity[field_name][language];
}
else if (entity[field_name]['und']) {
items = entity[field_name]['und'];
language = 'und';
}
else { items = entity[field_name]; }
}
var elements = fn(
entity_type, entity, field, null, entity.language, items, display
entity_type, entity, field, null, language, items, display
);
$.each(elements, function(delta, element) {
// If the element has markup, render it as is, if it is
Expand Down

15 comments on commit 2306255

@pierpa68
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @signalpoint,

I'm sorry to reopen this, but I think the fix above should be applied also to the drupalgap_entity_add_core_fields_to_form function, otherwise all values in the form (when the field language is "und" while the entity one is something else) show empty.
Will try myself meanwhile.

HTH

@signalpoint
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pierpa68 Yes please try adding these changes to drupalgap_entity_add_core_fields_to_form on your local copy, and let me know if that works. I have a hard time testing/debugging multi-lingual sites, because my clients never need them :(

@pierpa68
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@signalpoint I've fond an older version of the field.js module that seems to already include the fix above... Unfortunately I'm unable to find the commit number in github. Here it is a snippet. I can't understand why it was changed

 if (entity && entity[name] && entity[name].length != 0) {
              if (entity[name]) {
                if (entity[name][language]) {
                  items = entity[name][language];
                }
                else if (entity.language && entity[name][entity.language]) {
                  items = entity[name][entity.language];
                }
                else if (entity[name]['und']) {
                  items = entity[name]['und'];
                }
                else { items = entity[name]; }
              }

Current version in github:

if (entity && entity[name] && entity[name].length != 0 && entity[name][language]) {

The older version seems to work atm.

HTH

@signalpoint
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pierpa68 Thank you for clarifying, I believe I just resolved this via #503.

@pierpa68
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@signalpoint Thanks for comment.

Your commit almost solved the problem: I had to move line n.130

if (!form.elements[name][language]) { form.elements[name][language] = {};

after you set the language to 'und' at line `143-146 of field.js, otherwise at the line

form.elements[name][language][delta] = { value: default_value };

returns an error.

By the way, trying to merge my work with your recent commits, I was unable to run my application due to an error in drupalgap_init() : it seems that Drupal.cache_expiration is undefined and I can't find where it should be declared.

@signalpoint
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pierpa68 Grab the latest jdrupal.min.js for the fix to the missing Drupal.cache_expiration.

@signalpoint
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pierpa68 I moved the creation of the element after the determination of the language code as you suggested, thanks!

@pierpa68
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@signalpoint I found that a similar approach has to be applied in other procedures as well in order to make an entity editable without errors in case of different language.
I'll send you a pull shortly, of you like.

@signalpoint
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pierpa68 That would be great, thank you.

@pierpa68
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@signalpoint Already done. I'm sorry but I messed the pull a bit sending patches for 2 different issues (#751).
One is just the updated locale file for italian language.

@pierpa68
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@signalpoint I'm still having issues with language, this time adding new node.

Maybe a ultimate solution would be to use the translatable element of the field_info_field structure as returned by the function drupalgap_field_info_field(name).

Documentation here seems to suggest so.

If you think it could be correct I can try to make some of the necessary modification to the code.

@jomarocas
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi i have problems with this code,i update latest version and still no working, i change with two languages, and with und working body field, when i editing the node in drupalgap,int the body field no appear the editing text, but in the site yes, appear text, thanks with any update,thanks @pierpa68 @signalpoint

@jomarocas
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

send code with the array

[ { vid: '26', uid: '1', title: 'GczHhh', log: '', status: '1', comment: '1', promote: '0', sticky: '0', vuuid: '49c237f8-1a42-402f-a651-ff803fd152d8', nid: '26', type: 'notas', language: 'es', created: '1459811105', changed: '1459875470', tnid: '0', translate: '0', uuid: '1c13e0ca-e259-4501-aa17-a2332bb71a78', revision_timestamp: '1459875470', revision_uid: '1', body: [Object],

@pierpa68
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jomarocas you're right, there are already some issues in the actual version. But I've gone further and the code I'm testing here seems to work correctly now.
I hope to make a commit shortly, if @signalpoint agrees

@signalpoint
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pierpa68 I'm definitely open for pull requests that fix these multi lingual issues, thank you!

Please sign in to comment.