1

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?

1

The error is that you use die; the hook should simply return to the calling function. If you use die, you are avoiding that the code calling the validation handler is called back, which means the code that checks if there are any validation errors, and shows them to the users is never executed.

If you look at the code used by a Drupal validation handler, such as user_pass_validate(), you will notice it doesn't use die, which is never used from any form validation handler.

function user_pass_validate($form, &$form_state) {
  $name = trim($form_state['values']['name']);
  // Try to load by email.
  $users = user_load_multiple(array(), array('mail' => $name, 'status' => '1'));
  $account = reset($users);
  if (!$account) {
    // No success, try to load by name.
    $users = user_load_multiple(array(), array('name' => $name, 'status' => '1'));
    $account = reset($users);
  }
  if (isset($account->uid)) {
    form_set_value(array('#parents' => array('account')), $account, $form_state);
  }
  else {
    form_set_error('name', t('Sorry, %name is not recognized as a user name or an e-mail address.', array('%name' => $name)));
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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