【问题标题】:How to hide Fivestar rating field programatically after first comment?第一次评论后如何以编程方式隐藏五星级评级字段?
【发布时间】:2013-11-19 18:20:19
【问题描述】:

当用户在他/她已经评论过的节点上遇到时,我想在我的评论表单中隐藏 Fivestar 评级字段(称为“field_stars”)。我的网站上有一个名为 Commented 的标志,还有一个规则,当有人对其进行评论时代表评论员标记节点(效果很好)。我试图解决我的问题,根据我找到的信息here

function hiderating_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  $flag = flag_get_flag('commented') or die('no "commented" flag defined');
  if ($form_id == "comment_node_stuff_form") {
   if ($flag->is_flagged($form['#node']->uid, $user->uid)) { 
      unset($form['field_stars']);
    }
  }
}

不幸的是,它并没有改变任何东西(我也没有收到任何错误)。我做错了什么?


使用@jerdiggity 的代码进行调试后,我得到:

DEBUG: form id "comment_node_stuff_form" found. Debugging...
DEBUG: The code $flag->is_flagged($form["#node"]->uid, $user->uid) is returning FALSE.

(当然,我也收到了DEBUG: final form structure:,但粘贴在这里太长了)。我不明白为什么无法识别标记,因为它存储在数据库中...

【问题讨论】:

    标签: php drupal-7 drupal-comments drupal-fivestar


    【解决方案1】:

    尝试添加这些调试值以找到问题的根源(不是在实时服务器上,当然...仅在开发环境中):

    function hiderating_form_alter(&$form, &$form_state, $form_id) {
      global $user;
      if ($form_id == "comment_node_stuff_form") {
        // I'm not sure why the examples make use of "die" because that will stop the entire
        // site from continuing any further.
        $flag = flag_get_flag('commented');
    
        // ** Begin debug ** //
        // Notify us that we've at least got the right form.
        drupal_set_message(t('DEBUG: form id "comment_node_stuff_form" found. Debugging...'), 'warning');
        // Warn us if $flag is giving us issues
        if (!$flag) {
          drupal_set_message(t('DEBUG: No "commented" flag defined.'), 'warning');
        }
        // Check form components
        if (!isset($form['#node']) || !$form['#node']) {
          drupal_set_message(t('DEBUG: Something is wrong with or missing from $form["#node"]'), 'warning'); 
        }
        // Check more form components
        if (!isset($form['#node']->uid)) {
          drupal_set_message(t('DEBUG: $form["#node"]->uid is not set.'), 'warning'); 
        }
        // Again
        if (!isset($form['field_stars']) || empty($form['field_stars'])) {
          drupal_set_message(t('DEBUG: <em>$form["field_stars"]</em> is not set or is empty/false.'), 'warning'); 
        }
        // Check flag stuff
        if (!$flag->is_flagged($form['#node']->nid, $user->uid)) {
          drupal_set_message(t('DEBUG: The code <em>$flag->is_flagged($form["#node"]->nid, $user->uid)</em> is returning FALSE.'), 'warning'); 
        }
        // Again...
        if (!$flag->is_flagged($form['#node']->uid, $user->uid)) {
          drupal_set_message(t('DEBUG: The code <em>$flag->is_flagged($form["#node"]->uid, $user->uid)</em> is returning FALSE.'), 'warning'); 
        }
        // ** End debug ** //
    
        // Changing this to nid instead of uid
        if ($flag->is_flagged($form['#node']->nid, $user->uid)) { 
          unset($form['field_stars']);
        }
        // One more debug item:
        drupal_set_message('DEBUG: final form structure:<br/><pre>' . check_plain(print_r($form, 1)) . '</pre>', 'warning');
      }
    }
    

    【讨论】:

    • 感谢您的回复!我已经更新了我的问题。如果您需要特定的“最终表单结构”,请告诉我。
    • 我刚刚注意到的是,在您的解释中,您写道标志名称是 Commented,但您的代码将 commented 作为 flag_get_flag() 参数...您是否尝试过将第一个字母大写commented这个词?另外,$form['#node']-&gt;uid 是正确的还是应该是 $form['#node']-&gt;nid
    • 就是这样!应该是$form['#node']-&gt;nid - 当然!它现在终于可以使用了,所以如果您将其添加到您的答案中,我可以将其标记为答案。 (与此同时,我也会投票赞成。)非常感谢!
    • @Jeroen 不客气——很高兴它能正常工作。根据您的要求更新答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 2016-05-12
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    相关资源
    最近更新 更多