3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00

Allow other modules to help extract geometries from entities via hook_farm_map_entity_geometries().

This commit is contained in:
Michael Stenta 2018-05-30 15:30:19 -04:00
parent 2b4df1054f
commit 91cb17bb7f
3 changed files with 108 additions and 18 deletions

View file

@ -0,0 +1,50 @@
<?php
/**
* @file
* Hooks provided by farm_map.
*
* This file contains no working PHP code; it exists to provide additional
* documentation for doxygen as well as to document hooks in the standard
* Drupal manner.
*/
/**
* @defgroup farm_map Farm map module integrations.
*
* Module integrations with the farm_map module.
*/
/**
* @defgroup farm_map_hooks Farm map's hooks
* @{
* Hooks that can be implemented by other modules in order to extend farm_map.
*/
/**
* Extract geometries from an entity.
*
* @param $entity_type
* The entity type machine name.
* @param $entity
* The entity object.
*
* @return array
* Return an array of geometry strings in WKT format. An associative array
* is allowed, and the keys can be used to differentiate multiple geometries
* from the same entity.
*/
function hook_farm_map_entity_geometries($entity_type, $entity) {
$geometries = array();
// Find geometry in the standard geofield.
if (empty($entity->field_farm_geofield[LANGUAGE_NONE][0]['geom'])) {
$geometries[] = $entity->field_farm_geofield[LANGUAGE_NONE][0]['geom'];
}
return $geometries;
}
/**
* @}
*/

View file

@ -176,6 +176,39 @@ function farm_map_openlayers_object_preprocess_alter(&$build, $context) {
}
}
/**
* Extract geometries from an entity.
*
* @param $entity_type
* The entity type machine name.
* @param $entity
* The entity object.
*
* @return array
* Return an array of geometry strings in WKT format. An associative array
* is allowed, and the keys can be used to differentiate multiple geometries
* from the same entity.
*/
function farm_map_entity_geometries($entity_type, $entity) {
// Ask modules to extract geometries.
return module_invoke_all('farm_map_entity_geometries', $entity_type, $entity);
}
/**
* Implements hook_farm_map_geometries().
*/
function farm_map_farm_map_entity_geometries($entity_type, $entity) {
$geometries = array();
// Find geometry in the standard geofield.
if (!empty($entity->field_farm_geofield[LANGUAGE_NONE][0]['geom'])) {
$geometries[] = $entity->field_farm_geofield[LANGUAGE_NONE][0]['geom'];
}
return $geometries;
}
/**
* Implements hook_module_implements_alter().
*/

View file

@ -63,32 +63,39 @@ function farm_map_kml_action(array $entities, $context = array()) {
$placemarks = array();
foreach ($entities as $entity) {
// Get the geometry. If it is empty, skip the entity.
if (empty($entity->field_farm_geofield[LANGUAGE_NONE][0]['geom'])) {
continue;
}
$geometry = $entity->field_farm_geofield[LANGUAGE_NONE][0]['geom'];
// Ask modules to extract geometries from this entity.
$geometries = farm_map_entity_geometries($context['entity_type'], $entity);
// Get the entity id and label.
list($id, $rid, $bundle) = entity_extract_ids($context['entity_type'], $entity);
$label = entity_label($context['entity_type'], $entity);
// Create a placemark.
$placemark = array(
'pid' => $id,
'name' => $label,
'geometry' => $geometry,
);
// Create a placemark for each geometry.
foreach ($geometries as $key => $geometry) {
// If this is an area entity (taxonomy_term), add the description.
if ($context['entity_type'] == 'taxonomy_term' && $entity->vocabulary_machine_name == 'farm_areas') {
if (!empty($entity->description)) {
$placemark['description'] = $entity->description;
// Create a placemark.
$placemark = array(
'pid' => $id,
'name' => $label,
'geometry' => $geometry,
);
// If a non-numeric key is set, tag the ID and label with it.
if (!is_numeric($key)) {
$placemark['pid'] .= '-' . check_plain($key);
$placemark['name'] .= ' (' . check_plain($key) . ')';
}
}
// Add the placemark to the list.
$placemarks[] = $placemark;
// If this is an area entity (taxonomy_term), add the description.
if ($context['entity_type'] == 'taxonomy_term' && $entity->vocabulary_machine_name == 'farm_areas') {
if (!empty($entity->description)) {
$placemark['description'] = $entity->description;
}
}
// Add the placemark to the list.
$placemarks[] = $placemark;
}
}
// If there are no placemarks, bail with a warning.