You are here

function file_entity_set_title_alt_properties_on_file_fields in File Entity (fieldable files) 7.2

Same name and namespace in other branches
  1. 7.3 file_entity.file.inc \file_entity_set_title_alt_properties_on_file_fields()

Sets the title / alt properties on file fields attached to entities.

Files attached to a file or image field can be stored in the field cache or entity cache for whichever entity that the field is attached to. Because $file->alt and $file->title are set in file_entity_file_load() based on the current page language, they will go into the cache with that language as well. To ensure that the correct language is used when the entity is later loaded and displayed in a different language, the alt and title properties can be set again using this function.

Parameters

array $entities: An array of entity objects of the same type.

string $entity_type: The type of entity.

See also

file_entity_entity_load()

file_entity_entitycache_load()

1 call to file_entity_set_title_alt_properties_on_file_fields()
file_entity_entity_load in ./file_entity.file.inc
Implements hook_entity_load().

File

./file_entity.file.inc, line 242
File hooks implemented by the File entity module.

Code

function file_entity_set_title_alt_properties_on_file_fields($entities, $entity_type) {
  foreach ($entities as $entity) {
    list(, , $bundle) = entity_extract_ids($entity_type, $entity);
    foreach (field_info_instances($entity_type, $bundle) as $instance) {
      if (!empty($entity->{$instance['field_name']})) {
        foreach ($entity->{$instance['field_name']} as &$items) {
          foreach ($items as &$item) {

            // We need to detect any field items that passed through
            // file_field_load(), whether they are files, images, or something
            // else. There is no direct way to do that, but checking for a few
            // expected file properties on the field item should be sufficient.
            if (is_array($item) && !empty($item['fid']) && isset($item['uri']) && isset($item['filename'])) {
              $file = (object) $item;
              file_entity_set_title_alt_properties(array(
                $file,
              ));
              $item = (array) $file;
            }
          }
        }
      }
    }
  }
}