Sounds easy, but I couldn't set up a rule wich would compare number of node's comment to a value of field, let's call it 'goal'.

What I did was:

1. On 'admin/config/workflow/rules/reaction/add' I have created new rule called 'Make Sticky' and selected 'After saving a new comment' for 'React on event'.
2. On 'admin/config/workflow/rules/reaction/manage/rules_make_sticky' I clicked 'Add condition' and selected 'Data Comparison'.
3. On a newly opened page I selected 'comment:node:comment-count' for Data Selector and clicked Continue.
4. On the next page I selected 'is greater that' and arrived to the dead-end.

Because, for 'Value' I need value of a custom field_goal and there is no such option among data selectors. Maybe the value of field_goal can be pulled out by PHP code in PHP evaluation section provided on the same page, but maybe Rules simply don't allow comparing with content fields at all.

Anyway, I need someone more experienced to help me in setting up the necessary rule or, if the last is the case then, to write a small module which triggers only on comment add and which makes a node sticky if the number of comments is greater than a value set in field_goal. I have $30 for this little piece.

Anyone?

Comments

yngens’s picture

So I currently have the folling exported code for the rule:

{ "rules_make_sticky" : {
    "LABEL" : "Make Sticky",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "comment" ],
    "ON" : [ "comment_insert" ],
    "IF" : [
      { "data_is" : {
          "data" : [ "comment:node:comment-count" ],
          "op" : "\u003E",
          "value" : [ "comment:status" ]
        }
      }
    ],
    "DO" : [ { "node_make_sticky" : { "node" : [ "comment:node" ] } } ]
  }
}

I wonder is there any way to make Rules compare data with field values, something like:

{ "rules_make_sticky" : {
    "LABEL" : "Make Sticky",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "comment" ],
    "ON" : [ "comment_insert" ],
    "IF" : [
      { "data_is" : {
          "data" : [ "comment:node:comment-count" ],
          "op" : "\u003E",
          "value" : [ "comment:content:field_goal" ]
        }
      }
    ],
    "DO" : [ { "node_make_sticky" : { "node" : [ "comment:node" ] } } ]
  }
}
j_ten_man’s picture

Add a condition of 'Entity has Field' first and assert that the node has the field field_goal. From there you should have access to it in your data selectors (you'll have to make sure the field assertion comes before the comparison).

yngens’s picture

Thanks for the input. I have added field assertion, but unfortunately the field is still not available in data selectors.

{ "rules_sticky" : {
    "LABEL" : "Sticky",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "comment" ],
    "ON" : [ "comment_insert" ],
    "IF" : [
      { "entity_has_field" : { "entity" : [ "comment:node" ], "field" : "field_goal" } },
      { "data_is" : {
          "data" : [ "comment:node:comment-count" ],
          "op" : "\u003E",
          "value" : [ "comment:node:nid" ]
        }
      }
    ],
    "DO" : [ { "node_make_sticky" : { "node" : [ "comment:node" ] } } ]
  }
}
j_ten_man’s picture

What type of field is field_goal?

This works for me:

{ "rules_sticky" : {
    "LABEL" : "Sticky",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "comment" ],
    "ON" : [ "comment_insert" ],
    "IF" : [
      { "entity_has_field" : { "entity" : [ "comment:node" ], "field" : "field_goal" } },
      { "data_is" : {
          "data" : [ "comment:node:comment-count" ],
          "op" : "\u003E",
          "value" : [ "comment:node:field-goal" ]
        }
      }
    ],
    "DO" : [ { "node_make_sticky" : { "node" : [ "comment:node" ] } } ]
  }
}
yngens’s picture

Bounty is taken by a custom module developer, I've got my issue solved. Nevertheless would be nice if this could also be reached by Rules module.

sagar ramgade’s picture

Hi,

Here's the small module for this, make a folder called makesticky under sites/all/modules/ and put this two files inside the makesticky folder :

makesticky.module :

<?php
/**
 *Implements hook_comment_save()
 */
function makesticky_comment_insert($comment) {
  $node = node_load($comment->nid);
  
  if ($node->comment_count >= $node->field_goal[$node->language][0]['value']) {
    $node->sticky = 1;
    node_save($node);
  }
}

makesticky.info :

name = Make Sticky
description = "Makes node sticky when defined number of comments reached"
dependencies[] = comment
package = "Extras"
core = 7.x

Enable the module and check if it works for you.

Acquia certified Developer, Back end and Front specialist
Need help? Please use my contact form

yngens’s picture

Sagar,

Thanks for the input, but I'd like to cite from my last post above:

Bounty is taken by a custom module developer, I've got my issue solved.

. Sorry but I had already got the issue solved.

sagar ramgade’s picture

That's fine, thank you for the bounty anyways.

Acquia certified Developer, Back end and Front specialist
Need help? Please use my contact form

drupadawan’s picture

for your doxygen, Sagar : implements hook_comment_save, shouldn't that be hook_comment_insert?

sagar ramgade’s picture

Hi,

My bad, comment was wrong but used hook_comment_insert in the code.

Acquia certified Developer, Back end and Front specialist
Need help? Please use my contact form