You are here

function imageinfo_cache_entity_insert in Imageinfo Cache 7.3

Implements hook_entity_insert().

Capture image on entity save.

1 call to imageinfo_cache_entity_insert()
imageinfo_cache_entity_update in ./imageinfo_cache.module
Implements hook_entity_update().

File

./imageinfo_cache.module, line 193
Imageinfo Cache module.

Code

function imageinfo_cache_entity_insert($entity, $type) {
  $image_fields = array();
  $return = array();

  // Set bundle name.
  $bundle_name = NULL;
  if (!empty($entity->type)) {
    $bundle_name = $entity->type;
  }
  elseif (!empty($entity->bundle)) {
    $bundle_name = $entity->bundle;
  }
  elseif (!empty($entity->field_name)) {
    $bundle_name = $entity->field_name;
  }
  else {

    // Get the additional base details about this entity.
    list(, , $bundle) = entity_extract_ids($type, $entity);
    if (!empty($bundle)) {
      $bundle_name = $bundle;
    }
  }

  // Get field info.
  $instances = field_info_instances($type, $bundle_name);
  if (empty($instances)) {
    return $return;
  }

  // Find all image/media fields.
  foreach ($instances as $field_name => $values) {
    if (!empty($values['widget']['type'])) {
      $image_type = imageinfo_cache_detect_image_widget($values['widget']);
      if (!empty($image_type)) {
        $image_fields[] = $field_name;
      }
    }
  }
  if (empty($image_fields)) {
    return $return;
  }

  // Go into each image field and generate image styles.
  foreach ($image_fields as $field_name) {
    if (empty($entity->{$field_name})) {
      continue;
    }
    $field_language = field_language($type, $entity, $field_name, isset($entity->language) ? $entity->language : NULL);
    if (empty($entity->{$field_name}[$field_language])) {
      continue;
    }
    $instance_field = $instances[$field_name];
    $fids = array();
    foreach ($entity->{$field_name}[$field_language] as $field_values) {

      // Skip if no fid.
      if (empty($field_values['fid'])) {
        continue;
      }
      $fids[] = $field_values['fid'];
    }

    // Generate Image styles.
    $return[$field_name] = imageinfo_cache_create_image_styles_fids($fids, $instance_field, TRUE);
  }
  return $return;
}