2

We are using DrupalGap to make an app. We have nodes (content type chargingstations) which we want our users to be able to "check" into by pressing a button. Until now I have created a button on the nodes page in DrupalGap which executes a function on an onClick event.

This function takes in a NID which is the node I want the user to check into. I then have a field on the user called "checked_in_chargingstation" which is an entity reference field.

In the DrupalGap documentation there is not much information on user_update. It just says "...". But it seems like you can update a node by passing the NID. So I figured I could do the same for user? But it doesen't seem to work. I get a success alert, but I can't see any changes to the users.

My check in code. Drupal.user.uid gets the current users uid.

function check_in($nid){
  var $field_checked_in_chargingstation = {};
  $field_checked_in_chargingstation["und"] = {
    target_id: $nid,
  };

  var user = {
    uid: Drupal.user.uid,
    field_checked_in_chargingstation: $field_checked_in_chargingstation,
  }

  user_save(user, {
    success:function(result){
      console.log(result);
    }
  });
}

In the console I get:

Console.log()

Looking at the json I can see:

"field_checked_in_chargingstation": {
 "und": [
  {
   "target_id": "29088"
  }
 ]
},

But this is the wrong id. This is an old node which I have assigned before from Drupal and "edit" user. So it doesen't seem updated. Trying to drush cc all doesen't seem to work either.

If I try to edit the user from the app I only see null values for the entity reference field. One suggestion in the Drupalgap documentation is to make an entity reference view http://docs.drupalgap.org/7/Entities/Entity_Reference which I also did.

Sorry if this is a bit long. And I'm not sure if I should post it here on Stack Exchange or as an issue on DrupalGap.

2

It depends largely on what Widget you have selected for your entity reference field on your Manage Display page. The easiest widget to work with is the Autocomplete widget, at which point you'd send up this JSON:

"field_checked_in_chargingstation": {
 "und": [
  {
   "target_id": "... (29088)"
  }
 ]
},

The Services module is coupled with the forms system in Drupal, so as you change the widget on a field, the JSON data you need to send to Drupal may change.

  • On my manage fields i have chosen Autocomplete. In the manage display i can choose format and I have chosen "Entity id". – Ronjark Dec 7 '16 at 14:33
  • Since I was only sending the NID i changed my own entity refernece view to only contain the nid. Tried this, but it didn't seem work. Nothing is changed. Now I tried to send "nodetitle (nid)" by loading the nodes title. And change back to the default "entity selection". Still not working. And im a bit concerned that I cant edit or see the field when i try to edit the user in drupalgap. It just says "null". – Ronjark Dec 7 '16 at 15:02
  • I also get this error "WARNING: drupalgap_entity_render_field - entityreference_field_formatter_view() does not exist! (field_user_queue)" – Ronjark Dec 7 '16 at 15:07
  • Have you installed the entityreference.js module in DrupalGap? drupalgap.org/project/entityreference - that should fix your latter problems. You only need to send the ellipsis and the target id, you don't actually have to send the node title "... (29088)" – tyler.frankenstein Dec 7 '16 at 15:49
  • No, I had not heard of the entityreference module! Now I can finally change the field from user/edit. But regarding updating the node from a button click: Now i changed to elipsis, but it still won't save. In the console I get a success with the node as it should be saved. field_checked_in_chargingstation: Object: und Object: target_id: "... (26575)" But checking my users field from either drupalgap or drupal stills shows the old node. And if i go to the put url it shows the old node. "PUT: http://ifi-diw00.uio.no/?q=drupalgap/user/19.json" – Ronjark Dec 8 '16 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.