I used the search functionality, but i didn't find an awnser.
Is want to print my contact form in a block?
Is there a code/snippet to do this?

Comments

gausarts’s picture

good idea. haven't tried it myself. but i guess webform module allows page view as well as block view, just give yourself a try. once you had block view enabled, go to block adminitration, enable it for your sidebars. or else use cck

love, light n laughter

gausarts.com

love, light n laughter

panos’s picture

probably this module will do the trick in a simpler manner: http://drupal.org/project/formblock

themegarden.org’s picture

Take look at next posts and pages on drupal:
- http://drupal.org/node/99778
- http://drupal.org/node/73973 (Quick contact block)

TheSmegHead’s picture

I also would like a contact form in a block. I tried the solution in the thread above and got the following error:

warning: call_user_func_array() [function.call-user-func-array]: First argumented is expected to be a valid callback, 'quickcontactblock_form' was given in /home/commerci/public_html/includes/form.inc on line 218.

Is this a Drupal 4.7 vs 5.1 issue? Anyone know how to fix it?

vm’s picture

The code for quickcontactblock was written for 4.7, thus it may need to be rewriitten for Drupal 5.x

alex_stanhope’s picture

If you're using the Contact module, you can wrap up the form inside a block without re-writing it.

<?php
print drupal_get_form('contact_mail_page');
?>

If you want to tweak form fields you have to indirect both the _page() call and the _submit() call.

<?php
function local_contactblock_page()
{
    $form = contact_mail_page();
    // override default value to select a different category
    $form['cid']['#default_value'] = 0; 
    return($form);
}

function local_contactblock_page_submit($form_id, $form_values)
{
    return(contact_mail_page_submit($form_id, $form_values));
}

print drupal_get_form('local_contactblock_page');
?>

Alex Stanhope
Lightenna

TheSmegHead’s picture

Alex - this is great, thanks. I would like to tweak the form fields though. In particular, I'd like to shorten the input lines (as I want the contact form in the sidebar) and I would like to have a couple of additional input fields.

I cannot find a thread giving any information about how to do any of those things. Could you expand a bit on what to do in addition to using your second code snipet above?

Drum O’s picture

Hello The Smeghead,

I was just trying to do what you want, and I discovered that you can change the properties of a field like you would an array in php. Alex's example above changes just the default category selected, but if you want to change the size of the fields, you can do it like this:

<?php
function local_contactblock_page()
{
    $form = contact_mail_page();
    // override default length
    $form['name']['#size'] = 25;
    $form['mail']['#size'] = 25;
    $form['subject']['#size'] = 25;
    $form['message']['#size'] = 25;
    $form['message']['#rows'] = 10;
    return($form);
}

function local_contactblock_page_submit($form_id, $form_values)
{
    return(contact_mail_page_submit($form_id, $form_values));
}

print drupal_get_form('local_contactblock_page');
?>

Cheers mi dears,
Drogle

dagomar’s picture

This will display (all) the form categories to every user, even anonymous. I want a block for anonymous users but not with every form category displayed; only those which they have access to.

Dagomar Paulides
B.A. Digital Media Design
Partner @ Online Agency

hurricane_rufo’s picture

I ran into the same problem and managed to fix it by examining line 463ff. in contact.module...

My code now reads:

function local_contactblock_page()
{
    $form = contact_mail_page();
    // override default length
    $form['name']['#size'] = 25;
    $form['mail']['#size'] = 25;
    $form['subject']['#size'] = 25;
    $form['message']['#size'] = 25;
    $form['message']['#rows'] = 3;
    $form['cid']['#type']='value';                /* Disable selection */
    $form['cid']['#default_value']=1;          /* ...and choose a default value */
    return($form);
}

function local_contactblock_page_submit($form_id, $form_values)
{
    return(contact_mail_page_submit($form_id, $form_values));
}

print drupal_get_form('local_contactblock_page');

hth,
Markus

yngens’s picture

This is apparently for 5.x, could you please advice on how to tweak field sizes in 6.x? Putting above code in 6.x has given an error.

giorgosk’s picture

pp’s picture

It is working, but I am affraid it's a little bit dirty.

if(!function_exists('contact_site_page')) {
  include(drupal_get_path('module','contact').'/contact.pages.inc');
}
print contact_site_page();
pp’s picture

if(!function_exists('contact_site_page')) {
  include_once(drupal_get_path('module','contact').'/contact.pages.inc');
}
print contact_site_page();
kevbot’s picture

As a note, if you want to use the CAPTCHA module along with this code you'll have to add an entry to your {captcha_points} table that looks like this:

INSERT into captcha_points (form_id) values('local_contactblock_page');

This will make the form function you've defined in that block available in the CAPTCHA admin interface. If you don't do this CAPTCHA won't know that it should be rewriting the form defined by that function.

wwwoliondorcom’s picture

Hi,

Thanks for the code, as this contact form in block works well, but can you tell me how to do if I don't want the MESSAGE box to be resizable ?

I want to set it to 5 rows with

$form['message']['#rows'] = 5;

but I don't want visitors to be able to change it, so how can I do ?

Thanks !

wwwoliondorcom’s picture

Hi again,

after using this code I just found that the MESSAGE box set to $form['message']['#rows'] = 5;

is SOMETIMES too large in FIREFOX and prints above the main content.

Any idea?

Thanks a lot.

dotidentity’s picture

I like to print the page title in the subject field of my contact form?
Can anybody help me?

1kenthomas’s picture

With Contact Forms installed, this solution redirects any page displaying the block to /contact (or whatever the default redirect for the contact forms module is). Proper place for a fix seems to be there so am filing a bug issue there.

bartezz’s picture

Thanx for sharing... was looking for a solution to this for a while!

Regards

________________
Live fast die young

or’s picture

