My site with existing content needs a field that shows the number of nodes of a certain content type that have been created by the current user.

I can't use Views because the field needs to be able to be used in a Rules condition.

Nor can I use Rules Actions and Flag at the time of adding nodes because so many nodes are pre-existing.

Nor can I use Display Suite code fields because I don't think they are able to be used in Rules either.

Is a computed field and a custom query the best option?

I have so far failed to find a code snippet and I am not experienced in PHP.

  • Adrian Cid Almaguer's answer sounds perfect. But if it's a very simple rule condition where a user must have created X amount of nodes, then this is easy with VBO. It has a "Check number of results returned by a VBO view" condition. That just means you wouldn't need a field at all. – Niall Murphy Jul 15 '16 at 14:58
  • @NiallMurphy your point is interesting, can you create a answer with all the steps to accomplish this? – Adrian Cid Almaguer Jul 15 '16 at 15:18
  • 1
    Added it below. It has saved me a few times! – Niall Murphy Jul 15 '16 at 15:59

A method of using VBO+Rules to have a minimum number of results rule condition. It's a commonly used module and this can be really useful for a lot of things.

  1. Create a standard view and add the author: uid as a contextual filter. The default value must be Raw: Path Component 2.
  2. Add the VBO field, Bulk Operations.

Step 1

Next, we add the condition to the rule.

  1. Add Check number of results returned by a VBO view condition.
  2. Select the VBO view we just created. Master is fine.. It just needs the Bulk Operations field to show up here.
  3. Pass the current user's uid as an argument to the view. This gets sent as path component 2 which is why we had to choose it in the view.
  4. Set your minimum number of nodes / users / comments etc. that your view is counting.

Step 2

And done.

  • I will test this later, seems interesting – Adrian Cid Almaguer Jul 15 '16 at 16:20
  • This works and the Views+VBO+Rules instructions were very helpful. – JohnD Jul 19 '16 at 9:49
  • What's the purpose of the "250" (for "minimum number of results") in this answer? Nowhere in the question (or comments) I find anything that talks about that "250". PS: can't upvote this answer "anymore" ... – Pierre.Vriens Aug 17 '16 at 10:35
  • In the comments, he showed interest in this method but didn't give a number. I used 250 as my own test as the number of vbo results was 246 or something. Point 4 in this answer instructs him to add his own minimum number. – Niall Murphy Aug 17 '16 at 10:46

You can use the Computed Field module but you need to consider this:

When is the field actually computed?

If you wish to store the values in the database then you have to update or re-submit the node to compute the value of the field.

If it is not stored in the database then the value computes when the node loads and only when the node is loaded. The field will not work in views nor will it function properly if it depends on other non-stored computed fields.

This is the code of your field:

global $user;
$type = 'article';

$query = new EntityFieldQuery;
$result = $query
->entityCondition('entity_type', 'node')
->propertyCondition('status', 1)
->propertyCondition('type', $type)
->propertyCondition('uid', $user->uid)

$cant = count($result['node']);
$entity_field[0]['value'] = $cant;

Another way that come to my mind is have a variable to store this value, and you will edit this var each time a new node is created (hook_node_insert) or deleted (hook_node_delete), so in this case you will have always the real value.


Computed Field

  • Could one use your code snippet in a computed field and then use Computed Field Tools to keep the count up to date? – JohnD Jul 19 '16 at 9:59
  • @JohnD I think that with this module all will be ok, but is better if you make some test and write here your results. It's the first time that I read about the Computed Field Tools module. – Adrian Cid Almaguer Jul 19 '16 at 19:45
  • a very good answer, using php – innocent rock May 14 '19 at 22:11

You could try using the Token Embed Views module, and then use this token on that field. I have not tried this method, but you could give it a try.

So, I suggest to create a view that does this counting, and embed this view into your field, using the module above.

  • Interesting idea, which I may come back to in the future. The problem is that it needs a text field - tokens won't work in an integer field because they can only contain numbers. – JohnD Jul 19 '16 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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