1

I'm building a multistep register form in a custom module. I'm using profile2 module to save other custom fields. I want to let the users choose an image from their computers. This is the code that I use to upload the picture that saves in the file_managed Drupal table but without the user uid because at this point I don't create the user.

$form['empresa']['logo_empresa'] = array(
        '#title' => t('Logo de la empresa.'),
        '#type' => 'managed_file',
        '#description' => t('Tamaño máximo 3Mb y formato aceptado jpg,jpeg o png'),
        '#upload_location' => 'public://',
        '#upload_validators' => array(
        'file_validate_extensions' => array('png jpg jpeg'),
        'file_validate_size' => array(3*300*300),
        ),
);

When all fields are OK I submit the form and create the user as follows:

function empresas_registration_signup_form_submit($form, &$form_state) {


$nombre = $form_state['storage']['field_nombre_contacto'];
$apellidos = $form_state['storage']['field_apellidos_contacto'];
$mail = $form_state['storage']['email'];
$comunidad_autonoma = $form_state['storage']['field_comunidad_autonoma'];

$provincias = $form_state['storage']['field_provincia'];
$razon_social = $form_state['storage']['field_razon_social'];
$cif_nif = $form_state['storage']['field_cif_dni'];
$telefono = $form_state['storage']['field_telefono'];
$web = $form_state['storage']['field_web'];

$municipios = $form_state['storage']['field_municipio'];
$poblacion = $form_state['storage']['field_poblacion'];
$direccion = $form_state['storage']['field_direccion'];
$codigo_postal = $form_state['storage']['field_codigo_postal'];

$razon_social = $form_state['storage']['field_razon_social'];   
$clave = substr($razon_social, 0, 3);
$cif_nif = $form_state['storage']['field_cif_dni'];

$usuario = $clave.$cif_nif;
$password = password(8);
//$imagen = $form_state['values']['field_logo_empresa'];

$roles = user_roles();
$newuser = array(
        'name' => $usuario,
        'pass' => $password,
        'mail' => $mail,
        'status' => 1,
        'roles' => array('7' => 'empresa'),
        'access' => time(),

);
$existing_user = user_load($usuario);
if (!$existing_user->uid) {
    $account = user_save(null, $newuser);

    $profile = entity_create('profile2', array('type' => 'empresa'));
    $profile->uid = $account->uid;

    //Datos a guardar para el perfil

    $profile->field_nombre_contacto[LANGUAGE_NONE][0]['value'] = $nombre;
    $profile->field_apellidos_contacto[LANGUAGE_NONE][0]['value'] = $apellidos;
    $profile->field_razon_social[LANGUAGE_NONE][0]['value'] = $razon_social;
    $profile->field_cif_dni[LANGUAGE_NONE][0]['value'] = $cif_nif;
    $profile->field_direccion[LANGUAGE_NONE][0]['value'] = $direccion;
    $profile->field_codigo_postal[LANGUAGE_NONE][0]['value'] = $codigo_postal;
    $profile->field_telefono[LANGUAGE_NONE][0]['value'] = $telefono;
    $profile->field_web[LANGUAGE_NONE][0]['value'] = $web;
    $profile->field_poblacion[LANGUAGE_NONE][0]['value'] = $poblacion;
    $profile->field_comunidad_autonoma[LANGUAGE_NONE][0]['value'] = $comunidad_autonoma;
    $profile->field_provincia[LANGUAGE_NONE][0]['value'] = $provincias;
    $profile->field_municipio[LANGUAGE_NONE][0]['value'] = $municipios;

    $profile->save();

    $account->profiles['empresa'] = $profile;
    entity_save('user', $account);
    drupal_goto('user');

}
}

My question is: How could I set the fid from the table and assign it to the user?

  • Any reason not to use image field? – Mołot Nov 20 '13 at 9:42
  • You could create an image field in your entity type and attach it to your form with field_attach_form() or create a table and on form submission write fid to that table. – xurshid29 Nov 20 '13 at 10:41
  • @Mołot I've used $form_state['storage']['logo_empresa'] = ($form_state['values']['logo_empresa']); but nothing happens. Managed file saves the file in the table that's all. I'm quite new in Drupal so, How could I save the file in my form? – Luismin Nov 20 '13 at 11:57
1

@Luism, you should use field widget for it. field_attach_form attaches fields to form, field_attach_form_validate validates all attached fields, field_attach_submit prepared entity to be stored. Regarding values in $form_state['values'] you will have big structure inside $form_state['values']['FIELD_NAME'][LANGUAGE][INDEX] but you need only $form_state['values']['FIELD_NAME'][LANGUAGE][INDEX]['fid'], it will contain File ID from file_managed table (with status=0) for uploaded files and after profile2_save() file will be activated or deleted otherwise.

  • Thanks for the response. Could you please give me a tutorial link usage or something like that to see how put this in my custom form? – Luismin Nov 21 '13 at 9:47
  • Something like this code – m1r1k Nov 21 '13 at 10:11

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.