How to remove a comment based on it's field value? I have a multilingual site, Comments in this site include a language field (field_language), so users who send comment to nodes must specify in what language they are sending the comment. On loading a node I want to display only the comments which their field_language is equal to the current website language. I tried this code based on the following result



function THEME_preprocess_node(&amp;$variables) {
    global $language;
    foreach ($variables['content']['comments']['comments'] as $comment)
        if($comment['field_language']['#items'][0]['value'] != $language-&gt;language)
            unset($comment);
}


The $comment['field_language']['#items'][0]['value'] prints the comment field_language so I'm sure this is a correct syntax and the condition holds for a few comments but unset($comment); doesn't unset the comment.

I tried the following code

unset($variables['content']['comments']['comments'][71]);


and it successfully worked and removed the comment. The ID inside the [] is stored in the $comment['#entity_view_mode']['id'], I can easily use this ID to remove the comment but I think there must a more straightforward solution.

Is there any other solution like using hook_comment_load or anything else?
This code should work for you

function THEME_preprocess_node(&amp;$variables) {
  global $language;
  foreach ($variables['content']['comments']['comments'] as $key =&gt; $comment) {
    if ($comment['field_language']['#items'][0]['value'] != $language-&gt;language) {
      unset($variables['content']['comments']['comments'][$key]);
    }
  }     
}