Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By robert13 on
Hello,
I'm creating my first drupal module.
My goal is to add one field to the user registration form and print it in the admin user list.
So i created one more field in the form like this :
<?php
function testmodule_form_alter(&$form, &$form_state,$form_id) {
if($form_id=='user_register_form'){
$form['test'] = array(
'#type' => 'textfield',
'#title' => t('test'),
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
);
}
}
?>
It works, the textfied appears. But when i complete it, is the data saved somewhere?
I'm unable to catch the data in the hook testmodule_form_user_admin_account_alter...
Have i forgotten to do something?
How to do it?
Thank you very much :)
Comments
You can do this without code
You can do this without code by visiting "Configuration" >> "People" >> "Account Settings" >> "Manage Fields" (tab) where you can add fields to the user object. When adding (or editing) fields you can specify they appear on the registration form. With this approach data will be managed by Drupal.
If you use the form alter approach you must also manage the data.
Unfortunately i cant, i must
Unfortunately i cant, i must be doing this by programming. But thanks however for your answer
Why must you do it by
Why must you do it by programming?
Because i'm training :)
Because i'm training :)
And i'm sure it's doable i just don't understand how the data is saved
I think part of learning
I think part of learning Drupal is understanding first what it can do without writing code.
Because i'm training :)
Which is fair enough, but as others have said, you should learn what you can do in Drupal without code before working with code. There are three reasons for this:
1) Your development will be a lot faster
2) The systems you are working with will already have been tested
3) The systems you are working with will generally be tied in with Drupal APIs, meaning that you will have compatibility in the future with things you may not realize you need compatibility with now.
Now as for how to programmatically save the data, there are two ways of saving data on Entities (and Users are an entity):
1) As a Field, using the Field API
2) As a property, using the Schema API and the Database API
As a Field:
Pros - ties in with Drupal's API and will make your data more dynamic, and able to be used with other parts of Drupal, including caching and Views and other things.
Cons - Requires a lot of code to save a single piece of data
Tutorial: http://clikfocus.com/blog/how-set-custom-field-type-using-drupal-7-field...
As a property
Pros - much simpler to code
Cons - will not tie in with other parts of Drupal easily without additional sometimes difficult coding
How to do it:
1) First decide whether you want to add your db column to an existing table (like the {users} table), or create a separate table to store your data.
- If adding to an existing table: Implement hook_schema_alter() to define your column as it will be on the existing table. Implement hook_install() to add your field to that table upon install, and if your module is already installed on any systems, implement hook_update_N() to add your field to that table upon the next time update.php is run.
- If adding to a new table: Implement hook_schema() to define your table. If your module is already installed on any systems, also implement hook_update_N() to add your table to the system the next time update.php is run.
If you create a new table, or add a field to a table other than the {users} table:
2) In hook_user_insert(), use the Database API to save your data to the DB table/field you have created.
3) In hook_user_update(), use the Database API to save your data to the DB table/field you have created.
4) In hook_user_delete(), use the Database API to delete your data from the DB table/field you have created.
If you add a field to the {users} table:
2) In hook_user_presave(), add the field to the $account object, using the column name on the table. For example, if you defined a field named 'privacy' on the {users} table, in hook_user_presave(), you would do:
The relevant data will then be saved to the privacy column in the {users} table.
Thak you very much for your
Thank you very much for your detailed answer. I've already done it graphically now i'll try to follow/understand your procedure.
BR - Robert
Learn on, Robert.
Learn on, Robert.
I recommend this book. And there's a chapter devoted to form api, which should help you.
http://www.apress.com/9781430228387
But also, to do what you intend, I think you're also going to have to get your field into the database (field api) and then populate it when the form is submitted.
_
Nothing will be done with that data. While you are altering the form, you need to add a form submission handler and then you can process the data in that handler. Like this:
http://tylerfrankenstein.com/user/4/code/attach-custom-submit-handler-dr...
- Brendan