Hi!
I need to add a contact form to a certain node types where registered and anonymous users can send a message to the node owner. This is actually a classified ad directory where node owner is a registered user who has placed an ad.
I haven't any coder skills but I've managed to get it working - sort of... Code below generates the contact form nicely and submitting works also ok. Code is placed to Display Suite code field, but I suppose it should work in block too.
The problem is, I have noticed that it interferes core search module. Everytime I try to make a search, it gives me white screen and later some errors. If I disable this code, everything works fine. Here's the error:
Notice: Trying to get property of non-object in eval() (line 6 of /var/aegir/platforms/drupal-7.12/modules/php/php.module(80) : eval()'d code).
Notice: Trying to get property of non-object in eval() (line 8 of /var/aegir/platforms/drupal-7.12/modules/php/php.module(80) : eval()'d code).
Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of /var/aegir/platforms/drupal-7.12/includes/entity.inc).
Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->cacheGet() (line 354 of /var/aegir/platforms/drupal-7.12/includes/entity.inc).
Notice: Trying to get property of non-object in designlasi_contact_personal_form() (line 27 of /var/aegir/platforms/drupal-7.12/modules/php/php.module(80) : eval()'d code).
Notice: Trying to get property of non-object in eval() (line 6 of /var/aegir/platforms/drupal-7.12/modules/php/php.module(80) : eval()'d code).
Notice: Trying to get property of non-object in eval() (line 8 of /var/aegir/platforms/drupal-7.12/modules/php/php.module(80) : eval()'d code).
Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of /var/aegir/platforms/drupal-7.12/includes/entity.inc).
Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->cacheGet() (line 354 of /var/aegir/platforms/drupal-7.12/includes/entity.inc).
I would appreciate help with this one :-)
Here's the code:
<?php
// Get node id
$node = node_load(arg(1));
// Only for certain content types
if (($node->type) == 'buyer' OR 'seller') {
$uid = $node->uid;
$recipient = user_load($uid);
// Load required include
module_load_include("inc", "contact", "contact.pages");
function design_contact_personal_form($form, &$form_state, $recipient) {
global $user;
$form = contact_personal_form($form, $form_state, $recipient);
$node = node_load(arg(1));
drupal_set_title($node->title);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Your name'),
'#maxlength' => 255,
'#default_value' => $user->uid ? format_username($user) : '',
'#required' => TRUE,
'#size' => 35,
);
$form['mail'] = array(
'#type' => 'textfield',
'#title' => t('Your e-mail address'),
'#maxlength' => 255,
'#default_value' => $user->uid ? $user->mail : '',
'#required' => TRUE,
'#size' => 35,
);
// Load title
$form['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#size' => 35,
'#default_value' => 'Your classified ad',
'#required' => TRUE,
);
$form['message'] = array(
'#type' => 'textarea',
'#title' => t('Message'),
'#required' => TRUE,
'#rows' => 4,
);
return($form);
}
function design_contact_personal_form_validate($form, $form_state) {
return(contact_personal_form_validate($form, $form_state));
}
function design_contact_personal_form_submit($form, $form_state) {
$form = contact_personal_form_submit($form, $form_state);
// No redirect
$form_state['redirect'] = drupal_get_destination();
return($form);
}
// Print form
return drupal_render(drupal_get_form('design_contact_personal_form', $recipient));
} // End
?>
Comments
_
One thing I see right off the bat... try changing
if (($node->type) == 'buyer' OR 'seller') {to
if (isset($node) && ($node->type == 'buyer' || $node->type =='seller')) {.Thanks, WorldFallz! One step
Thanks, WorldFallz! One step closer; now I get real results instead of white screen. But there's still one error:
Notice: Trying to get property of non-object in eval() (line 7 of /var/aegir/platforms/drupal-7.12/modules/php/php.module(80) : eval()'d code).
_
impossible to say without knowing what line 7 is (line 7 above is blank).
I found it now! I just needed
I found it now! I just needed to wrap first line like this:
No more errors :-D
Thanks for taking time with this!
The other members that have
The other members that have replied have bigger brains than me so perhaps they could confirm, but I was wondering if it's better to use menu_get_object instead of node_load
I think because the menu router already has the node object loaded.
They end up being the same
They end up being the same thing - menu_get_object() makes a call to node_load(). In the docs, it doesn't appear that either function caches the result.
Contact me to contract me for D7 -> D10/11 migrations.
Hi Jaypan, you are probably
Hi Jaypan, you are probably right.
Though, I always thought menu_get_object calls menu_get_item, and stores the router item in a static variable..
menu_get_item is called initially by menu_execute_active_handler() during the page request and is stored as a static variable, so if you call menu_get_object again it just gets the stored object from drupal_static?
You're right, menu_get_item
You're right, menu_get_item does store it in memory.
Contact me to contract me for D7 -> D10/11 migrations.
_
Actually, with d7, if you follow node_load it leads back to entity_load:
"The entities are stored in a static memory cache, and will not require database access if loaded again during the same page request."
So it's cached ;-)
I had to dig down to find
I had to dig down to find that, but it happens in DrupalDefaultEntityController::load
Edit: That all said, there are a lot of function calls to get down to this point, whereas calling menu_get_object() only calls two - menu_get_object() and menu_get_item() before it gets to the cache, so it may be a little better, though we are talking a couple of milliseconds at best.
Contact me to contract me for D7 -> D10/11 migrations.
Ooh, didn't know that. I did
Ooh, didn't know that. I did a test..
menu_get_object always takes around 2.00ms, whereas node_load always takes around 0.08ms. So I guess node_load is the way to go?
Try running those through
Try running those through loops around 10000 times each, and it will give you a more accurate reading - individual load times can vary.
Contact me to contract me for D7 -> D10/11 migrations.
Hope I've done this right..
Hope I've done this right..
For 10000 loops:
menu_get_object = 11.642124 ms
node_load = 0.057509 ms
for 5000 loops:
menu_get_object = 6.136584 ms
node_load = 0.05772 ms
I'm doing this on my home computer so probably not massively accurate, though I think there is enough there to convince me to use load_node in future.
That's a good test.
That's a good test. node_load() definitely looks to be the better result by a longshot.
Contact me to contract me for D7 -> D10/11 migrations.
_
seriously-- nice test! good to know too.
You also might want to look
You also might want to look at the Contact Form on Node module.
Yes, I tried that module, but
Yes, I tried that module, but it appears not to work for anonymous users :-(
where does this code go?
@torsti I want to do exactly the same thing so where do I place this php code please?