You are here

function farm_movement_populate_geometry in farmOS 7

Helper function for populating a movement field collection geometry from the "move to" area reference field.

Parameters

Entity $entity: The entity to act upon.

See also

farm_log_entity_presave().

1 call to farm_movement_populate_geometry()
farm_movement_entity_presave in modules/farm/farm_movement/farm_movement.module
Implements hook_entity_presave().

File

modules/farm/farm_movement/farm_movement.module, line 223
Farm movement.

Code

function farm_movement_populate_geometry($entity) {

  // Define the area field name.
  $area_field = 'field_farm_move_to';

  // If the log doesn't have an area reference field, bail.
  if (!isset($entity->{$area_field})) {
    return;
  }

  // If a geometry is already defined, bail.
  if (!empty($entity->field_farm_geofield[LANGUAGE_NONE][0]['geom'])) {
    return;
  }

  // Load the area(s) referenced by the area reference field.
  $area_ids = array();
  if (!empty($entity->{$area_field}[LANGUAGE_NONE])) {
    foreach ($entity->{$area_field}[LANGUAGE_NONE] as $area_reference) {
      if (!empty($area_reference['tid'])) {
        $area_ids[] = $area_reference['tid'];
      }
    }
  }

  // Extract geometries from the areas.
  $geoms = farm_area_extract_geoms($area_ids);

  // Populate the geofield.
  farm_map_geofield_populate($entity, $geoms);
}