0

I built a custom form and one of them is of type geocode with lat/lon fields. When I try to save the node I receive the following error:

Cannot use object of type stdClass as array in /var/www/vhosts/example.com/httpdocs/modules/field/field.attach.inc on line 208\n'.

The error disappears when I put comments in this field. My question is how can I solve this since it is very important field (I try to create nodes with geocode locations)

Here is how I declare my field:

$form['field_locationmap'] = array(
    '#title' => 'geofield',
    '#type' => 'geofield_latlon',
  );

It seems that the error occurs when I m trying to pass the values to the object:

$location->field_locationmap->und->{0}->geom->lon=$form_state['values']['field_locationmap']['lon'];
$location->field_locationmap->und->{0}->geom->lat=$form_state['values']['field_locationmap']['lat'];
  • 1
    is this drupal 7 or 8? – kombogdan Aug 2 '18 at 21:21
0

Assuming this is Drupal 7 field_locationmap is an array so your assignment should look something like this:

$location->field_locationmap['und'][0]['geom']['lat']

Also make sure that['geom']['lat'] is the correct structure for that value (i.e it is an array with two keys) P.S. Consider using $location->language instead of 'und' in case you support multiple languages.

  • Actually, it's the opposite. The error is Cannot use object of type stdClass as array, which means the code is using an object as array. It's not even the code shown in the question, to cause the error, but Drupal core code (modules/field/field.attach.inc). – kiamlaluno Aug 3 '18 at 13:24
  • You might be misunderstanding the error. if you first create a stdClass like so: $obj->prop and then some other place you will try to do $obj['prop'] you will get that error. The error happens while trying to store the node to database. Thus the error within Drupal core. Look into entities and fielding process within Drupal 7 for better understanding of the process. – kombogdan Aug 3 '18 at 16:56
  • But the OP is showing a line like $location->field_locationmap->und->{0}->geom->lon, which cannot be causing the cannot use object of type stdClass as array error, since the code would eventually use an array as object, and you are suggesting to use $location->field_locationmap['und'][0]['geom']['lat']. – kiamlaluno Aug 3 '18 at 19:50
  • The error is clear enough: The code causing it is using as array an object. That is not the case of the code shown from the OP, which is using the operator to access object properties. – kiamlaluno Aug 3 '18 at 19:52
  • 1
    We don't remove answers just because they are wrong answers. You didn't even write a comment as answer, so there isn't any reason to remove it. – kiamlaluno Aug 3 '18 at 21:07

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.