[英]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.