DrupalGap icon indicating copy to clipboard operation
DrupalGap copied to clipboard

Setting Entity Reference Fields

Open mkinnan opened this issue 9 years ago • 4 comments

The only way I can set an entity reference field is doig

form.elements['group_ref']['und']['0'].default_value = _GET('group_nid');

However, by doing default_value it sets an actual default value so all future nodes that have no reference get a value of that default_value previously set. I've tried doing value but that doesn't work.

I don't think default_value is the right thing to do, but it is the only thing that works.

Thoughts?

https://github.com/signalpoint/DrupalGap/issues/704 https://github.com/signalpoint/DrupalGap/issues/709

mkinnan avatar Nov 13 '16 22:11 mkinnan

To clarify, the default value that gets set is for the user while in the app or in emulator. It does not appear to be set globally.

I can see the default value has been set when the node create form is viewed in console.log.

mkinnan avatar Nov 14 '16 15:11 mkinnan

Here's a snippet from my crude work around that has passed my emulator testing ...

if (form.id == 'node_edit' && form.bundle == 'discussion') {

  if (_GET('dt') == 1) {
    console.log('-- discussion type: group --');
    form.elements['group_ref']['und']['0'].default_value = _GET('gnid');
    form.elements['field_event_ref']['und']['0'].default_value = '';
  }
  if (_GET('dt') == 2) {
    console.log('-- discussion type: event --');
    form.elements['group_ref']['und']['0'].default_value = _GET('gnid');
    form.elements['field_event_ref']['und']['0'].default_value = _GET('enid');
  }

mkinnan avatar Nov 15 '16 03:11 mkinnan

@mkinnan This seems like an OK approach. When you set the default_value, if you were to check if it is a new node, then you could skip setting the value if that's what you need. Please clarify, thanks.

signalpoint avatar Nov 23 '16 02:11 signalpoint

@signalpoint The default_value isn't really a default value since that value changes. The problem is that if a user creates a discussion on an event in a group, the discussion gets referenced by two entity fields (one for the group, and one for the event). If the user then creates a discussion in a group, the discussion should only be referenced by the group entity field ... but the entity reference field for events has the default value set when the user created that discussion on an event. Thus the group discussion gets erroneously referenced to an event.

Does that clarify what is happening with default value?

mkinnan avatar Nov 23 '16 02:11 mkinnan