You are here

function _node_gallery_api_update_entity_fields in Node Gallery 7

Update the field values in the entity, to reflect the membership.

This is used to allow other modules that save a new/ existing entity to act on the field values, even before hook_field_load() is called.

2 calls to _node_gallery_api_update_entity_fields()
node_gallery_api_field_attach_insert in ./node_gallery_api.module
Implements hook_field_attach_insert().
node_gallery_api_field_attach_update in ./node_gallery_api.module
Implements hook_field_attach_update().

File

./node_gallery_api.module, line 907
Node Gallery module.

Code

function _node_gallery_api_update_entity_fields($entity_type, $entity) {
  if ($entity_type != 'node') {
    return;
  }
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  $relationship_type = node_gallery_api_get_relationship_type(NULL, $bundle);
  if (empty($relationship_type)) {
    return;
  }
  $wrapper = entity_metadata_wrapper($entity_type, $entity);
  $field_name = node_gallery_api_get_item_field_name(NULL, NULL, $relationship_type->id);
  $field = field_info_field($field_name);
  $ngids = array();
  $relationships = node_gallery_api_get_relationships(NULL, $wrapper->nid
    ->value());
  if (!empty($relationships)) {
    if ($field['cardinality'] == 1) {
      $relationship = reset($relationships);
      $ngids = $relationship->ngid;
    }
    else {
      foreach ($relationships as $relationship) {
        $ngids[] = $ngids = $relationship->ngid;
      }
    }
    if ($ngids) {
      $wrapper->{$field_name}
        ->set($ngids);
    }
  }
}