繁体   English   中英

编辑其他用户的评论时如何隐藏Fivestar字段?

[英]How to hide Fivestar field when editing a comment from another user?

我对文章的评论中有一个必填的Fivestar评分字段,称为“ Stars”,我使用以下自定义模块将其隐藏(请参阅: https : //drupal.stackexchange.com/questions/90629/how-to-hide-rating-field-当添加评论到自己的节点 ):

function hiderating_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  if ($form_id == "comment_node_article_form") {
   if ($form['#node']->uid == $user->uid) { 
      unset($form['field_stars']);
    }
  }
}

作为管理员,我有权编辑其他用户的评论。 假设用户对自己的文章发表了评论。 这意味着由于上面的代码,他不必设置“ Stars”字段。 但是,当我尝试编辑该注释时, 确实必须为“星标”选择一个值。

我该如何预防? 足以检查来自写评论的用户的uid与来自编辑评论的用户的uid是否不同。 最后,标记必须保留我自己发表新评论时选择明星的义务!


编辑:我尝试以下代码:

function hiderating_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  $comment->uid = $form_state['values']['uid'];
  if ($form_id == "comment_node_article_form") {
   if ($comment->uid != $user->uid) { 
    unset($form['field_stars']);
    }
  }
}

显然, $form_state['values']定义不正确,因为出现以下错误:

“注意:未定义的索引:hiderating_form_alter()中的值”。

什么是正确的代码?

编辑中的代码不起作用,因为在提交之前不存在$form_state['values'] 这是正确的代码:

function hiderating_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  if ($form_id == "comment_node_article_form") {
   if ($form['uid']['#value'] != $user->uid AND $form['uid']['#value'] != 0) { 
    unset($form['field_stars']);
   }
  }
}

使用dpm($form) ,我发现$form['uid']['#value'] #value $form['uid']['#value']返回编写评论的用户的uid。 仅当编辑注释时,该值才不同于0。 当用户编写新注释时,表单中的uid为0。这就是为什么if需要的if ,第二个AND的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM