I want to limit the users' review of my "Product" content type to only one, and I tried to implement a code from this link in Drupal 7 as a simple module named "review_limit." I saw this module in my module list and installed it without an error.
This is my code:
function review_limit_form_alter($form_id, &$form) {
// If the form is a "product_review" node form
if ($form_id == 'product_review_node_form') {
// Add an extra validation function //
$form['#validate'][] = 'review_limit_user_node_limit';
}
}
// Validation function
function review_limit_user_node_limit($node) {
// Get our logged in user's details
global $user;
$result = db_query("SELECT n.uid, n.nid
FROM {field_data_field_product_review_pr}
Inner Join {node} n ON field_data_field_product_review_pr.entity_id = :nid
Inner Join {users} ON n.uid = :uid", array(':nid' => $node->nid,':uid' => $user->uid))->fetchfield();
//if founded referenced field from the logged user, drupal show error message
if ($result) {
// Throw error
//drupal_set_message('You have already submitted a review of this item.', 'error');
form_error($element['title'], t('You have already submitted a review of this item.'));
die;
}
}
and i have prepare a template named product--review.tpl.php, page--node--add--product_review.tpl.php and call it from my template.php using
function bartik_theme() {
return array(
'product-review_node_form' => array(
'arguments' => array(
'form' => NULL,
),
'template' => 'product_review-node-form',
'render element' => 'form',
),
);
}
This module does not work, every user is still can to create a product review node as much as they want. Did I miss anything? Can anyone help me to solve this problem?