Drupal 7 - how do I create a custom registration form - drupal-7

I am new to Drupal development. I want to create a registration form for visitors of my site. Default registration page only has two fields: Username and Email address.
How can I add more fields to it, e.g password, picture and timezone. I also want to capture tow other information - Date of Birth and Gender. These two fields are not available in default users table. How can I tie these information with a user account? Do I have to create a new table and put these information there referencing the uid of user table? If it is possible how can I pull the joined record?
Is there any possibility that I create a new content type for this purpose but records still go to default users table and can be used for login?
If 2 above is not possible I probably have to use hook_form_alter but where should I put this function?
When creating a custom registration form shall I use default registration page i.e. /user/register and customize it?
I am sorry if above questions look very childish and silly! Hope you will consider my my newbie status. If possible please help me with a step by step solution.
Thanks!
UPDATE
To accomplish the requirement I created a custom module called user_signup and in user_signup.module file I have written the following code.
<?php
/*
Implements hook_menu()
*/
function user_signup_menu(){
$items = array();
$items['user/signup'] = array(
'title' => 'Sign Up',
'page callback' => 'drupal_get_form',
'page arguments' => array('user_signup_registration_page'),
'access arguments' => array('access content'),
);
return $items;
}
function user_signup_registration_page($form, &$form_state){
$form['name'] = array(
'#title' => 'Username',
'#description' => 'choose a username',
'#type' => 'textfield',
'#required' => TRUE,
);
$form['mail'] = array(
'#title' => 'Email',
'#description' => 'enter a valid email address',
'#type' => 'textfield',
'#required' => TRUE,
);
$form['pass'] = array(
'#title' => 'Password',
'#description' => 'Enter a strong password',
'#type' => 'password',
'#required' => TRUE
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Create Account'),
);
return $form;
}
function user_signup_registration_page_submit($form, &$form_state){
$new_user_id = db_insert('users')
->$fields(array( **//this is line number 45**
'name' => $form_state['values']['name'],
'mail' => $form_state['values']['mail'],
'pass' => $form_state['values']['pass'],
))
->execute();
drupal_set_message(t('New user created'));
}
Everything works perfectly but when I hit the submit button I am getting this error:
Fatal error: Method name must be a string in D:\xampp\htdocs\imdbind\sites\all\modules\user-signup\user_signup.module on line 45
I have marked line number 45 in above code snippet as **//this is line number 45**. I did not find any difference when comparing my code with theirs. What I am doing wrong?

