You are here

function geocoder_widget_get_field_value in Geocoder 7

Get a field's value based on geocoded data.

Parameters

string $entity_type: Type of entity.

array $field_instance: Field instance definition array.

mixed $entity: Optionally, the entity. Pass either the entity or $source_field_values.

array $source_field_values: Array of deltas / source field values. Pass either this or $entity.

Return value

bool|array Three possibilities could be returned by this function:

  • FALSE: do nothing.
  • An empty array: use it to unset the existing field value.
  • A populated array: assign a new field value.
3 calls to geocoder_widget_get_field_value()
geocoder_field_attach_load in ./geocoder.widget.inc
Implements hook_field_attach_load().
geocoder_field_attach_presave in ./geocoder.widget.inc
Implements hook_field_attach_presave().
geocoder_field_widget_form in ./geocoder.widget.inc
Implements hook_field_widget_form().

File

./geocoder.widget.inc, line 411
geocoder.widget.inc

Code

function geocoder_widget_get_field_value($entity_type, array $field_instance, $entity = NULL, array $source_field_values = NULL) {
  if (!$source_field_values && !$entity) {
    trigger_error('geocoder_widget_get_field_value: You must pass either $source_field_values OR $entity', E_USER_ERROR);
    return FALSE;
  }

  // Required settings.
  if (isset($field_instance['widget']['settings']['geocoder_handler']) && isset($field_instance['widget']['settings']['geocoder_field'])) {
    $handler = geocoder_get_handler($field_instance['widget']['settings']['geocoder_handler']);
    $field_name = is_array($field_instance['widget']['settings']['geocoder_field']) ? reset($field_instance['widget']['settings']['geocoder_field']) : $field_instance['widget']['settings']['geocoder_field'];
    $target_info = field_info_field($field_instance['field_name']);
    $field_info = geocoder_widget_get_field_info($entity_type, $field_instance, $entity);

    // Get the source values.
    if (!$source_field_values) {
      $source_field_values = geocoder_widget_get_entity_field_value($entity_type, $field_instance, $entity);
    }

    // If no valid source values were passed.
    if (empty($source_field_values)) {
      return array();
    }

    // Get the handler-specific-settings.
    if (isset($field_instance['widget']['settings']['handler_settings'][$handler['name']])) {
      $handler_settings = $field_instance['widget']['settings']['handler_settings'][$handler['name']];
    }
    else {
      $handler_settings = array();
    }

    // Determine how we deal with deltas (multi-value fields)
    if (empty($field_instance['widget']['settings']['delta_handling'])) {
      $delta_handling = 'default';
    }
    else {
      $delta_handling = $field_instance['widget']['settings']['delta_handling'];
    }

    // Check to see if we should be concatenating.
    if ($delta_handling === 'c_to_s' || $delta_handling === 'c_to_m') {
      $source_field_values = geocoder_widget_get_field_concat($source_field_values);
    }

    // Allow other modules to alter values before we geocode them.
    drupal_alter('geocoder_geocode_values', $source_field_values, $field_info, $handler_settings, $field_instance);
    if (is_array($source_field_values) && count($source_field_values)) {

      // Geocode geometries.
      $geometries = array();
      foreach ($source_field_values as $delta => $item) {
        $geometry = NULL;
        if (!variable_get('geocoder_recode', 0)) {

          // Attempt to retrieve from persistent cache.
          $geometry = geocoder_cache_get($handler['name'], $item, $handler_settings);
        }

        // No cache record, so fetch live.
        if ($geometry === NULL || $geometry === FALSE && !variable_get('geocoder_cache_empty_results')) {

          // Geocode any value from our source field.
          try {
            $geometry = call_user_func($handler['field_callback'], $field_info, $item, $handler_settings);

            // Save result persistently.
            geocoder_cache_set($geometry, $handler['name'], $item, $handler_settings);
          } catch (Exception $e) {
            watchdog_exception('geocoder', $e, NULL, array(), WATCHDOG_ERROR, geocoder_widget_get_link($entity_type, $entity));
            continue;
          }
        }
        if ($geometry instanceof Geometry) {
          $geometries[] = $geometry;
        }
        elseif (variable_get('geocoder_log_empty_results', FALSE)) {
          watchdog('geocoder', 'No results for geocoding', NULL, WATCHDOG_NOTICE, geocoder_widget_get_link($entity_type, $entity));
        }
      }
      if (empty($geometries)) {

        // This field has no data, so set the field to an empty array in
        // order to delete its saved data.
        return array();
      }

      // Resolve multiple-values - get back values from our delta-resolver.
      $values = geocoder_widget_resolve_deltas($geometries, $delta_handling, $target_info);

      // Set the values - geofields do not support languages.
      return array(
        LANGUAGE_NONE => $values,
      );
    }
  }
}