You are here

function getlocations_fields_field_update in Get Locations 7

Same name and namespace in other branches
  1. 7.2 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 276
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'];
  }
  $mode = 'update';
  if (isset($entity->revision) && $entity->revision && (isset($entity->nid) && isset($entity->vid) && $entity->nid !== $entity->vid)) {
    $mode = 'insert';
  }
  $settings['display_settings'] = getlocations_fields_get_display_settings($instance['display']['default']['settings']);
  if (!empty($items)) {
    $criteria = array();

    // 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 ($entity_type == 'node') {
      $criteria = array(
        'field_name' => $field['field_name'],
        'vid' => $entity->vid,
        'nid' => $entity->nid,
      );
    }
    elseif ($entity_type == 'user' || $entity_type == 'profile2') {
      $criteria = array(
        'field_name' => $field['field_name'],
        'uid' => $entity->uid,
      );
    }
    elseif ($entity_type == 'comment') {
      $criteria = array(
        'field_name' => $field['field_name'],
        'cid' => $entity->cid,
      );
    }
    elseif ($entity_type == 'taxonomy_term') {
      $criteria = array(
        'field_name' => $field['field_name'],
        'tid' => $entity->tid,
      );
    }
    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);
    }
  }
}