0

My content type 'Article' has an integer field called 'Ranking'. How can I check which nodes have the three highest values after saving a new node or comment? I'd like to use the result in a rule, so I'm looking for some kind of "The node has ranking 1/2/3" condition.

Any ideas how I can achieve this?

3

1 Answer 1

1

If within your Computed Field you've selected Store value in the database, you can easily use Views to sort results based upon this field.

So, for example, in broad strokes, you could create a View that filtered on whatever you needed, most probably published and node type; then for sort, you would select this field and select descending; in paging, you'd select a specific number, eg, 3; and in fields, you'd select whatever you wanted to display.

If you wanted to have a block of the top three nodes, in the fields I could see you selecting the title and so forth, but if you are wanting to see if a specific node was in the top three, I'd just put the node nid in there.

So, if that's the case, when this View is run, it will return three nids.

If you look into views_get_view_result() you can quite easily access that output, eg:

$result=views_get_view_result('top_three'); // or whatever the name of the view is

will populate $result with something along these lines:

Array
(
    [0] => stdClass Object
        (
            [nid] => 2340
            [node_created] => 1390349535
        )

    [1] => stdClass Object
        (
            [nid] => 1176
            [node_created] => 1390086303
        )

    [2] => stdClass Object
        (
            [nid] => 1133
            [node_created] => 1390086313
        )

)

So, now you just have to look in there to see if your current nid is in there and if so, act accordingly.

Now, of course, the other question is where to put all of this? I'd suggest looking into hook_node_update() but be aware of the performance considerations of doing all of this each time a node is updated. Caching is certainly your friend here.

2
  • Thanks, that sounds very promising! I created the top_three view you suggested: it displays the nids of the 3 articles with the highest value in the integer field 'Ranking'. Now, I'd like to create a new rule with an event like 'After saving new content' or 'A node has been flagged' that switches a 'Trophy' image field of the highest rated node (situated at the top in top_three) to some picture. I was wondering how I can get the condition right. Should I add an 'Execute custom PHP code' condition? What should the code look like? I'd create 2 similar rules for the 2nd & 3rd best rated content.
    – Jeroen
    Commented Jan 26, 2014 at 22:22
  • 1
    you could do this with custom php, eg, upon saving content, do whatever to get the node nid of the node that needs the gold badge, then load that node and set whatever you need in that node object and then save it; and etc on the silver and bronze.
    – Jimajamma
    Commented Jan 26, 2014 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.