Hi,

Can I embed the 'web form' module that way in a block?

IncrediblyKenzi’s picture

I just sent around a post about this elsewhere, but will repeat here.

The easiest way to do this is to embed the form in a block using php code. In drupal 5:

print drupal_get_form('webform-client-form-16');

(where '16' above is the ID of your webform, gotten from looking at the edit link url on admin/content/webform)

Alternatively, you can define it in a block hook:

function mymodule_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks[0]['info'] = t('Contact Form');
      return $blocks;
    case 'view':
      if ($delta == 0) {
        return drupal_get_form('webform-client-form-16');
      }
  }
}

For a consistent user experience, make sure you have the webform specify a landing page for the "thank you" message.

dotidentity’s picture

When i do this, i recieve this message:

warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'webform-client-form-40' was given in /Applications/MAMP/htdocs/Projecten/www.dreamcarsale.nl/includes/form.inc on line 218.

Using the code in contemplate

sumbawa’s picture

This might be of some help in calling drupal_get_form in Drupal 6. I'm also trying for a contact form in a block and have got something working. (By the way I'm new to Drupal so take my suggestions with a "grain of salt")

For webform_client_form_x, I believe that it's underscores not hyphens for that one. That should get rid of the first error. However you'll then need some additional arguments for drupal_get_form: two required arguments and two optional ones.

This code should work a bit better:

$node = node_load(12);
$submission = array();
$enabled = TRUE;
$preview = FALSE;

print drupal_get_form('webform_client_form_12', $node, $submission, $enabled, $preview);

where drupal_get_form requires the node for the form and a submission array. Then $enabled and $preview are the optional ones. However you'll likely need to pass $enabled = TRUE, as FALSE seems to be the default. I'm still working out how $preview works exactly.

Hope this gets you a bit further.

perfectmatch’s picture

Have searched for hours for the answer to this ...... thanks very much!

Pushkar Gaikwad’s picture

Unfortunately its not working, i am only seeing a blank block with no content. I am putting the current client form id in the code though :(

I am on 5.6 I think

bartezz’s picture

A long shot, but have you set the input format of the block to php?

Cheers

________________
Live fast die young

derekwebb1’s picture

In Drupal 6 you have to do the following:

<?php 
  require_once drupal_get_path('module', 'contact') .'/contact.pages.inc';
  print drupal_get_form('contact_mail_page');
?>

It's best to do error checking though see below: http://drupal.org/node/166432#comment-1319974

sobi3ch’s picture

OK so now I understand, in full study code case this should look like this (I'm working on D6);

  1. Make sure u enabled [contact] module
  2. Add default block, switch filter to php (enable [PHP filter] module if u are under D6) and add below code
<?php 
if(!function_exists('contact_site_page')) {
  include_once(drupal_get_path('module','contact').'/contact.pages.inc');
}
// (attention !) contact module must be enabled
print drupal_get_form('contact_mail_page');
?>

Taada that's all, works great.

rangi500’s picture

Thanks sobi3ch, that works great for me.

derekwebb1’s picture

Could you not do something like:

<?php
if(!function_exists('contact_site_page')) {
  include_once(drupal_get_path('module','contact').'/contact.pages.inc');
  print drupal_get_form('contact_mail_page');
}
?>
shuvradeep’s picture

doesn't work for me. the entire page forwards to /contact. what am i doing wrong?
i did exactly as above

giorgosk’s picture

you did exactly as above ? where ?
you replied to the original thread which has no instructions but a question !!!

There is plenty of things to try in this thread, check the next solution

------
GiorgosK
Web Development

q9c9p’s picture

I've tried too the code above and also to fix permission about the contact block in the permission settings but nothing, I can't see the form or any info in my block, nothing is rendered.

illepic’s picture

To add a bit to this regarding adding a contact form to a block in Drupal 7, I think the following method is frowned upon but works.

if(!function_exists('contact_site_page')) {
  require_once drupal_get_path('module', 'contact') .'/contact.pages.inc';
  print drupal_render(drupal_get_form('contact_site_form'));
}

For details on the change please see: http://drupal.org/node/224333#unrendered

Anonymous’s picture

MakeOnlineShop’s picture

Hi,

Do you know if it is better to use this module than code above ?

Any problem ?

Thanks.

vikom’s picture

I just came across this issue with Drupal 7. I usually use the Form block module for putting the contact form in a block. However there is no Drupal 7 release for that module yet. So I wrote a simple module to that puts the sitewide contact form in a block. If anyone needs it you can download it from my Github.

https://github.com/Vikom/contactblock

jackygrahamez’s picture

Viktor,

I used your code but when I send a message with it and I don't know how to debug this at the moment.

Warning: rawurlencode() expects parameter 1 to be string, object given in user_cookie_save() (line 3806 of /var/www/kndev/modules/user/user.module).
Warning: rawurlencode() expects parameter 1 to be string, array given in user_cookie_save() (line 3806 of /var/www/kndev/modules/user/user.module).

vikom’s picture

Hi

It's been a while since I wrote the module. But we're running it on my company website with the stable D7 release and it works fine. No error messages and e-mails are received without any problem. The module is very small and when I take a quick look at it now I don't really see how the module can be related to the error message you're getting. Can you send e-mails from the contact page at /contact?

jackygrahamez’s picture

Yes I can send from this contact form. Actually I think its a problem with the contact module. Because I disabled your block code and still have this issue
http://kndev.dyndns.biz/contact

tfranz’s picture

Nice litte module: seems to work well!
Thank you!!

mark@redhorseinteractive.com’s picture

Isn't this like a basic basic basic thing? Look at this mess? This should be like super straight forward.

vm’s picture

Different strokes for different folks over the years ... thread was started in 2007. There are multiple modules to deal with contact forms in blocks in 2012.