1

This is the code I am using for Drupal 6:

/**
 * @Implement of hook_nodeapi()
 */
function mycaptcha_nodeapi(&$node, $op, $teaser, $page) {
  global $user;
  switch ($op) {
    case 'insert':
      if (isset($_SESSION['mycaptcha_ask_key'])) {
        unset($_SESSION['mycaptcha_ask_key']);
      }
    break;
    case 'validate':
      if ($node->mycaptcha_ask) {

        mycaptcha_ask_form_validate($node->mycaptcha_ask);
      }
    break;
  }
}

/**
 * @Implement of hook_comment()
 */
function mycaptcha_comment(&$comment, $op) {
  global $user;
  switch ($op) {
    case 'insert':
      if (isset($_SESSION['mycaptcha_ask_key'])) {
        unset($_SESSION['mycaptcha_ask_key']);
      }
    break;
    case 'validate':
      if ($comment['mycaptcha_ask']) {
        mycaptcha_ask_form_validate($comment['mycaptcha_ask']);
      }
    break;
  }
}

What are the equivalent hooks that I should implement for Drupal 7, to obtain code equivalent to the one I am actually using for Drupal 6?

from kiamlaluno's help. i changed the code to the following. but the validation still doesn't work. but the above code(hook_comment) works ok under drupal 6.

what's wrong with my code in drupal 7.

function mycaptcha_comment_insert($comment) {  
  global $user;
      if (isset($_SESSION['mycaptcha_ask_key'])) {
        unset($_SESSION['mycaptcha_ask_key']);
      }
   }

function mycaptcha_form_comment_form_alter(&$form, &$form_state, $form_id) {
    global $user;
  $form['#validate'][] = 'mycaptcha_comment_validate';
}

function mycaptcha_comment_validate($form, &$form_state) {
    global $user;
      if ($comment['mycaptcha_ask']) {
        mycaptcha_ask_form_validate($comment['mycaptcha_ask']);
      }

  }
0

2 Answers 2

6

I will reply to your question basing on the value for $op shown in your code snippet.

hook_nodeapi(), "insert"

The equivalent for Drupal 7 is hook_node_insert(), which receives just one parameter: $node.

hook_nodeapi(), "validate"

The equivalent for Drupal 7 is hook_node_validate(), which receives three parameters: $node, $form, &$form_state.
The implementations of hook_node_validate() don't usually use the last parameter, but they check the values of the properties for the $node object.

hook_comment(), "insert"

The equivalent of that hook, in Drupal 7, is hook_comment_insert(), which receives the comment object as unique parameter.

hook_comment(), "validate"

Unfortunately, in Drupal 7 there isn't hook_comment_validate(); you need to add a validation handler for the comment form.

function mymodule_form_comment_form_alter(&$form, &$form_state, $form_id) {
  $form['#validate'][] = 'mymodule_comment_validate';
}

function mymodule_comment_validate($form, &$form_state) {
  /**
   * Validate the comment.
   * $form_state['values']['name'] is the name of the user who wrote the comment;
   * $form_state['values']['is_anonymous'] is TRUE if the user is anonymous (not logged in);
   * $form_state['values']['mail'] is the email address of who wrote the comment;
   * $form_state['values']['homepage'] is the URL of the website of who wrote the comment;
   * $form_state['values']['subject'] is the title of the comment;
   * $form_state['values']['comment'] is the text of the comment;
   * $form_state['values']['language'] is the language associated with the comment;
   * $form_state['values']['uid'] is the user ID of who wrote the comment.
   * $form_state['comment'] is the comment object.
   */
}

Notice that mymodule_comment_validate() doesn't get any $comment parameter, and $form_state['comment'] is an object, not an array. You are using a $comment variable that is not defined, nor is one of the parameters.

The code I wrote works independently from the content type of the node where the comment appears. In Drupal 7, the hooks called to alter a form build from another module are hook_form_alter(), hook_form_BASE_FORM_ID_alter(), hook_form_FORM_ID_alter(). The base form ID for the comment form is "comment_form."
If you just need to alter the comment form for a specific content type (e.g. the "Article" content type), then you replace mymodule_form_comment_form_alter() with mymodule_form_comment_node_article_form_alter().

0
2

See the documentation for Converting 6.x modules to 7.x.

Specifically, hook_node_insert(), hook_node_validate(), hook_comment_insert(), and comment_form_validate() (see commment_validate() removed).

0

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.