4

I created a Taxonomy with a "Limit" field, so that I can add term within "limit" number. Here is a screenprint of it:

enter image description here

My question: How can I limit the number of nodes that are using each term, so that the 'count of nodes' (= number of nodes that are using some term) can't be above this "Limit" field?

UPDATE: I try @Pierre.Vriens Answer and i think still not working..

The term + Limit field

Step 01: VBO

Step 2: Components

Step 3: Rule

3

You can make this work by using Rules in combination with the Views Bulk Operations module, as further detailed in the "blueprint" below.

Step 1

Use the Views and Views Bulk Operations module to create a view of all nodes that have a specific taxonomy term, in which you add a VBO field to it. Simply include a field like nid for each node (no need for any other fields). Just make sure you get exactly 1 row for each node you want to take into consideration.

Step 2

Create a Rules Component like so:

  • Using these Rules Component parameters:

    • a parameter of data type that matches with the data type of your 'Taxonomy with "limit" field' (probably "integer"), let's assume the parameter has machine name node_limit.
    • a parameter that matches with the term reference field as defined in your node's content type, let's assume the parameter has machine name node_termreference (which can be either a single value, or it can have multiple values, depending on how you defined it in your content type).
  • With a Rules Condition "Check number of results returned by a VBO view", whereas:

    • you select the VBO view created in Step 1.
    • you want to check if that number of results is greater or equal to node_limit (which is the 1st parameter used by this Rules Component).
  • Rules Actions:

    • Set an appropriate Drupal message (that the node limit for that term has been reached already).
    • Remove the term that was entered from the node_termreference field (which is the 2nd parameter used by this Rules Component); Which Rules Action to use for doing so depends on how that field is defined, e.g if it is a multi value field, then you should use "remove from list".

Step 3

Create a custom Rule like so:

  • Rules Events:
    • Before saving a new node (optionally limited to relevant content types).
    • Before updating an existing node (optionally limited to relevant content types).
  • Rules Conditions:
    • Entity has field, for the field that refers to your taxonomy term (to make this field available for further processing in your subsequent Rules conditions/actions). More specifically we'll need it (as a token) to specify a value for the node_termreference parameter to execute the Rules Component from Step 2 later on.
  • Rules Actions:
    • Fetch entity by ID, whereas you fetch the taxonomy term entered in the node being created or updated. By doing so, your 'Taxonomy with "limit" field' will become available as a token in subsequent Rules Actions. More specifically we'll need it (as a token) to specify a value for the node_limit parameter to execute the Rules Component from Step 2 later on.
    • Execute the Rules Component from Step 2, by passing that Rules Component the appropriate values for node_limit and node_termreference to actually perform its logic.

Done! .... only using (popular) contributed modules, with no custom code involved .... All you need is Rules, Views and Views Bulk Operations.

PS: the above is only a blueprint ... Mostly because your question has not sufficient details. E.g you didn't explain how those terms are defined in your content type).

  • "add a VBO field to it" ...how i do that? – a.alshamiri Feb 8 '17 at 11:42
  • @a.alshamiri : in your view, similar to adding any field to your view (to show it as an extra column in the views results). In the typical popup window, use filter value "Bulk Operations". That should show you a reduced list of candidates, which all start with "Bulk Operations". Get it? – Pierre.Vriens Feb 8 '17 at 11:48
  • i add (Bulk operations: Content (Content)) in view.. and i do exactly (steps) but does't work.. still create new node.. – a.alshamiri Feb 8 '17 at 11:56
  • Did my prior comment help to "add a VBO field to it"? About your last comment: the design of my answer is NOT that it stops the creation of a new node. Instead it "only" does not accept the term that was entered anymore. Can you confirm that the first Rules Action (in the Rules Component (step 2 ) about "Set an appropriate Drupal message" does produce the correct msgs already (= it is shown when the limit is reached, and it is not shown when the limit is not reached yet). If that's the case, you may need an extra rules Action in that Rules Component, ok? Be aware: remote debugging is tough! – Pierre.Vriens Feb 8 '17 at 12:12
  • VBO ..yeah it's help.. but not what i need.. as you see i want e.g (term1 - limit 3).. so when i had 3 node assigned to term1 and i want to create new node..I HOPE to show error message "that the node limit for that term has been reached already" .. Thanks again – a.alshamiri Feb 8 '17 at 18:27
1

I think if you work with node using Node Form, you should use hook_node_validate

  • I'd be interested to see/learn how the relevant part of a custom module would look like to answer this question here (using the hook you recommended). – Pierre.Vriens Feb 6 '17 at 12:40
1

I do not think any contributed module having this feature yet. You need to do little bit customization for this.

You put some condition in hook_node_validate to check how much node associated with that node and compare with the limit you have set.

You can use function taxonomy_select_nodes to Return nodes attached to a term across all field instances.

Limitation :::This function requires taxonomy module to be maintaining its own tables, and will return an empty array if it is not. If using other field storage methods alternatives methods for listing terms will need to be used.

Learn how to add custom validation to an existing Drupal form

  • Hope you don't mind my comment, but I do think there is a way to get this to work with contributed modules ... (stay tuned ...). But I'd be interested to see/learn how the relevant part of a custom module (using the hook/function you recommended) would look like to answer this question here. – Pierre.Vriens Feb 6 '17 at 12:39
  • My idea is alter the node add form and in node validate function. You will get the term id which you have entered. From that term id get the limit and selected node previously. Compare those two and use form_set_error() to restrict. – DRUPWAY Feb 6 '17 at 12:52
  • In my case.. how i add (taxonomy_select_nodes) – a.alshamiri Feb 8 '17 at 7:35
  • call this function and pass the term id. like taxonomy_select_nodes($tid). – DRUPWAY Feb 8 '17 at 7:42
  • 1
    Use some debug method to see the outcomes. Like dsm, kint ... – DRUPWAY Feb 8 '17 at 7:43

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.