I'm trying to add custom fields to the user registration form. I need to save 2 of them into the Drupal User's database and 1 into my own separate database.
I'm using hook_form_alter to add the fields and then hook_form_submit to save the fields.
How do I submit the first/last name fields into drupal's user table and then save the SSN into my own? I have a function to switch between the 2 databases already but am just not sure how to use hook_submit_form to save some in one database and some in another?
function myModule_form_alter(&$form, &$form_state, $form_id){
if($form_id == 'user_registration_form'){
$form['f_name'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#required' => TRUE,
'#attributes' => array('class' => array('fname'))
);
$form['l_name'] = array(
'#type' => 'textfield',
'title' => t('Last Name'),
'#required' => TRUE,
'#attributes' => array('class' => array('lname'))
);
$form['ssn'] = array(
'#type' => 'textfield',
'#title' => t('Social Security Number')
'#maxlength' => 11,
'#required' => TRUE,
'#attributes' => array('class' => array('ssn'), 'placeholder' => '999-99-9999')
);
Would it be something like this?
function myModule_form_submit($form, &$form_state){
//Array of information to save into drupal user's table
$edit = array(
'f_name' => $form_state['values']['f_name'],
'l_name' => $form_state['values']['l_name'],
'second_email' => $form_state['values']['second_email'],
);
user_save(drupal_anonymous_user(), $edit);
drupal_set_message(t('The form has been submitted.'));
}
I'm not sure if I can use drupal's user_save() function and pass it this new information to save or if I have to do something in the .install file to add these new columns and then target them in the form_submit?
In what field will you save the custom field data? You should create a custom table for those fields and save data in there. When you load the user, you should also load those details from that table. There is no way you can directly load those custom field data when user object is loaded.
Related
I am trying to add a new field to an existing content type using form_alter, using below code I am able to have the field display on the form
function mymodule_form_alter(&$form, &$form_state, $form_id) {
$form['new_product_field'] = array(
'#type' => 'file',
'#attributes' => array(
'class' => array(
'field-type-file',
'field-widget-file-generic',
),
),
'#title' => t('New product field'),
'#description' => t('Description for this new product field'),
);
}
On an existing field, I can run below code and that updates the below 3 settings.
But how do I get those same settings set on the field that I am creating above though the form_alter?
$field_instance = field_info_instance('node', 'field_existing_field', 'resource');
$field_instance['widget']['settings']['filefield_sources']['filefield_sources']['upload'] = 0;
$field_instance['widget']['settings']['filefield_sources']['filefield_sources']['attach'] = "attach";
$field_instance['widget']['settings']['filefield_sources']['source_reference']['autocomplete'] = 0;
field_update_instance($field_instance);
I'm developing a Drupal 7's module. I defined a node type named 'Booth'. Now in my module, I created a form with some fields like name, phone, address and so on. One of these fields is Booth which is a Select type element. I wanna have the booths titles (that I added in "Add Content > Booth") as my Select Element options. How can I do that? How can I fill the options array, with title field of my booth content type? [Please look at the image below]
The first field must be filled with title of booth titles
$form['exbooth'] = array(
'#type' => 'select',
'#title' => t('Exhibition Booth'),
'#options' => array(), // I want to fill this array with title fields of booth content type
'#required' => TRUE,
);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#required' => TRUE,
);
$form['lastname'] = array(
'#type' => 'textfield',
'#title' => t('Last Name'),
'#required' => TRUE,
After some digging in drupal API, finally I found the solution.
I used entity_load() function to retrieve all nodes of 'booth' content type and then I put result's titles in an array and set that array for the Select options:
$entities = entity_load('node');
$booths = array();
foreach($entities as $entity) {
if($entity->type == 'booth') {
$i = 1;
$booths[$i] = $entity->title;
}
}
....
//inputs
$form['exbooth'] = array(
'#type' => 'select',
'#title' => t('Exhibition Booth'),
'#options' => $booths, // I set my array of booth nodes here
'#required' => TRUE,
);
I use drupal 7, and Entity API to develope a module. I have an entity to record client information. I wish to use image_field to let client upload their logo. So I have this function:
function silver_client_enable()
{
field_cache_clear();
field_associate_fields("silver_client");
if(field_info_field('logo'))
return;
$field = array(
'field_name' => 'logo',
'cadinality' => 1,
'type' => 'image',
);
field_create_field($field);
$instance = array(
'field_name' => 'logo',
'entity_type' => 'silver_client',
'bundle' => 'silver_client',
'label' => 'Logo',
'description' => 'Logo',
'display' => array(
'default' => array('label' => 'hidden')
),
'settings' => array(
'file_directory' => '/logo',
),
'widget' => array(
'type' => 'image_image',
),
);
field_create_instance($instance);
}
In the entity creation/edit form, I use :
field_attach_form('silver_client', $client, $form, $form_state);
to attch the field.
When I called up this form, the image upload field was corrected displayed. An i can use it to uplod file to serve.
In the form submit function, I save the entity as:
entity_save('silver_client', $client);
However, after I press the save button, the entity table is correctly saved. Field table is not. Both field_data_logo and field_revision_logo are empty.
I believer Entity API looks after the retrieving and saving of attached fields. Can someone tell me what is wrong with my code? Thank you.
You have to write the values back into your entity:
field_attach_submit('silver_client', $client, $form, $form_state);
entity_save('silver_client', $client);
http://api.drupal.org/api/drupal/modules!field!field.attach.inc/function/field_attach_submit/7
And you should validate the field values:
field_attach_validate('silver_client', $client, $form, $form_state);
http://api.drupal.org/api/drupal/modules!field!field.attach.inc/function/field_attach_validate/7
Furthermore if you don't want to declare your entity and fields by yourself you might checkout the EntityConstructionKit : http://drupal.org/project/eck which allows to export entity structures with Features just like Views.
I'm trying to add a couple of sections onto the user profile in Drupal 7 under:
<div class="profile" typeof="sioc:UserAccount" about="/drupal/user/1">
I'm adding three new sections, but the problem is that, although I am using the same way to add the three sections, only one of them is rendered as a child of the div above, while the other two are rendered as siblings. What am I doing wrong?
This is how I'm creating the content:
function plan_user_user_view($account) {
//Create the markup for the events region
$account->content['events'] = array(
'#type' => 'user_profile_item',
'#theme' => 'events',
'#events' => $events);
//Create the region for the venues
$account->content['venues'] = array(
'#type' => 'user_profile_item',
'#theme' =>'venues',
'#userid' => $user->uid,
'#venues' => $venues);
//Create the region for creating an event
$account->content['creator'] = array(
'#prefix' => '<div class="user-event-item" id="quick-event-creator">',
'#suffix' => '</div>',
'#type' => 'user_profile_item',
'#title' => t('QUICK EVENT CREATOR'),
'#markup' => drupal_render(drupal_get_form('event_creation')));
}
Also, is there a better way to create that last piece of content there? The other two seem fine in a template file but the last one since it's a form I was wondering if there are better ways of doing that.
Thanks,
Maybe you should have a look on this project profil2 which is the successor of content_profil for Drupal 6. With it you will be able to add informations onto users profils and if you want to code your custom fields by yourself it should be a good starting point to read.
Best.
How about this:
// Create the category.
$account->content['mymodule'] = array(
'#type' => 'user_profile_category',
'#title' => t('My module content'),
);
// Create first item (child).
$account->content['mymodule']['events'] = array(
'#type' => 'user_profile_item',
'#title' => t('Events'),
'#markup' => t('Whatever'),
);
// Create second item (child).
$account->content['mymodule']['venues'] = array(
'#type' => 'user_profile_item',
'#title' => t('Venues'),
'#markup' => t('Whatever'),
);
and so on. The bottom line is that user_profile_item items should be children of a user_profile_category item.
When I submit the form without filling in one of the required fields( or any combination of required fields) there is no status message presented to let me know I have missed the required fields.
The second time I submit the form the status message shows me what fields are required.
The status message seems to be one step behind the form submission.
If after the first submit I change what fields are filled in and I submit again then the status message that should have show the previous time will show now.
When the form is filled in correctly it submits as normal.
The form is displayed using drupal_get_form( 'otherWaysToRequest' );
This is called in a template file in the theme.
Does anyone know why the status message is one step behind?
This is a sample of the code being used
function otherWaysToRequest(&$form_state)
{
global $base_url;
$pathToTheme = path_to_theme();
$form['top-check'] = array(
'#type' => 'fieldset',
'#attributes' => array('class' => 'checkboxes'),
);
$form['top-check']['gift'] = array(
'#title' => t('Included a gift'),
'#type' => 'checkbox',
'#suffix' => '<br />',
'#required' => false,
);
$form['top-check']['contact'] = array(
'#title' => t('I would like to speak to you'),
'#type' => 'checkbox',
'#suffix' => '<br />',
'#required' => false,
);
$form['name'] = array(
'#title' => t('Name'),
'#type' => 'textfield',
'#required' => true,
);
$form['email'] = array(
'#title' => t('Email Address'),
'#type' => 'textfield',
'#required' => true,
);
$form['bottom-check'] = array(
'#type' => 'fieldset',
'#attributes' => array('class' => 'checkboxes'),
'#description' => t('<p class="Items">If you have ...:</p><p class="Items">I have included .....</p>')
);
$form['bottom-check']['share'] = array(
'#title' => t('A Share'),
'#type' => 'checkbox',
'#suffix' => '<br />',
'#required' => FALSE,
);
$form['submit'] = array(
'#type' => 'image_button',
'#src' => $pathToTheme.'/image.gif',
'#value' => t('Submit Form'),
);
}
function otherWaysToRequest_validate($form, &$form_state)
{
$mail_reg_ex = '/[-a-zA-Z0-9._]+[#]{1}[-a-zA-Z0-9.]+[.]{1}[a-zA-Z]{2,4}/';
if(!preg_match($mail_reg_ex, $form_state['values']['email']))
{
form_set_error('email', t('Invalid email address.'));
}
if( 0 == $form_state['values']['gift'] & 0 == $form_state['values']['contact'] )
{
form_set_error('gift', t('You must choose one of the first two options on the form'));
}
}
function otherWaysToRequest_submit($form, &$form_state)
{
//mail details
}
It's because by the time you're calling drupal_get_form in your template file the messages have already been committed for the current page; your validation messages will therefore show up the next time messages are displayed to the screen which is on the next page load.
You should build up the form in a custom module rather than the theme to get around this. The easiest way would be to create a block which you can assign to a region (using either hook_block in Drupal 6 or a combination of hook_block_info() and hook_block_view in Drupal 7).