You are here

function getlocations_fields_field_update in Get Locations 7.2

Same name and namespace in other branches
  1. 7 modules/getlocations_fields/getlocations_fields.module \getlocations_fields_field_update()

Implements hook_field_update(). Define custom update behavior for this module's field types.

Parameters

$entity_type: The type of $entity.

$entity: The entity for the operation.

$field: The field structure for the operation.

$instance: The instance structure for $field on $entity's bundle.

$langcode: The language associated with $items.

$items: $entity->{$field['field_name']}[$langcode], or an empty array if unset.

File

modules/getlocations_fields/getlocations_fields.module, line 252
getlocations_fields.module @author Bob Hutchinson http://drupal.org/user/52366 @copyright GNU GPL

Code

function getlocations_fields_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
  $settings = FALSE;
  if (isset($field['settings'])) {
    $settings = $field['settings'];
  }
  $settings['display_settings'] = getlocations_fields_get_display_settings($instance['display']['default']['settings']);
  $mode = 'update';
  if (isset($entity->revision) && $entity->revision && (isset($entity->nid) && isset($entity->vid) && $entity->nid !== $entity->vid)) {
    $mode = 'insert';
  }
  if (!empty($items)) {
    $criteria = array();
    if ($field['field_name']) {
      $criteria = array(
        'field_name' => $field['field_name'],
      );
      $et_info = entity_get_info($entity_type);
      $id_name = isset($et_info['entity keys']['id']) ? $et_info['entity keys']['id'] : '';
      if ($id_name && isset($entity->{$id_name}) && $entity->{$id_name} > 0) {
        $criteria['entity_type'] = $entity_type;
        $criteria['entity_id'] = $entity->{$id_name};
      }
    }

    // feeds sanity
    if (module_exists('feeds') && module_exists('getlocations_feeds') && isset($entity->feeds_item) && !$entity->feeds_item->is_new) {

      // feeds update, set active so that glid can be added later
      foreach ($items as $key => $item) {
        if (!isset($items[$key]['active'])) {
          $items[$key]['active'] = 1;
        }
      }
    }
    if (!empty($criteria)) {

      // look for delete_location in $items
      if (!$instance['required'] || $instance['required'] && count($items) > 1) {
        foreach ($items as $key => $item) {
          if (isset($item['delete_location']) && $item['delete_location'] > 0) {
            getlocations_fields_delete_record($items[$key], $criteria);
            unset($items[$key]);
          }
        }
      }
      $items = getlocations_fields_save_locations($items, $criteria, $settings, $mode);
    }
  }
}