Pre-render callback to adjust weights and visibility of non-field elements.
function _field_extra_fields_pre_render($elements) {
$entity_type = $elements['#entity_type'];
$bundle = $elements['#bundle'];
if (isset($elements['#type']) && $elements['#type'] == 'form') {
$extra_fields = field_info_extra_fields($entity_type, $bundle, 'form');
foreach ($extra_fields as $name => $settings) {
if (isset($elements[$name])) {
$elements[$name]['#weight'] = $settings['weight'];
}
}
}
elseif (isset($elements['#view_mode'])) {
$view_mode = $elements['#view_mode'];
$extra_fields = field_extra_fields_get_display($entity_type, $bundle, $view_mode);
foreach ($extra_fields as $name => $settings) {
if (isset($elements[$name])) {
$elements[$name]['#weight'] = $settings['weight'];
// Visibility: make sure we do not accidentally show a hidden element.
$elements[$name]['#access'] = isset($elements[$name]['#access']) ? $elements[$name]['#access'] && $settings['visible'] : $settings['visible'];
}
}
}
return $elements;
}