Please, advice me on such a question about the Rules module.
Background: I have two types of users and 2 types on content. Each user can create one type of content. User of type2 can create a content of content_type_2 which has an entity reference field to a node of content_type_1. But this user should have a limit - such user may reference a node of content_type_1 only once.
How I implemented it:
rule is triggered when user_type2 is visiting content_type_2 node-add form. A NID of reference to content_type_1 is present as URL parameter.
rule fetches nodes by property (where author is current user)
for each item of the list of fetched entities is triggered another one rules component
component checks is current list item is of "content_type_2"
component checks current list item's entity reference field (nid of the referenced node) is equal to the nid value from URL parameter.
if both conditions are true - prevent node saving and display and error message.
It works fine and does it's job. But I worry about the following: what will happened if a user of user_type2 already has let say 10000+ of nodes of content_type_2? Does it mean that "fetch entity by property" will query for all that number of nodes at once? Can it highly slow down my site performance?
If it is true, maybe there is a better way of organizing this checking process? Will be thankful for any comment and advice!
UPD 1 (to make the question more clear):
User type "Client" can create nodes of type "Request" .
User type "Provider" can create nodes of type "Proposal".
"Proposal" type has entity reference field to "Request" node (only 1 value allowed, it is required).
Provider can create multiple number of "Proposal" nodes.
But he has to be restricted to create only 1 "Proposal" per a certain "Request" node. So he should not be allowed to reference a "Request" node if he has already referenced it in any of his previous Proposals.
Reference field is filled automatically ("Provider" can't do it by hands). "Provider" also can't edit any of published Proposals, but can delete any of them.
UPD 2 (Rules export)
Reaction Rule which is triggered at "node/add/proposal"
{ "rules_check_unique" : {
"LABEL" : "Check Proposal is unique per Request",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"TAGS" : [ "business_custom" ],
"REQUIRES" : [ "rules", "rules_url_argument" ],
"ON" : { "init" : [] },
"IF" : [
{ "data_is" : { "data" : [ "site:current-page:path" ], "value" : "node\/add\/proposal" } },
{ "rules_url_argument_argument_exists" : { "argument" : "field_request_reference" } }
],
"DO" : [
{ "rules_url_argument_rules_action_get_argument_from_url" : {
"USING" : { "type" : "integer", "argument_name" : "field_request_reference" },
"PROVIDE" : { "argument_from_url" : { "argument_from_url" : "Argument from URL" } }
}
},
{ "entity_query" : {
"USING" : {
"type" : "node",
"property" : "author",
"value" : [ "site:current-user" ]
},
"PROVIDE" : { "entity_fetched" : { "entity_fetched" : "Fetched entity" } }
}
},
{ "LOOP" : {
"USING" : { "list" : [ "entity-fetched" ] },
"ITEM" : { "list_item" : "Current list item" },
"DO" : [
{ "component_rules_if_proposal_is_already_posted_for_a_request" : { "cnid" : [ "argument-from-url" ], "proposal" : [ "list-item" ] } }
]
}
}
]
}
}
Component Rules which check if Proposal for the referenced Request already exists
{ "rules_if_proposal_is_already_posted_for_a_request" : {
"LABEL" : "If proposal is already posted for a request",
"PLUGIN" : "rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"USES VARIABLES" : {
"cnid" : { "label" : "Current NID", "type" : "integer" },
"proposal" : { "label" : "Proposal", "type" : "node" }
},
"IF" : [
{ "node_is_of_type" : {
"node" : [ "proposal" ],
"type" : { "value" : { "proposal" : "proposal" } }
}
},
{ "data_is" : { "data" : [ "proposal:field-request-reference:nid" ], "value" : [ "cnid" ] } }
],
"DO" : [
{ "drupal_message" : {
"message" : "You have already made a proposal for this request.",
"type" : "error"
}
},
{ "redirect" : { "url" : "node\/[cnid:value]" } }
]
}
}