Skip to content

Instantly share code, notes, and snippets.

@alex-kirienko
Created January 29, 2016 15:11
Show Gist options
  • Save alex-kirienko/8def8bbc8add7f9882f8 to your computer and use it in GitHub Desktop.
Save alex-kirienko/8def8bbc8add7f9882f8 to your computer and use it in GitHub Desktop.
Drupal form attach one field
<?php
function wj_users_reg_interests_form($form = array(), &$state) {
$account = user_load($GLOBALS['user']->uid);
field_attach_form('user', $account, $form, $state, NULL, array('field_name' => 'field_interests'));
// Add submit button.
$form['actions'] = array(
'#type' => 'actions',
'submit' => array(
'#type' => 'submit',
'#value' => t('Next Step'),
'#attributes' => array(
'class' => array('btn btn-primary btn-lg pull-right'),
),
),
);
// Overwrite all other submit handlers, we'll run them manually in submit handler
$form['#submit'] = array(
'wj_users_reg_interests_form_submit'
);
return $form;
}
function wj_users_reg_interests_form_submit($form, &$form_state) {
$account = $form['field_interests'][LANGUAGE_NONE]['#entity'];
// Execute submit hook, removes empty fields.
field_attach_submit('user', $account, $form, $form_state);
// Populate $edit with the properties of $account, which have been edited on
// this form by taking over all values, which appear in the form values too.
$edit = array_intersect_key((array) $account, $form_state['values']);
$account = user_save($account, $edit);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment