I have the block that is shown in every node of one content type. The block contains the link (the anchor tag) which leads to the page with contact form.

Is it possible to add subject automatically to the contact form depending on which page the user clicked on the link and came to contact form page? The subject would be the title of that page.

I use Webform module for the contact form.

1 Answer 1


I have created a custom module, but the form_alter can just as well be placed in the theme template file.

function MYMODULE_block_info() {
  $blocks['contact_at_webform_block'] = array(
    'info' => t('Contact Us'),

  return $blocks;

function MYMODULE_block_view($delta = ''){
  $block = array();

  switch ($delta) {
    case 'contact_at_webform_block':
      if (user_access('access content')) {
        $block['subject'] = t('Contact Us');
        $block['content'] = l('Contact Us', 'contact', array('query' => array('subject' => drupal_get_title())));
  return $block;

function MYMODULE_form_alter( &$form, &$form_state,$form_id ){
  //dpm($form); dpm($form_id);
  if ($form_id == 'webform_client_form_5') {
    $qp = drupal_get_query_parameters();
    $subject = isset($qp['subject']) ? $qp['subject'] : '';
    $form['submitted']['subject']['#default_value'] = $subject;

So the important part of the link in the block is to add the page title as a query parameter, so the href of the anchor will look like this contact?subject=PAGE_TITLE. If you use a block from views or created from the block UI then make sure to output the link in similar fashion.

The form alter code picks up the query parameter and inserts it into the webform subject text field. You will have to adapt the code here as your form id and text field will be different. Install devel module, then uncomment the dpm statement that is relevant. First dpm the value of your $form_id and replace mine (webform_client_form_5) with yours. Then dpm $form and inspect $form['submitted']. Look for your subject field.

  • I've never done anything like this before, can you point me to the right direction?
    – mixerowsky
    Commented Aug 7, 2014 at 16:59
  • Updated answer. Commented Aug 8, 2014 at 6:16
  • Works great...today I learned something new...thank you...
    – mixerowsky
    Commented Aug 8, 2014 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.