function media_library_field_attach_presave in Media Library 7
Implements hook_field_attach_presave().
File
- ./
media_library.module, line 118 - Restricts the media library to only designated files.
Code
function media_library_field_attach_presave($entity_type, $entity) {
// Find the entity's bundle.
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
// Examine every field instance attached to this entity's bundle.
foreach (field_info_instances($entity_type, $bundle) as $field_name => $instance) {
// Load the instance's field info.
$field = field_info_field($instance['field_name']);
// Load the instance's field instance info.
$instance = field_info_instance($entity_type, $field_name, $bundle);
// Check if the instance is a file or image field.
if ($field['type'] == 'file' || $field['type'] == 'image') {
if (!empty($entity->{$field_name})) {
foreach ($entity->{$field_name} as $langcode => $items) {
foreach ($items as $delta => $item) {
// Add or remove the file from the media library depending on the
// field instance setting.
if (isset($instance['settings']['media_library_include_in_library'])) {
$file = file_load($item['fid']);
$file->library = $instance['settings']['media_library_include_in_library'];
file_save($file);
}
}
}
}
}
}
}