Bonjour,
J’essaye de faire un module pour donner son avis sur un commentaire avec un système de «bien», «pas bien» et un bouton pour avertir l’administrateur que le contenu d’un commentaire n’est pas approprié. En ce qu’il nous concerne, je souhaite afficher un formulaire avec 3 boutons image contenant une valeur numérique (nombre de vote).
Pour afficher ce formulaire j’ai donc utilisé le hook_comment(), les conditions de filtrages d’affichage fonctionnent, ma fonction d’affichage est bien lu (test avec la fonction drupal_set_message), mais rien n’y fait, mon formulaire n’apparait pas et mes balises «div» contenu dans ma fonction d’affichage non plus.
Je ne sais donc pas si ma fonction hook est fautive ou alors si cela vient de mon formulaire ou encore de ma fonction d’affichage. Je vous colle ci-dessous le code de mon fichier .module
<?php
/<strong>
* Valid permissions for this module
* @return array An array of valid permissions for the vote comment module
*/
function vote_comment_perm() {
return array('access vote comment', 'administrer vote comment');
}
/</
strong>
* Implementation of hook_menu().
*/
function vote_comment_menu() {
$items = array();
$items['admin/settings/vote-comment'] = array(
'title' => 'Vote comment Module',
'description' => 'Page du module de Vote comment.',
'page callback' => 'drupal_get_form',
'page arguments' => array('vote_comment_settings_form'),
'access arguments' => array('administrer vote comment'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/settings/vote-comment/config'] = array(
'title' => 'Vote comment Configuration',
'description' => 'Page de configuration de Vote comment.',
'type' => MENU_DEFAULT_LOCAL_TASK,
'file' => 'vote_comment.admin.inc',
);
return
$items;
}
/**
* Regarde si le formulaire Vote comment peut etre affiche ou pas.
*/
function _vote_comment_show($comment) {
$vote_comment_show = FALSE;
$node_type_query = db_fetch_object(db_query("SELECT type FROM {node} n LEFT JOIN {comments} c ON c.nid = n.nid WHERE c.cid = %d", $comment->cid));
$node_type = in_array($node_type_query->type, variable_get('vote_comment_node_types', array()), TRUE);
if ($node_type) {
$vote_comment_show = TRUE;
}
return
$vote_comment_show;
}
function
vote_comment_comment(&$comment, $op) {
$vote_comment_show = _vote_comment_show($comment);
switch ($op) {
case 'view':
if($vote_comment_show && user_access('access vote comment')) {
drupal_set_message('je peux afficher le formulaire');
$comment->content['vote-com'] = array(
'#value' => show_vote_comment(),
'#weight' => 10,
);
}
else {
}
break;
}
}
function
show_vote_comment() {
drupal_set_message('je vais dans la fonction show');
$output = '<div class="vote_comment_div">';
$output .= 'test';
$output .= drupal_get_form('vote_comment_good_form');
$output .= drupal_get_form('vote_comment_bad_form');
$output .= drupal_get_form('vote_comment_alert_form');
$output .= '</div>';
drupal_set_message($output);
return $output;
}
//Les boutons de formulaires
function vote_comment_good_form($form_state) {
$count = db_result(db_query("SELECT COUNT(*) FROM {vote_comment} WHERE comment_id = %d AND vote = 1", $comment->cid));
$form = array();
$form['nid_good'] = array(
'#type' => 'hidden',
'#value' => $comment->nid,
);
$form['cid_good'] = array(
'#type' => 'hidden',
'#value' => $comment->cid,
);
$form['vote_good'] = array(
'#type' => 'image_button',
'#src' => $drupal_path . '/sites/all/modules/vote_comment/images/good.png',
'#submit' => 'vote_comment_good_form_submit',
'#default_value' => $count,
);
return
$form;
}
function
vote_comment_good_form_submit($form, &$form_state) {
$query = db_query("INSERT INTO {vote_comment} (node_id, comment_id, vote, timestamp) VALUES (%d, %d, 1, %d)", $form_state['values']['nid_good'], $form_state['values']['cid_good'], time());
drupal_set_message('Votre avis a bien été pris en compte');
}
function
vote_comment_bad_form($form_state) {
$count = db_result(db_query("SELECT COUNT(*) FROM {vote_comment} WHERE comment_id = %d AND vote = 0", $comment->cid));
$form = array();
$form['nid_bad'] = array(
'#type' => 'hidden',
'#value' => $comment->nid,
);
$form['cid_bad'] = array(
'#type' => 'hidden',
'#value' => $comment->cid,
);
$form['vote_bad'] = array(
'#type' => 'image_button',
'#src' => $drupal_path . '/sites/all/modules/vote_comment/images/bad.png',
'#submit' => 'vote_comment_bad_form_submit',
'#default_value' => $count,
);
return
$form;
}
function
vote_comment_bad_form_submit($form, &$form_state) {
$query = db_query("INSERT INTO {vote_comment} (node_id, comment_id, vote, timestamp) VALUES (%d, %d, 0, %d)", $form_state['values']['nid_bad'], $form_state['values']['cid_bad'], time());
drupal_set_message('Votre avis a bien été pris en compte');
}
function
vote_comment_alert_form($form_state) {
$form = array();
$form['nid_alert'] = array(
'#type' => 'hidden',
'#value' => $comment->nid,
);
$form['cid_alert'] = array(
'#type' => 'hidden',
'#value' => $comment->cid,
);
$form['vote_alert'] = array(
'#type' => 'image_button',
'#src' => $drupal_path . '/sites/all/modules/vote_comment/images/abus.png',
'#submit' => 'vote_comment_alert_form_submit',
'#value' => ' ',
);
return
$form;
}
function
vote_comment_alert_form_submit($form, &$form_state) {
$query = db_query("INSERT INTO {vote_comment} (node_id, comment_id, vote, timestamp) VALUES (%d, %d, 2, %d)", $form_state['values']['nid_alert'], $form_state['values']['cid_alert'], time());
$admin_mail = variable_get('vote_comment_admin_mail', array());
$object = "[ABUS-commentaire]";
$corps = "Bonjour, \n";
$corps .= "Le commentaire suivant a été signalé comme abusif, irrespectueux, insultant ... \n\n";
$corps .= l(t('Lien vers le commentaire.'),$base_url.'/node/'.$form_state['values']['nid_alert'].'#comment-'.$form_state['values']['cid_alert'] );
$corps .= "\n\nCordialement.";
$message['headers']['to'] = $admin_mail;
$message['subject'] = $object;
$message['body'] = $corps;
drupal_mail_send($message);
drupal_set_message('L\'administrateur a bien été averti');
}
?>Merci d’avance
- Vous devez vous identifier ou créer un compte pour écrire des commentaires

Bonjour,
Je ne comprend pas comment afficher mes formulaire entre le commentaire et les links. Pourtant dans mon drupal_set_message, j’ai bien mon formulaire (bien que la value ne soit pas visible)
Quelqu’un aurait un conseil à me donner ?
Merci d’avance
axelou
45
Pour les commentaires, c’est $comment->comment qui contient la valeur du corps du commentaire (contrairement aux nodes).
<?php
function vote_comment_comment(&$comment, $op) {
$vote_comment_show = _vote_comment_show($comment);
switch ($op) {
case 'view':
if($vote_comment_show && user_access('access vote comment')) {
drupal_set_message('je peux afficher le formulaire');
$comment->comment .= show_vote_comment();
}
break;
}
}
?>
Damien Tournoud
808
Merci beaucoup Damien
Je n’avais pas du tout compris cela en me référent à l’API de Drupal.
axelou
45