Drupal 7 - Confirm email address - drupal

I have added some extra fields to the standard 'create account' page; notably a 'confirm email' field.
How do hook into the validation so that I can add some custom validation rules of my own (e.g. to check the two emails match)?
I have found hook_user_presave, but am unsure on how to code it or where I should put it.
Any and all help appreciated.

I would advise installing the LoginToboggan module, it actually has the option for that exact functionality out of the box and has a bunch of other useful options as well.
If you want to do it yourself though you'd probably be better off implementing hook_form_FORM_ID_alter() and adding a validation function directly to the registration form:
function mymodule_form_user_register_form_alter(&$form, &$form_state, $form_id) {
$form['#validate'][] = 'mymodule_user_register_form_validate';
}
function mymodule_user_register_form_validate(&$form, &$form_state) {
if ($form_state['values']['first_email'] != $form_state['values']['second_email']) {
form_set_error('second_email', 'The email addresses much match.');
}
}
Make sure you clear Drupal's cache once you've implemented the form alter function so Drupal registers it correctly.
Hope that helps.

Here is the example solution for Drupal 7:
/**
* Implements hook_menu().
* Note: You can define your own menu callback optionally.
*/
function foo_menu() {
$items['foo-signup'] = array(
'title' => 'Create new account',
'page callback' => 'drupal_get_form',
'page arguments' => array('user_register_form'),
'access callback' => 'user_register_access',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function foo_form_user_register_form_alter(&$form, &$form_state, $form_id) {
$form['account']['mail_confirm'] = array(
'#type' => 'textfield',
'#title' => t('Confirm e-mail address'),
'#maxlength' => EMAIL_MAX_LENGTH,
'#description' => t('Please confirm your e-mail address.'),
'#required' => TRUE,
);
$form['#validate'][] = 'foo_user_register_form_validate';
}
/**
* Implements validation callback.
*/
function foo_user_register_form_validate(&$form, &$form_state) {
if ($form_state['values']['mail'] != $form_state['values']['mail_confirm']) {
form_set_error('mail_confirm', 'The email addresses must match.');
}
}

Related

Drupal 6 how do page-customnamehere.tpl.php work

I'm working with a drupal 6.26 install. I can see file names like page-2011-custom-landing-page.tpl.php in the theme directory I'm using.
From what I understand, I should be able to see this template at http://www.mydomain.com/2011-custom-landing-page however I just get a 'page not found' message at that address. What's going on?
If you see a file name as 'page-2011-custom-landing-page.tpl.php' in you theme folder it means that there is a template file named 'page-2011-custom-landing-page.tpl.php' using for a page. That page may be defined in one of your custom module.
Like this:
<?php
/**
* Implements hook_menu().
*/
function custommodulename_menu() {
$items['pathname'] = array(
'title' => 'title',
'page callback' => 'custommodulename_pagename',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_theme().
* Adds our theme specificiations to the Theme Registry.
*/
function custommodulename_theme($existing, $type, $theme, $path) {
$items = array();
$items['custommodulename_pagename_page'] = array(
'render element' => 'form',
'template' => 'page-2011-custom-landing-page', //name of file(template) to be created,here create page-2011-custom-landing-page.tpl.php in the custom module folder
);
return $items;
}
/**
* Callback function(menu)
*/
function custommodulename_pagename(){
return theme('custommodulename_pagename_page');
}
?>
page-2011-custom-landing-page is not a url, it is a template name. You can see the content inside the template my accessing the menu callback that using that template. (here it is : http://yoursite.com/pathname)
Reference : http://www.developerdoc.com/answer/add-template-menu-call-back

Using Drupal's node form in a new page

In a custom module I want to have a page defined in hook_menu, that shows the add form for a specific content type, with some modifications to the form.
So far it's working, and even saving the new node, but only with the default values I'm setting in the code, i.e. it's not picking up anything the user types into the form. I checked and $form_state['input'] contains the inputted values, but $form_state['values'] doesn't, so the new node gets saved wrong.
Here's the relevant code:
function mymodule_menu() {
return array(
'admin/content/myadd/%' => array(
'title' => 'my custom add page',
'page callback' => 'mymodule_node_add',
'page arguments' => array(3),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
),
);
}
function mymodule_node_add() {
module_load_include('inc', 'node', 'node.pages');
//I'm doing a print here instead of returning because I'm calling this page
//in an AJAX popup, so I don't want the whole page to output, only the form.
print render(drupal_get_form('mymodule_node_add_form'));
}
function mymodule_node_add_form($form, &$form_state) {
if (!isset($form_state['node']) {
global $user;
$node = (object) array(
'uid' => $user->uid,
'type' => 'mycontenttype',
'language' => LANGUAGE_NONE,
);
//this is setting a default value
$node->myfield = array(LANGUAGE_NONE => array(array('value' => arg(3))));
$form_state['build_info']['args'] = array($node);
$form = drupal_build_form('mycontenttype_node_form', $form_state);
$form['actions']['submit']['#submit'][0] = 'mymodule_node_add_form_submit';
//there's a lot more customization of the form here, like adding fields, etc.
}
return $form;
}
function mymodule_node_add_form_submit($form, &$form_state) {
//here's where $form_state['input'] is correct but $form_state['values'] isn't.
$node = node_form_submit_build_node($form, $form_state);
node_save($node);
$form_state['values']['nid'] = $node->nid;
$form_state['nid'] = $node->nid;
$form_state['redirect'] = 'some/other/page';
}
So, am I doing something wrong here? Should I be concerned about form ids being wrong? (my form's id is mymodule_node_add_form, but the actual form might output mycontenttype_node_form), would this affect me?
You want hook_form_alter() (see api.drupal.org). I would try to use the existing content type's form and simply alter it with hook_form_alter(). I would also try to first get it working as a standard, non-AJAX page, so you can get all the advantages of dpm() and other debugging techniques. When you have it down solid, then modify it to take advantage of the AJAX techniques.
mymodule_form_alter(&$form, &$form_state, $form_id) {
// use this with your devel module turned on to verify
// your $form_id and contents of all forms that load on a given page
dpm($form);
// once you verify your $form_id, you can begin accessing your form and altering it
switch( $form_id ) {
case 'my_target_form_id' :
// this part is just pseudocode, I haven't memorized the $form structure,
// you can get it from your dpm().
if( $form['node']->type == 'my_target_content_type' ) {
$form['actions']['submit']['#submit'][0] = 'mymodule_node_add_form_submit';
}
break;
}
}

Replace the node-edit menu in Drupal

How can I change (unset or add) a new button in my edit-node menu? In this case, I would like to diable the 'Settings'-menu and add a new menu... I looked in the $form and the $form_state, but no luck there. At least, that's what I think...
EDIT
Module name: publication
Install: publication.install
File: publication.module
function publication_menu_alter(&$items) {
unset($items['node/%node/edit']);
}
EDIT 2
function publication_menu() {
$items['node/add/fiche'] = array(
'title' => 'New linked fiche',
'type' => MENU_LOCAL_TASK
);
return $items;
}
EDIT 3
What I'm trying to do is to allow my users to add some more content to existing content. So they are not allowed to edit the current content, only to add some details. So I thought, I delete the edit-button and replace it with an add-button and the add-button links to a page where he can create more content. That's it :)
You should use hook_menu_alter to unset menu.
function publication_menu_alter(&$items) {
// print_r($items);
// Find path you want to unset then unset it.
// Should be something like:
unset($items['your/menu/path']);
}
And hook_menu for defining new one. In your case I believe it should be menu type MENU_LOCAL_TASK since you want to add a new tab. Isn't it?
function publication_menu() {
$items['node/%node/something_else'] = array(
'title' => 'My title',
'page callback' => 'mymodule_abc_view',
'page arguments' => array(1),
'access arguments' => array('access content'),
'type' => MENU_LOCAL_TASK
);
return $items;
}
function mymodule_abc_view($nid = NULL) {
return 'This node ID is '. $nid;
}

Ask twice for email in the Ubercart checkout page

Is there a Ubercart module to ask the user to insert his email twice in the checkout page?
There is an email confirmation checkbox in ubercart checkout settings. No additional modules needed.
I doubt there is a module for this. You can do this with hook_form_alter in a custom module. Should only be 10-20 lines of code.
Something like
function module_form_FORM_ID_alter(&$form, &$form_state) {
$form['...']['second_mail'] = array(
'#title' => t('Verify E-mail'),
'#type' => 'textfield',
'#weight' => xx,
);
$form['#validate'][] = 'module_validate_function_name';
}
function module_validate_function_name(&$form, &$form_state) {
if ($form_state['values']['mail'] != $form_state['values']['second_mail']) {
form_set_error('second_mail', t('You have mistyped your e-mail, please verify');
}
}
The above is example code, but might actually work, it depends how the ubercart checkout form is created, more specifically, the name of it's mail field.
There are a few blanks but it should be easy enough to fill out.
I got it working by using this:
/* Code to add confirm email for uc checkout */
function custom_code_form_alter(&$form, $form_state, $form_id) {
if($form_id == "uc_cart_checkout_form" && $form['panes']['customer']['primary_email']['#type'] != 'hidden'){
$form['panes']['customer']['primary_email']['#weight'] = '0';
$form['panes']['customer']['new_account']['#weight'] = '2';
$form['panes']['customer']['confirm_email'] = array(
'#title' => t('Verify E-mail address'),
'#type' => 'textfield',
'#size' => '32',
'#required' => true,
'#weight' => '1'
);
$form['#validate'][] = 'custom_code_validate_confirm_email';
}
}
function custom_code_validate_confirm_email(&$form, &$form_state){
if($form_state['values']['panes']['customer']['primary_email'] != $form_state['values']['panes']['customer']['confirm_email']) {
form_set_error('panes[customer][confirm_email', t('Email addresses must match.'));
}
}
/* end code for confirm_email */

How to develop custom forms for Drupal's admin users?

which will be the best way to develop custom forms for drupal, for admin's part of the system?
thank you in advance!
First thing, you need a location to access your form from, preferably in the "admin/*" namespace if the form is only meant for administration.
If you're just showing a form, you could directly use drupal_get_form as page callback (but you could use any function to generate the HTML code, even mix with theme functions)
Also, you need to know which permission(s) is required to access the form.
By default, I used "access administration pages" but you probably should use something more specific, depending on what you intend the form for.
Let's say the path is "admin/build/something", you need to use hook_menu to register that path:
/**
* Implementation of hook_menu().
*/
function modulename_menu(){
return array(
'admin/build/something' => array(
'title' => 'Example Admin Form',
'description' => 'Admin form introduced by the MODULENAME module',
'type' => MENU_NORMAL_ITEM,
'page callback' => 'drupal_get_form',
'access arguments' => array('access administration pages'),
),
);
}
Now, to actually display a page: the value provided in "page arguments" was the name
of the function that drupal_get_form expects to provide the form structure
(which must be an associative array):
/**
* Form Structure
*/
function modulename_form_something(&$form_state){
$form = array();
$form['myfield'] = array(
'#title' => 'My Field',
'#description' => 'This is a basic text input field',
'#type' => 'textfield',
'#default_value' => $form_state['values']['myfield'],
);
//
// Here you can add more elements in the form
//
return $form;
}
Here is more informations about the Forms API, which you can use to make some pretty complex forms easily.
Now your form is displayed at "/admin/build/something", but you probably want to do soemthing with these data as well; by default, the validate and submit functions are named the same as the form structure function, with "_validate" and "_submit" respectively (however you can override this with #validate and #submit in the form structure).
For example, let's say the string "no" is not a valid value, everything else is accepted.
/**
* Form validation
*/
function modulename_form_something_validate($form, &$form_state){
if ($form_state['values']['myfield'] == 'no'){
form_set_error('myfield', '"<b>no</b>" is not a valid answer, try again.');
}
}
The validation is called first, however you should only check if data are alright in that function. If you need to perform actions when the form is received, do it in the "submit" handler instead because validate may be called several times while submit is called only once.
/**
* Form submission
*/
function modulename_form_something_submit(&$form, &$form_state){
//
// Here you can perform whatever action that form is made for.
//
drupal_set_message( 'The form has been sent. "myfield" has the following value: '.$form_state['values']['myfield'] );
}
Let's summarize, here's the whole modulename.module file:
<?php
/**
* Implementation of hook_menu().
*/
function modulename_menu(){
return array(
'admin/build/something' => array(
'title' => 'Example Admin Form',
'description' => 'Admin form introduced by the MODULENAME module',
'type' => MENU_NORMAL_ITEM,
'page callback' => 'drupal_get_form',
'page arguments' => 'modulename_form_something',
'access arguments' => array('access administration pages'),
),
);
}
/**
* Form Structure
*/
function modulename_form_something(&$form_state){
$form = array();
$form['myfield'] = array(
'#title' => 'My Field',
'#description' => 'This is a basic text input field',
'#type' => 'textfield',
'#default_value' => $form_state['values']['myfield'],
);
//
// Here you can add more elements in the form
//
return $form;
}
/**
* Form validation
*/
function modulename_form_something_validate($form, &$form_state){
if ($form_state['values']['myfield'] == 'no'){
form_set_error('myfield', '"<b>no</b>" is not a valid answer, try again.');
}
}
/**
* Form submission
*/
function modulename_form_something_submit(&$form, &$form_state){
//
// Here you can perform whatever action that form is made for.
//
drupal_set_message( 'The form has been sent. "myfield" has the following value: '.$form_state['values']['myfield'] );
}
Don't forget you also need a .info file for being able to install the module:
Source of modulename.info:
; $Id
name = Admin Form
description = This module adds an admin form
package = Example Module
core = "6.x"
version = "6.x-0.1-dev"
The Drupal form api system will help you make any form you need. If you need to store settings, system_settings_form is a nice shortcut.
The only difference when making admin forms, is to remember to set some sort of permission required, and to place the form somewhere in the /admin/ section of the site. There really isn't anything special about admin forms.
Unless I'm misunderstanding your question, I think you can avoid the hassle of Forms API by using the Webform module.
No code required, nice UI and built in statistics tools.
http://drupal.org/project/webform
Watch a couple tutorial videos and you'll be making just about any form in no time.

Resources