0

I'm trying to build a custom "edit profile" page and use a lot the field_attach_form function in order to build it :

field_attach_form('user', $user_entity, $form, $form_state, NULL, array('field_name' => 'field_user_firstname'));
field_attach_form('user', $user_entity, $form, $form_state, NULL, array('field_name' => 'field_user_lastname'));
field_attach_form('user', $user_entity, $form, $form_state, NULL, array('field_name' => 'field_user_organisation'));

I'm trying to insert input for user mail and user picture, but as they are properties and not fields, the following snippet is not working.

field_attach_form('user', $user_entity, $form, $form_state, NULL, array('field_name' => 'mail'));

Is it possible to insert user's properties form with this function ? or is there another way ?

2
  • Have you ever thought about a solution with display suite?
    – Huelfe
    Commented Sep 28, 2015 at 14:34
  • 1
    field_attach_form only attaches... Fields. You will have to use user_account_form directly and tinker around. I had to do this, and ended up creating a custom form wrapper.
    – AKS
    Commented Sep 28, 2015 at 18:26

2 Answers 2

2

I was working on a similar task; my task was to put the address field into the popup and I used the following code to accomplish it. I looked at modules/user/user.page.inc for it.

    function MYMODULE_init() {
         //To avoid user_profile_form_validate function not found
         module_load_include('inc', 'user', 'user.pages');
         } //end init function

        //Custom form for fields
        function MYMODULE_form($form, &$form_state) {

        $form = array();
        global $user;
        $account = user_load($user->uid);

        if (!isset($form_state['user'])) {
            $form_state['user'] = $account;
          }
          else {
            $account = $form_state['user'];
          }

        $form['#user'] = $account;
        user_account_form($form, $form_state);
        // Attach field widgets.
            $langcode = entity_language('user', $account);
            field_attach_form('user', $account, $form, $form_state, $langcode);
        //All fields get attached so removing unnecessary fields.  
            unset($form['account']['pass']);
            unset($form['account']['roles']);
 $key = array_search('user_validate_current_pass', form['#validate']);
    if ($key !== FALSE) {
      unset($form['#validate'][$key]);
    }
    // hide the current password fields
    $form['account']['current_pass_required_value']['#access'] = FALSE;
    $form['account']['current_pass']['#access'] = FALSE;

            $form['actions'] = array('#type' => 'actions');
            $form['actions']['submit'] = array(
            '#type' => 'submit',
            '#value' => t('Save'),
           );

          $form['actions']['cancel'] = array(
            '#type' => 'submit',
            '#value' => t('Cancel'),
            '#submit' => array('MYMODULE_form_cancel'),
            '#limit_validation_errors' => array(),
          );
          $form['#validate'][] = 'user_profile_form_validate';
          // Add the final user profile form submit handler.
          $form['#submit'][] = 'user_profile_form_submit';

          return $form;
        }

        // Add function for CANCEL button.
        function MYMODULE_form_cancel(&$form_state) {
        $url = $_GET['destination'] ? $_GET['destination'] : 'choose/your/path';
          drupal_goto($url);
        }
0

Thanks to kkalaskar i managed to do what I was looking for :

function _my_module_edit_profile() {
  global $user;
  $user_entity = user_load($user->uid);
  $var['form'] = drupal_get_form('_my_module_edit_profile_form',$user_entity);
  return render($var['form']);
}

function _my_module_edit_profile_form($form, &$form_state) {
  $account = $form_state['build_info']['args'][0];
  if (!isset($form_state['user'])) {
    $form_state['user'] = $account;
  }
  else {
    $account = $form_state['user'];
  }
  $form['#user'] = $account;
  $form['#user_category'] = 'account';
  user_account_form($form, $form_state);
  $langcode = entity_language('user', $account);
  field_attach_form('user', $account, $form, $form_state, $langcode);

  $form['account']['#access'] = false;
  $form['field_user_initialized']['#access'] = false;

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Done !'),
    '#weight' => 100
  );
  $form['#validate'][] = 'user_profile_form_validate';
  $form['#submit'][] = 'user_profile_form_submit';
  return $form;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.