Just change line 45 with following
From:
->$fields(array( **//this is line number 45**
To
->fields(array( **//this is line number 45**

You can add Fields here /admin/config/people/accounts/fields int eh People Account settings Manage Fields form.

Related

How to add placeholders in customized module?

I am new to drupal, currently i am creating a new module for forms. inside of the module i added the code as follows .
$form['first name'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
);
in this case i wanted to use place holders for text fields.
So how can i do ?
Give them in #attributes like below
'#attributes' =>array('placeholder' => t('placeholder text goes here'))

The requested URL /drupalhr/drupal/employee/add/ was not found on this server

I have created a custom module in drupal with entities. I have installed the entity api module. I have created my database schema with just two columns (employee_id, first_name) through the help of employee_management.install file (where as employee_management is my custom module name) and employee is my entity name.
I have also written the requisite functions employee_management.module but still it shows me the error , Whenever i tried to add a new entity in the admin/structure/employee it shows me the following error: "Not Found".
The requested URL drupal/employee/add/ was not found on this server.
function employee_management_entity_info() {
$employee_info['employee'] = array(
// A human readable label to identify our entity.
'label' => t('Employee Entity'),
// The controller for our Entity - extends the Drupal core controller.
'controller class' => 'EmployeeController',
// The table defined in hook_schema()
'base table' => 'employee',
// Returns the uri elements of an entity
'uri callback' => 'employee',
// Fieldable that we can attach fields to it - the core functionality will
// do the heavy lifting here.
'fieldable' => TRUE,
// The unique key of our base table.
'entity keys' => array(
'id' => 'employee_id',
),
// FALSE disables caching - caching functionality is handled by Drupal core
'static cache' => TRUE,
// Attach bundles - i.e. alternative configurations of fields associated with a main entity.
'bundles' => array(
'employee' => array(
'label' => 'Employee',
// Information below is used by the Field UI - they "attach" themselves here and lets us
// do the standard field management that all the core entities enjoy.
'admin' => array(
'path' => 'admin/structure/employee/add',
'access arguments' => array('administer employee entities'),
),
),
),
// View modes allow entities to be displayed differently based on context. We simply have one option
// here but an alternative would be to have a Full and Teaser mode akin to node.
'view modes' => array(
'full' => array(
'label' => t('Full'),
'custom settings' => FALSE,
),
)
);
return $employee_info;
}
EDIT
function employee_uri($employee) {
return array(
'path' => 'employee/' . $employee->employee_id,
);
}
And here is the complete list of function in the file employee_management.module
You don't automagically get the route and form to create your entity, you'll have to implement that yourself. See hook_menu and this guide.

CakePHP - authentication component, using a different table name and column name

Just for organization sake, I wanted to use a different table for the authentication component to check, but it doesn't quite work. While I can initially state:
$this->Auth->userModel = "CoreUsers" plus set the loginAction to my proper MVC
works to look at that table just to confirm it's there, but the login doesn't work, it only keeps returning an incorrect password. Something happens in the authentication component; I can't tell what makes it fail. When I rename my table to "Users", it works.
The other part is I'd prefer to actually use the column name of 'email' rather than 'username' since that's really what I'm using anyway.
I am just not having luck finding a complete tutorial and reference sets to do both these successfully with CakePHP 2.x. What is the way forward?
References:
Stack Overflow question How do I use a table other than "Users" for CakePHP's AuthComponent?
Stack Overflow question CakePHP - 'AuthComponent' with a different model name (not 'User')
(I had a look for answers, but I never quite got the whole answer.)
Make sure your database table "core_users" and model "CoreUser" exists.
When you setup component you can put login/logout redirect here.
var $components = array(
"Auth" => array(
'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'core_users', 'action' => 'login')
),
"Session");
Now in beforeFilter medhod you can put the following
function beforeFilter(){
$this->Auth->authenticate = array(
AuthComponent::ALL => array('userModel' => 'CoreUser', 'scope' => array("CoreUser.status" => 1), "fields" => array("username" => "email", "password" => "your_password_field"), 'Form', 'Basic'
);
}
Above example you can ignore status, if you need to pass any other info on the login verification u can use that. Now about you can remove 'Basic' if you only need form validation.
I hope this would work .
First, model names are generally singular. Are you sure you didn't mean CoreUser which would look in the core_users table?
Secondly, you can use the email field instead of username by setting the fields key on your auth setup.
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel' => 'CoreUser',
'fields' => array('username' => 'email')
)
)
)
);
See the book for more information.

How do I make certain menu items visible to certain roles?

In /drupal/admin/structure/menu/manage/main-menu, I have some links that I only want displayed for user's that have a specific permission.
How would I go about this?
In my module, I have
...
$items['resume/joblist'] = array(
'page callback' => 'ac_resume_job_list',
'access arguments' => array('view joblist'),
'title' => 'Job List',
'description' => 'Job List',
);
...
function ac_resume_permission()
{
return array("view joblist" => array("title" => "View Job List"));
}
When I go to "resume/joblist" under a user without the permission, I get the "Access Denied" as expected, however the link is still displayed.
hook_perm() was renamed to hook_permission() in Drupal 7, and there's a bit of a mismatch between view mylink that you define and view joblist that you declare as a permission.
You could change your code to look more like this:
function mymodule_menu() {
$items['mylink'] = array(
'page callback' => 'mymodule_mylink',
'access arguments' => array('view mylink'),
'title' => 'My Link',
'description' => 'My Link',
);
return $items;
}
function mymodule_permission() {
return array(
'view mylink' => array(
'Title' => 'View My Link'
)
);
}
After you clear Drupal's cache navigate to admin/people/permissions and assign your new permission to the role you want to be able to access the page you define in hook_menu().
Once you've done that users with that role will be able to access the page :)
I recommend using the following module: Menu Item Visibility, it does exactly what you need.

Validating made up fields in CakePHP?

Here's my scenario:
I'm creating a password change page.
the real field that holds the password is User.password
On the password create page, I used 3 made up fields:
$form->input('User.old_passwd');
$form->input('User.new_passwd');
$form->input('User.confirm_new_passwd');
How do I validate them with the rules:
old password must match User.password
new_passwd and confirmnew_passwd must
be equal
Are there better solutions for this? I'm open for suggestions. Thanks!
The built-in authentication component doesn't offer that functionality. I would specify the validation rules for your "made up" fields in the validate property of the model and write my own validation methods, for example: correctPassword() to ensure that the users enter their old password and matchingPasswords() to ensure that the new password was re-typed correctly.
var $validate = array(
// your existing validation rules
'old_passwd' => array(
'rule' => 'correctPassword',
'message' => 'invalid password'
),
'new_passwd' => array(
'rule' => array('minLength', 8),
'message' => '8 characters minimum';
),
'confirm_new_passwd' => array(
'rule' => 'matchingPasswords',
'message' => 'passwords do not match'
)
);
function correctPassword($check) { }
function matchingPasswords($check) { }
See the Cookbook for more information about custom validation rules.

Resources