I am trying to fill in lat/lng data in the Simple Map field of an entry through a plugin. All information from entry is saved except for Simple Map. Apparently I am doing something wrong there.

Here is my code:

$entry = new Entry(); // use craft\elements\Entry;
$entry->.... // set fields
    'otherFields' => $value,
    'mySimpleMapFieldName' => [
       'lat' => 52.27, //$location['lat'],
       'lng' => 0.51 //$location['lng'],

I think what you are running into is the getField() method on #147 of the MapService. What this does is return a model based on whether this is an existing record or a post request and if neither return an empty model.

if ( 
        // Is this a Post request and isnt there a console request AND is there a value
        && \Craft::$app->request->isPost
        && $value
    ) {

        // Create a new map model with the $value param - creating a model that takes the $value into account
        $model = new Map($value);
    } else if ($record) {

        // Is there a record? Create a model that takes these record params into account
        $model = new Map($record->getAttributes());
    } else {

        // We havent got a post request.or a record. This is what is happening to your input. 
        $model = new Map();
If you Edit $model = new Map(); TO $model = new Map($value); Your above code works fine. 

I am not sure as to the inner workings of Simple Map - have only used the C2 version. The best way to fix this for the long term is probably to create an issue on SimpleMap's github page. My recommendation is to replace the above else{} method, with what i described below. Maybe there is some way to get around this that doesnt involve source edits - like i said i dont know Simple map inside out - i might just be missing something obvious. In case there isn't i would recommend fixing the MapService with something like the below:

else {

    Didnt meet the satisfied conditions. Is $value an array and does it exist? 
    if(!$value || !is_array($value)){

        // Not happy with this input - create a blank map. 
        $model = new Map();

        We're happy with the sended value - it isnt a post request. Lets take the value into account
        $model = new Map($value);

I ran the above code with your example and it ran just fine.

I think it goes without saying that it is a terrible idea to just edit just your MapService file with the above code as a permanent solution as that will give you instability/incompatibility problems. What i would recommend is editing the method temporarily to test if this solves your problem - just to make sure. If it does it's probably wise to open an issue. Either if it does or doesn't - probably smart to open a github issue to see what the devs themselves say.

P.S - your code example is missing one array brackets at the end - probably smart to edit that before pasting it back.

Hope i was able to help in someway :)

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.