function exif_custom_process_entity in EXIF Custom 7
Process a given entity to see if it has EXIF data to be saved.
Parameters
object $file: The file being processed.
1 call to exif_custom_process_entity()
- exif_custom_entity_presave in ./
exif_custom.module - Implements hook_entity_presave().
File
- ./
exif_custom.module, line 148 - Primary hook implementations for EXIF Custom.
Code
function exif_custom_process_entity(&$file) {
if (!isset($file->type) || !isset($file->uri)) {
return;
}
if ($file->type != 'image') {
return;
}
if (arg(3) == 'exif_custom') {
return;
}
// Media Browser saves in two steps: first a temporary file is saved after
// which the user is presented with a form an can edit fields on the file.
// After potentially updating the file fields the user presses save and the
// file is made permanent and saved. As exif_custom_process_entity is invoked
// in both situations we need to detect the last save and avoid mapping as it
// would overwrite any changes the user made.
if (arg(0) == 'media' && arg(1) == 'browser' && $file->status == FILE_STATUS_PERMANENT) {
return;
}
// Skip mapping if the file has a path that is excluded.
$patterns = variable_get('exif_custom_exclude_paths', NULL);
if (!empty($patterns) && drupal_match_path($file->uri, $patterns)) {
return;
}
$data = exif_custom_get_exif_fields($file->uri, TRUE);
if (empty($data)) {
return;
}
$mappings = exif_custom_get_mapping();
if (empty($mappings)) {
return;
}
$lang = entity_language('file', $file);
if (!$lang) {
$lang = LANGUAGE_NONE;
}
foreach ($mappings as $field => $values) {
// Ignore fields that aren't present.
if (!isset($data[$values->exif_field])) {
continue;
}
// File name is special case.
if ($field == 'filename') {
$file->filename = $data[$values->exif_field];
continue;
}
// Optionally process fields which have already been imported. This avoids
// re-processing files which have already been saved.
if (!variable_get('exif_custom_overwrite_existing') && !empty($file->{$field})) {
continue;
}
$array = array();
$field_info = field_info_field($field);
$instance_info = field_info_instance('file', $field, 'image');
switch ($field_info['type']) {
case 'taxonomy_term_reference':
$vids = array();
foreach ($field_info['settings']['allowed_values'] as $allowed_value) {
$vocabulary = taxonomy_vocabulary_machine_name_load($allowed_value['vocabulary']);
if ($allowed_value['vocabulary'] === 0) {
break;
}
$vids[$vocabulary->vid] = $vocabulary;
}
$element = array();
$element['#value'] = isset($data[$values->exif_field]) ? $data[$values->exif_field] : FALSE;
$form_state = array();
$array = array();
$array[$lang] = _exif_custom_taxonomy_autocomplete_validate($element, $vids);
break;
case 'country':
$countries = array_flip(countries_get_countries('name'));
if (isset($countries[$data[$values->exif_field]])) {
$array[$lang][0]['iso2'] = $countries[$data[$values->exif_field]];
}
break;
case 'creative_commons':
module_load_include('module', 'creative_commons');
$licence_by_id = array(
'CC_NONE' => 1,
'CC_BY' => 2,
'CC_BY_SA' => 3,
'CC_BY_ND' => 4,
'CC_BY_NC' => 5,
'CC_BY_NC_SA' => 6,
'CC_BY_NC_ND' => 7,
'CC_0' => 8,
'CC_PD' => 9,
);
$licence_by_id = array_merge($licence_by_id, array_flip(creative_commons_get_licence_types()));
if (isset($licence_by_id[$data[$values->exif_field]])) {
$array[$lang]['0']['licence'] = $licence_by_id[$data[$values->exif_field]];
}
break;
case 'date':
$array[$lang]['0']['value'] = strtotime($data[$values->exif_field]);
break;
default:
$array[$lang]['0']['value'] = $data[$values->exif_field];
}
// Set the values.
$file->{$field} = $array;
}
}