Making Contact form using other modules and showing it on the desired page or region in Drupal 7

Categories

Component ID

2033485

Component name

Making Contact form using other modules and showing it on the desired page or region in Drupal 7

Component type

module

Maintenance status

Development status

Component security advisory coverage

not-covered

Component created

Component changed

Component body

Contact form serves as a drop-in replacement for the contact module. Other users can fill the fields in the contact form and send it to the user.

Here we'll show you how to make a form using Private Message module. Follow the below steps and you'll be able to show your conatct form on the desired
pages or regions. But we need user id or profile id of a user in the url to achieve this, as without having one of these two ids we can't be able to
filter the form according to the user.

1) Install and configure the Private Message module for drupal 7.(http://drupal.org/project/privatemsg)

2) Now create a view of user type and display as a block.
-> Then add a relationship - User:Profile
-> add contextual filter - Profile:Profile id/User:uid
-> add fields - User:uid/Profile:uid and make it exclude from display.
-> add another field Global:php and write the below code in the Output Code

module_load_include('pages.inc','privatemsg');
$recipients = user_load(array('uid' => $row->uid));
$output .= drupal_render(drupal_get_form('privatemsg_new', $recipients));
print $output;

In the above code:

<------- module_load_include('pages.inc','privatemsg'); is used to include the private message module to show the form.
$recipients = user_load(array('uid' => $row->uid)); is used to get the user to whom other user want to contact
$output .= drupal_render(drupal_get_form('privatemsg_new', $recipients)); is used to get the private message form with a recipient mail id.------>

3) This will make a block of Contact form which you can show on any page whose url contains profile id or user id of a user.

Now for adding new fields to the form like email of the sender etc. Follow the below steps to achieve this.

1) Open privatemsg.module file of Private Message module and search the function _privatemsg_send($message) {} then add the below line where all
the field arguments are written------> $args['aemail'] = $message->mail;

2) Add a field aemail in the database table pm_message.

3) For adding token for the new field just add the info of the token in the function privatemsg_token_info() {} as ------>

$message['aemail'] = array(
'name' => t("Aemail"),
'description' => t("The body of the message."),
);

and add in function privatemsg_tokens($type, $tokens, array $data = array(), array $options = array()) {} ------>

case 'aemail':
$replacements[$original] = $message->mail;
break;

In the above code:

3) This will make a new field email of sender in the Contact form that we've just made using Private Message module

Now, we have completed the simple Contact form and adding extra fields to it.