2

I have read in a lot of sources that I should use the geofield_compute_values() function when trying to programmatically save coordinates in Drupal.

However it does not work for me, that function is undefined in the Drupal 8.5.2 that I am using.

I've installed geofield using composer and I can use it as usual in the admin area and there are no problems with saving there.

Here are some examples I've tried with, the first example gives me undefined function geofield_compute_values() :

$geofield_data = geofield_compute_values([
    'lat' => $lat,
    'lon' => $lon,
], GEOFIELD_INPUT_LAT_LON);

$cbisProduct->set('field_koordinater', $geofield_data);

I have also tried this out with no successful result and no errors :

$geofield = [
    'geom' => "POINT (" . $lon . " " . $lat . ")",
    'geo_type' => 'point',
    'lat' => $lat,
    'lon' => $lon,
    'left' => $lon,
    'top' => $lat,
    'right' => $lon,
    'bottom' => $lat,
];

$cbisProduct->set('field_koordinater', $geofield);

2 Answers 2

8
+50

Seems like you're trying to use the geofield_compute_values() function which was available in 7.x version, but not in 8.x

You should look into the wkt_generator service. i.e.

<?php $wktGenerator = \Drupal::service('geofield.wkt_generator'); ?>

I haven't tried this, but something like this should work:

<?php

$point = [
  'lat' => $request->get('lat'), 
  'lon' => $request->get('lon'),
];
$value = \Drupal::service('geofield.wkt_generator')->WktBuildPoint($point);

$node->field_koordinater->setValue($value);

Also, WktGeneratorTest.php and GeofieldItemTest.php files could be a good start to see how to use the service in your implementation.

1
  • 1
    If anyone finds this, DON'T CONFUSE geolocation and geofield as I did! Geolocation fields are set like this: stefvanlooveren.me/blog/… Geofields are set just as @osman stated in his answer – thank you for that! Jul 11, 2022 at 13:30
3

This function is not available in Drupal 8. You have to rely on the basic GeofieldItem class that extends FieldItemBase. Also, as mentioned by oman, you can use WktGenerator to easily build points, polygons, etc.

Here a working example. Let's say your have an entity $cbisProduct with a multivalued geofield field_koordinater, and you want to set the first item with arbitrary lat/lon coordinates :

// Get geofield item
$geofield = $cbisProduct->get('field_koordinater')->get(0);

// Generate a point [lat, lon]
$coord = ['45.909621', '6.127147'];
$point = \Drupal::service('geofield.wkt_generator')->WktBuildPoint($coord);

// Calling this function will compute values AND assign geodata to the field instance
$geofield->setValue($point);

// You can read the computed geodata from the field
$geodata = $geofield->getValue();
//dpm($geodata);

// Explicitly set field data (needed if $geofield is not a reference)
$cbisProduct->set('field_koordinater', [$geodata]);

// Save entity
$cbisProduct->save();

Under the hood, GeofieldItem::setValue calls another method responsible to directly assign the computed values to the field instance :

# \Drupal\geofield\Plugin\Field\FieldType\GeofieldItem
protected function populateComputedValues() {
  /* @var \Geometry $geom */
  $geom = \Drupal::service('geofield.geophp')->load($this->value);

  if (!empty($geom)) {
    /* @var \Point $centroid */
    $centroid = $geom->getCentroid();
    $bounding = $geom->getBBox();

    $this->geo_type = $geom->geometryType();
    $this->lon = $centroid->getX();
    $this->lat = $centroid->getY();
    $this->left = $bounding['minx'];
    $this->top = $bounding['maxy'];
    $this->right = $bounding['maxx'];
    $this->bottom = $bounding['miny'];
    $this->geohash = substr($geom->out('geohash'), 0, GEOFIELD_GEOHASH_LENGTH);
    $this->latlon = $centroid->getY() . ',' . $centroid->getX();
  }
}

Note : You don't necessarily need WktGenerator for building points, as long as you know the geofield type and how geophp should handle it. For example, the following 2 statements are equivalent :

$point = \Drupal::service('geofield.wkt_generator')->WktBuildPoint($coord);
// is equivalent to 
$point = GEOFIELD_TYPE_POINT . '(' . implode(' ', $coord) . ')');

But it is safer to rely on the WktGenerator especially with more complex data types.

Your Answer

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

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