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'];
2
  • 1
    is this drupal 7 or 8?
    – kombogdan
    Commented Aug 2, 2018 at 21:21
  • @kombogdan The placement of Field Module in drupal-root/modules says it is drupal 7 not 8 or above Commented Apr 18, 2021 at 8:05

2 Answers 2

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.

13
  • 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).
    – avpaderno
    Commented Aug 3, 2018 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
    Commented Aug 3, 2018 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'].
    – avpaderno
    Commented Aug 3, 2018 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.
    – avpaderno
    Commented Aug 3, 2018 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.
    – avpaderno
    Commented Aug 3, 2018 at 21:07
0

I guess you are using Entity metadata wrapper solution to set values of an entity (in this case node) , if so you have to use set method like this :

$location->field_locationmap->set([$form_state['values']['field_locationmap']['lon'], $form_state['values']['field_locationmap']['lat']]);

if not using entity metadata wrapper you might using some code like below:

$location = new stdClass();
$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'];
$location->save();

if so as drupal expects fields value to be an array, it throws exception when facing object, so you need to modify your code like this:

$location = new stdClass();
$location->field_locationmap['und'][0]['geom'] = [
  'lon' => $form_state['values']['field_locationmap']['lon'],
  'lat' => $form_state['values']['field_locationmap']['lat']
];
$location->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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