Created
March 8, 2012 19:54
-
-
Save sprice/2003047 to your computer and use it in GitHub Desktop.
Provide image field default images in Drupal 7
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
<?php | |
// General function in base feature or custom module. | |
/** | |
* Return a file object after saving a default image and storing the fid | |
* in a variable. | |
* | |
* @param $default_image | |
* An associative array containing: | |
* - scheme: File API stream wrapper scheme (ie: 'public://', 'private://') | |
* - dest_directory: The directory within the files directory the default | |
* image will be copied to. No leading or trailing slash. | |
* - source_path: The full path to the existing default image that will | |
* be copied. | |
* - file_name: The name of the default image. | |
* - variable: The variable name that will store the default image fid. | |
* | |
* @return | |
* A file object of the saved default image file. | |
*/ | |
function default_image_create($default_image) { | |
$file_uri = $default_image['scheme'] . $default_image['dest_directory'] . '/' . $default_image['file_name']; | |
$full_dir = $default_image['scheme'] . $default_image['dest_directory']; | |
file_prepare_directory($full_dir, FILE_CREATE_DIRECTORY); | |
file_unmanaged_copy($default_image['source_path'], $file_uri, FILE_EXISTS_REPLACE); | |
$file = (object) array( | |
'uid' => 1, | |
'status' => FILE_STATUS_PERMANENT, | |
'filename' => $default_image['file_name'], | |
'uri' => $file_uri, | |
'filemime' => file_get_mimetype($default_image['source_path']), | |
); | |
$file = file_save($file); | |
if ($file) { | |
variable_set($default_image['variable'], $file->fid); | |
} | |
return $file; | |
} | |
// Generate default image from custom feature module .install file. Assumes | |
// /images directory containing default image. | |
/** | |
* Implements hook_install(). | |
*/ | |
function custom_install() { | |
$default_image = array( | |
'scheme' => 'public://', | |
'dest_directory' => 'default_images', | |
'source_path' => drupal_get_path('module', 'custom') . '/images/' . 'default.png', | |
'file_name' => 'default.png', | |
'variable' => 'default_image_fid', | |
); | |
$file = default_image_create($default_image); | |
} | |
/** | |
* Implements hook_uninstall(). | |
*/ | |
function custom_uninstall() { | |
$fid = variable_get('custom_default_image_fid', FALSE); | |
if ($fid) { | |
$file = file_load($fid); | |
file_delete($file, TRUE); | |
variable_del('custom_default_image_fid'); | |
} | |
} | |
// Override the image field default fid in feature module .module file. | |
/** | |
* Implements hook_field_default_fields_alter(). | |
* | |
* Set the default image. | |
*/ | |
function custom_field_default_fields_alter(&$fields) { | |
if (isset($fields['node-custom-field_image']['field_config']['settings']['default_image'])) { | |
$fid = $fields['node-custom-field_image']['field_config']['settings']['default_image']; | |
$fields['node-custom-field_image']['field_config']['settings']['default_image'] = variable_get('default_image_fid', $fid); | |
} | |
} |
@glesage This is something in Features, not Drupal core.
Thx for sharing. Some notes: this hook comes from features. E.g. it runs at /admin/structure/features
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey guys, I've been trying to do this with the current version of Drupal (7.26) but cannot seem to get this hook to do anything...
Where can I get info about it? Google search brings up nothing... Has the naming changed? (As suggested in previous post)?
Thanks!