function file_entity_field_widget_form_alter in File Entity (fieldable files) 7.3
hook_field_widget_form_alter
File
- ./
file_entity.module, line 2748 - Extends Drupal file entities to be fieldable and viewable.
Code
function file_entity_field_widget_form_alter(&$element, &$form_state, $context) {
// This hook is used to prepopulate the alt and title field in any image field widget,
// with the values which are set in the file_entity fields,
// but ONLY if there is a alt/title set in the original file entity,
// and ONLY if the values are empty.
if ($context['field']['type'] == 'image') {
foreach (element_children($element) as $delta) {
$fid = $element[$delta]['#default_value']['fid'];
$file = entity_load('file', array(
$fid,
), $conditions = array(), $reset = FALSE);
if ($file) {
// not 100% sure whether this is the right langcode to use, when entity translations are enabled.
$langcode = $context['langcode'];
$alt_field = field_get_items('file', $file[$fid], 'field_file_image_alt_text', $langcode);
$title_field = field_get_items('file', $file[$fid], 'field_file_image_title_text', $langcode);
if (!empty($alt_field[0]['value']) && empty($element[$delta]['#default_value']['alt'])) {
$element[$delta]['#default_value']['alt'] = $alt_field[0]['value'];
}
if (!empty($title_field[0]['value']) && empty($element[$delta]['#default_value']['title'])) {
$element[$delta]['#default_value']['title'] = $title_field[0]['value'];
}
}
}
}
}