Created
December 10, 2012 15:03
-
-
Save rafinskipg/4251090 to your computer and use it in GitHub Desktop.
Default images for fields in features
This file contains 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
function module_field_default_fields_alter(&$fields) { | |
$source_dir = drupal_get_path('theme', 'soc') . '/images/default'; | |
$field_default_images = array( | |
//'user-user-field_user_picture' => 'avatar.png', - This is exported in the feature | |
//'field_collection_item-field_people-field_people_photo' => 'avatar.png', - example for field collection | |
'node-event-field_event_image' => 'minificha_eventos.gif', | |
'node-news-field_image' => 'minificha_noticias.gif', | |
); | |
foreach ($field_default_images as $field_name => $filename) { | |
$source = $source_dir . '/' . $filename; | |
$destination = 'default_images/' . $filename; | |
if (isset($fields[$field_name])) { | |
module_set_default_image($fields[$field_name], $filename, $source, $destination); | |
} | |
} | |
} | |
function module_set_default_image(&$field, $filename, $source, $destination) { | |
// See if a default image hasn't been set for this field yet | |
if (empty($field['field_config']['settings']['default_image'])) { | |
// Dynamically set the user default image on the field | |
$destination = file_default_scheme() . '://' . $destination; | |
// Check to see if it exists already | |
$result = db_select('file_managed', 'f') | |
->fields('f', array('fid')) | |
->condition('f.uri', $destination) | |
->execute(); | |
$fid = $result->fetchField(); | |
// Simulate an upload of the default user image | |
if (!$fid && file_exists($source)) { | |
$file = new stdClass; | |
$file->filename = $filename; | |
$file->timestamp = REQUEST_TIME; | |
$file->uri = $source; | |
$file->filemime = file_get_mimetype($source); | |
$file->uid = 1; | |
$file->status = 1; | |
$file = file_copy($file, 'public://', FILE_EXISTS_REPLACE); | |
$fid = $file->fid; | |
} | |
$field['field_config']['settings']['default_image'] = (string) $fid; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment