Skip to content

Instantly share code, notes, and snippets.

@mrded
Created September 16, 2013 15:29
Show Gist options
  • Save mrded/6582200 to your computer and use it in GitHub Desktop.
Save mrded/6582200 to your computer and use it in GitHub Desktop.
File attachment VIA Form API
function wj_profile_photo_upload_form() {
$form = array();
$instance = field_info_instance('profile2', 'field_photo', 'profile');
$field = field_info_field('field_photo');
$form['profile_field_photo'] = array(
'#type' => 'managed_file',
'#upload_location' => file_field_widget_uri($field, $instance),
'#upload_validators' => file_field_widget_upload_validators($field, $instance),
'#value_callback' => 'file_field_widget_value',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Upload'),
'#id' => 'wj-profile-photo-upload',
);
$form['#attributes']['enctype'] = "multipart/form-data";
return $form;
}
function wj_profile_photo_upload_form_submit($form, &$form_state) {
if ($fid = $form_state['values']['profile_field_photo']) {
db_update('file_managed')->fields(array('status' => 1))->condition('fid', $fid)->execute();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment