简体   繁体   English

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

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

My comments on articles have a required Fivestar rating field called 'Stars' and I hid it with the following custom module (see: https://drupal.stackexchange.com/questions/90629/how-to-hide-rating-field-when-adding-comment-to-own-node ): 我对文章的评论中有一个必填的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']);
    }
  }
}

As an administrator, I've permission to edit comments from other users. 作为管理员,我有权编辑其他用户的评论。 Suppose that a user commented on his own article. 假设用户对自己的文章发表了评论。 That means he didn't have to set the 'Stars' field, due to the code above. 这意味着由于上面的代码,他不必设置“ Stars”字段。 But when I try to edit that comment, I do have to select a value for the 'Stars'. 但是,当我尝试编辑该注释时, 确实必须为“星标”选择一个值。

How can I prevent this? 我该如何预防? It's sufficient to check that the uid from the user who wrote the comment differs from the uid from the user who edits the comment. 足以检查来自写评论的用户的uid与来自编辑评论的用户的uid是否不同。 Finally, mark that the obligation to select stars when I leave a new comment myself must be preserved! 最后,标记必须保留我自己发表新评论时选择明星的义务!


Edit: I tried the following code: 编辑:我尝试以下代码:

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']);
    }
  }
}

Apparently, $form_state['values'] isn't well defined, because I get the following error: 显然, $form_state['values']定义不正确,因为出现以下错误:

"Notice: Undefined index: values in hiderating_form_alter()". “注意:未定义的索引:hiderating_form_alter()中的值”。

What's the correct code? 什么是正确的代码?

The code in the edit doesn't work, because $form_state['values'] isn't present before the submit. 编辑中的代码不起作用,因为在提交之前不存在$form_state['values'] This is the correct code: 这是正确的代码:

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']);
   }
  }
}

Using dpm($form) , I discovered that $form['uid']['#value'] returns the uid from the user who wrote the comment. 使用dpm($form) ,我发现$form['uid']['#value'] #value $form['uid']['#value']返回编写评论的用户的uid。 The value is only different from 0 if a comment is edited. 仅当编辑注释时,该值才不同于0。 When a user writes a new comment, the uid from the form is 0. That's why the AND in the second if is necessary. 当用户编写新注释时,表单中的uid为0。这就是为什么if需要的if ,第二个AND的原因。

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

相关问题 在第一个评论后如何以编程方式隐藏Fivestar评分字段? - How to hide Fivestar rating field programatically after first comment? 如何以编程方式隐藏评论中的字段? - How to hide a field in a comment programatically? 如何防止用户向其他用户发表评论 - How to prevent a user from leaving comment to another user 隐藏评论部分的用户评论电子邮件 - Hide User Review Email from Comment Section 如何隐藏一个用户从另一个用户在php中上传的图像? - how to hide an image uploaded by a user from another user in php? 如何使用Fivestar计算用户在其所有节点上的平均评分? - How to calculate the user's average rating on all his/her nodes with Fivestar? Drupal 7-当用户编辑节点时,如何向用户隐藏节点ID? - Drupal 7 - How do I hide the node id from the users when they are editing their nodes? 当用户从yii2的下拉列表中选择名称时,如何获取并在另一个表单字段中显示用户名? - how to get and display username in another form field when user select name from dropdownlist in yii2? 如何限制用户编辑URL参数 - How to restrict the user from editing an URL parameter SilverStripe 内联编辑/隐藏字段值 - SilverStripe inline editing / hide field value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM