Created
October 19, 2017 11:50
-
-
Save szeidler/675fe82b145b32ad41b5510d5f893a7c to your computer and use it in GitHub Desktop.
Merge Fotoweb metadata into File Entity fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function mymodule_file_presave($file) { | |
if ($file->type === 'image' && file_uri_scheme($file->uri) === 'fotoweb') { | |
$wrapper = entity_metadata_wrapper('file', $file); | |
$field_map = array( | |
'field_file_image_alt_text' => 120, | |
'field_file_image_title_text' => 5, | |
'field_image_caption' => 121, | |
'field_image_byline' => 80, | |
); | |
$metadata = $file->metadata['asset_metadata']; | |
try { | |
foreach ($field_map as $field_name => $metadata_key) { | |
// Update field only, when existing and metadata is not empty. | |
if ($wrapper->__isset($field_name) && !empty($metadata[$metadata_key]['value'])) { | |
$value = $metadata[$metadata_key]['value']; | |
// Some attributes are nested, concatenate them comma-separated. | |
if (is_array($value)) { | |
$value = implode(', ', $value); | |
} | |
// We need to divide between textfields and text_formatted fields. | |
if ($wrapper->{$field_name}->type() === 'text_formatted') { | |
$wrapper->{$field_name}->value = $value; | |
$wrapper->{$field_name}->format = 'caption'; | |
} | |
else { | |
$wrapper->{$field_name} = $value; | |
} | |
} | |
} | |
} catch (EntityMetadataWrapperException $exception) { | |
watchdog('mymodule', $exception->getMessage(), array(), WATCHDOG_ERROR); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment