I have an issue.
a site that is long time running, already have various customisations to the node/add on homepage and an iOS app that requires me to make Drupalgap fit in (I can't alter the way the type/fields are set up.

we are using Gmaps and the locations picker to get locations from the users.
So I used form alter to change the inputs, I would like to archive that the user adds image, description and a category ( taxonomy) all is great and swell, except I want the user locations to be stored in the map location picker as it is required.

the alter form have these customisations:

form.elements.field_sted_location.und.locpick.user_latitude = {
			type: 'hidden',
			value:'_byhoest_map_user_latitude'
		};
		form.elements.field_sted_location.und.locpick.user_longitude = {
			type: 'hidden',
			value:'_byhoest_map_user_longitude'
		};

but when I try to post a new subject I get the error (excuse my localisation):

[Error] Failed to load resource: the server responded with a status of 406 (Not Acceptable) (www.byhoest.dk, line 0)
[Log] {"form_errors":{"field_sted_location][und][0][locpick][user_latitude":"Feltet Breddegrad er obligatorisk.","field_sted_location][und][0][locpick][user_longitude":"Feltet L\u00e6ngdegrad er obligatorisk."}} (jdrupal-7.x-1.1.min.js, line 31)

so this is not the way to inject a variable into the form I suppose (?)

a working node posts this as JSON:

{
    "vid": "4206",
    "uid": "2682",
    "title": "L\u00f8gkarse",
    ...
    "field_sted_location": {
        "und": [
            {
                "lid": "1265",
                "name": "",
                "street": "",
                "additional": "",
                "city": "",
                "province": "",
                "postal_code": "",
                "country": "dk",
                "latitude": "55.618411",
                "longitude": "12.579332",
                "source": "1",
                "is_primary": "0",
                "locpick": {
                    "user_latitude": "55.618411",
                    "user_longitude": "12.579332"
                },
                "province_name": "",
                "country_name": "Danmark"
            }
        ]
    },
...

how do I get the value in properly into the locpick attribute ?

Comments

dan1eln1el5en’s picture

Issue summary: View changes
dan1eln1el5en’s picture

Issue summary: View changes
tyler.frankenstein’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: -gmap alter form

Please share the links to the project pages for the Drupal contrib modules you are using to make this happen on your Drupal site. Then we'll see if the corresponding modules exist in DrupalGap.

dan1eln1el5en’s picture

oh, well Let me have a look.

meanwhile you can see the page here: http://www.byhoest.dk/beta/#node_add_sted

i think it's basically Gmaps and Locations that are being used one for maps the other for locations (and the picker), after reading around I found that I need to upgrade the fields to use geofields in the future, but now I just need this little field to populate to have a working version 1

gmaps: https://www.drupal.org/project/gmap
Location: https://www.drupal.org/project/location

tyler.frankenstein’s picture

Title: Gmap lockpick support » Location and Gmap module locpick support
Version: 7.x-1.11 » 7.x-1.x-dev
Category: Support request » Feature request
Status: Postponed (maintainer needs more info) » Needs work

The location module for DrupalGap will need its hook_field_widget_form() and hook_assemble_form_state_into_field() implemented. That will allow the widget to be displayed correctly on the node add/edit form, and then when it is submitted, it can properly assemble the JSON to POST to the server.

https://github.com/signalpoint/location
http://api.drupalgap.org/

dan1eln1el5en’s picture

ok, I have Locations module installed in the drupal project, so shall I read your reply as there is something in it that needs updating or that I need to rewrite the code to make it work ? (if so could you give me a good link how to implement this ?)

tyler.frankenstein’s picture

You need to install the Location module for DrupalGap, then implement the two hooks I mentioned previously in the Location module for DrupalGap. Use the DrupalGap API for reference, or other DrupalGap contrib modules that have implemented those hooks, are also good for reference.

dan1eln1el5en’s picture

just wanted to get back to you, I took _strong_ inspiration from geofields.js, here is the new location module in my app :)
(Note the localisation...)

/**
 * Implements hook_field_formatter_view().
 */
function location_cck_field_formatter_view(entity_type, entity, field, instance, langcode, items, display) {
  try {
    var element = {};
    $.each(items, function(delta, item) {
        element[delta] = {
          markup: theme('location', item)
        };
    });
    return element;
  }
  catch (error) { console.log('location_cck_field_formatter_view - ' + error); }
} 

/**
 * Theme a location.
 */
function theme_location(variables) {
  try {
    var html = '';
    console.log(variables);
    if (variables.street) { html += variables.street + '<br />'; }
    if (variables.additional) { html += variables.additional + '<br />'; }
    if (variables.city) { html += variables.city + ''; }
    if (variables.province) {
      if (variables.city) { html += ', '; }
      html += variables.province + ' ';
    }
    if (variables.postal_code) { html += variables.postal_code + '<br />'; }
    else { html += '<br />'; }
    if (variables.country) { html += variables.country + '<br />'; }
    
    //if (variables.locpick) { html += variables.lockpick.user_latitude + '<br /> + ' variables.lockpick.user_longitude + '<br />'; }
    return html;
  }
  catch (error) { console.log('theme_location - ' + error); }
}

/**
 * Implements hook_field_widget_form().
 */
function location_cck_field_widget_form(form, form_state, field, instance, langcode, items, delta, element) {
  try {
    // Convert the form element to a hidden field since we'll populate it with
    // values dynamically later on.
    items[delta].type = 'hidden';
    
    // For a latitude/longitude widget, we create two text fields and a button
    // to get the current position and fill in the two text fields.
    if (instance.widget.type == 'location') {
      var onchange = '_location_cck_field_widget_form_change(this, \'' + items[delta].id + '\')';
      var lat_id = items[delta].id + '-lat';
      var lat = {
        id: lat_id,
        title: 'Længdegrad',
        type: 'textfield',
        options: {
          attributes: {
            id: lat_id,
            onchange: onchange
          }
        }
      };
      var lon_id = items[delta].id + '-lon';
      var lon = {
        id: lon_id,
        title: 'Breddegrad',
        type: 'textfield',
        options: {
          attributes: {
            id: lon_id,
            onchange: onchange
          }
        }
      };
      var options = {
        lat: lat.id,
        lon: lon.id
      };
      var btn = {
        id: items[delta].id + '-btn',
        text: 'Stedet er her !',
        type: 'button',
        options: {
          attributes: {
            onclick: '_location_cck_field_widget_form_click(\'' + lat.id + '\', \'' + lon.id + '\')'
          }
        }
      };
      items[delta].children.push(lat);
      items[delta].children.push(lon);
      items[delta].children.push(btn);
    }
    else {
      console.log('WARNING: location_cck_field_widget_form() - widget type not supported! (' + instance.widget.type + ')');
    }
  }
  catch (error) { console.log('location_cck_field_widget_form - ' + error); }
}

/**
 *
 */
function _location_cck_field_widget_form_change(textfield, input) {
  try {
    // Depending on which textfield just changed values, grab the other one as
    // well, then build the lat,lon value for the hidden input.
    var which = $(textfield).attr('id');
    var other = which;
    if (which.indexOf('-lat') != -1) { other = which.replace('-lat', '-lon'); }
    else {
      other = which.replace('-lon', '-lat');
      var swap = other;
      other = which;
      which = swap;
    }
    var value = $('#' + which).val() + ',' + $('#' + other).val();
    $('#' + input).val(value);
  }
  catch (error) { console.log('_location_cck_field_widget_form_change - ' + error); }
}


/**
 *
 */
function _location_cck_field_widget_form_click(lat_id, lon_id) {
  try {
    navigator.geolocation.getCurrentPosition(
      function(position) {
        // Place the coordinate values into the text fields, then force a change
        // event to fire.
        $('#' + lat_id).val(position.coords.latitude);
        $('#' + lon_id).val(position.coords.longitude).change();
      },
      function(error) {
        console.log('_location_cck_field_widget_form_click - getCurrentPosition - ' + error);
      },
      {
        enableHighAccuracy: true
      }
    );
  }
  catch (error) { console.log('_location_cck_field_widget_form_click - ' + error); }
}

/**
 * Implements hook_assemble_form_state_into_field().
 */
function location_cck_assemble_form_state_into_field(entity_type, bundle,
  form_state_value, field, instance, langcode, delta, field_key) {
  try {
    if (empty(form_state_value)) { return null; }
    var coordinates = form_state_value.split(',');
    if (coordinates.length != 2) { return null; }
    // We don't want to use a key for this item's value.
    field_key.value = 'locpick';
    // Return the assembled value.
    return {
      user_latitude: coordinates[0],
      user_longitude: coordinates[1]
    };
  }
  catch (error) {
    console.log('location_cck_assemble_form_state_into_field - ' + error);
  }
}
tyler.frankenstein’s picture

@dan1eln1el5en, that looks amazing, nice work! Would you please fork the location module on GitHub, then commit and submit a pull request for your changes? That way the code contributions can be easily attributed to you. - https://github.com/signalpoint/location - or if you'd like to be an administrator of that project on github, let me know, thanks!

dan1eln1el5en’s picture

hey Tyler,

would love to, but right now I need this app out of the door first, I starting to get the logic of the structures here.
I'm just going to post another question/issue, because I think I might have overlooked something...that would be fairly last thing to ensure I can send out a new beta tonight (CET time zone here)