1

I've got an Implementation of hook_form() on a custom page which I use for user registration.

For example I have this field:

function popover_form($form, &$form_state){

    // some other fields are left out here to simplify my question

    $form['second']['education'] = array(
      '#type' => 'radios',
      '#title' => 'Afgeronde opleiding',
      '#options' => popover_get_tax_term_options('education'),
      '#default_value' => variable_get('education', NULL),
      '#required' => TRUE,
    );
    return $form;
}

Then in form_submit() I'm trying to do this:

function popover_form_submit($form, &$form_state){
  $edit = array(

    // some other fields like username, email etc are also left out here.

    // field_firstname is a simple string and works.
    'field_firstname' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['first']['field_firstname']))),
    // education doesn't work ( because it is a taxonomy term ? )
    'education' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['second']['education']))),
  );
  user_save(drupal_anonymous_user(), $edit);
}

so either I need to change my submit handler because taxonomy terms are saved differently or I need to update the standard user edit 'view' ( $sitename/user/1/edit ) so the taxonomy terms become visible? Or I'm doing something else wrong?

EDIT: I also found out that it is needed to connect taxonomy terms with the user on the admin/config/people/accounts/fields page before you can get this to work.

3

The column name for a taxonomy term reference is 'tid' rather than 'value', if you just change that your code should work:

$edit = array(
  'field_firstname' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['first']['field_firstname']))),
  'education' => array(LANGUAGE_NONE => array(array('tid' => $form_state['values']['second']['education']))),
);

user_save(drupal_anonymous_user(), $edit);
5
  • I've changed my code but can see the data stored anywhere. where should the data be stored in the database so I can check this is working? – FLY May 23 '12 at 9:43
  • It'll be in the field_data_field_education and field_revision_field_education tables – Clive May 23 '12 at 9:45
  • hmm that table field_data_field_education doesn't exists field_data_education does exists but is empty – FLY May 23 '12 at 9:53
  • To be honest I've never seen a user added like the way yo're doing it before, normally you would build up a stdClass for the user with fields attached to it, like $user = drupal_anonymous_user(); $user->education[LANGUAGE_NONE][0]['tid'] = $tid; user_save($user); – Clive May 23 '12 at 9:56
  • thanks that did the trick!, well at least a user is added to field_data_education no 'education' value but I can work from here. – FLY May 23 '12 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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