I have a rule that executes when a certain type of entity is created; when this happens, an Action Set creates new entities based off of the content of some text field in the original entity--everything works great.
The problem is that with this approach, the user can create duplicate content. I've looked at all the options, and determined that the easiest solution would be to create a new Rules Action.
The action would be a clone of the "Create a new entity" action, with some additional PHP code that check if the entity exists before creating it. Additionally, if an existing entity is found, it would pass it off as the "created_entity" variable so that it can be used as normal with the rest of the actions in the action set.
I haven't done any development with Rules before... Can somebody give me some hints, on a high level, as to what classes/functions I would need to modify? What code would I need to clone, and would there be any special "gotchas" with finding the existing entity and then passing it off as the "created_entity"? I'm concerned in particular that Rules may not have access to that entity's fields if it wasn't created as part of the normal "create a new entity" process.
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | rules-createunique-1992192-4.patch | 4.74 KB | micromegas |
Comments
Comment #1
micromegas commentedDoes anyone have any ideas or hints to share?
It seems like the files are entity.rules.inc and entity.eval.inc, and the best course of action would be to clone the "Create Enity" section into a new "Create Entity If Unique" section that contains the additional code.
Comment #2
micromegas commentedComment #3
micromegas commentedMost of this is covered in the developer documentation at http://drupal.org/node/878718, closing.
Comment #4
micromegas commentedTo anyone who is searching for a similar solution, I've attached a patch for 7.x-2.x (dev).
The new create_unique function contains an SQL query for existing entities first, and, if found, passes the first found entity off as the "created entity" variable instead of creating a new one--read the patch itself for more details.
I do intend to roll this into a proper patch, that works on all entities, and contains GUI elements to select the match type ('contains' or 'exact') and match field, but for now you will have to modify the code to fit your specific circumstance.
Comment #5
SharonD214@aol.com commentedTrying to apply this patch #4 with the dev version gives me the following error:
Fatal error: Class 'RulesEventHandlerEntityBundle' not found in ...l\sites\all\modules\rules\modules\node.rules.inc on line 135.
Reverting to Rules 7.x-2.3 works fine (dev version without patch also gives the error)
Any ideas?
Sharon
Comment #6
micromegas commentedIf the dev version gives the error regardless if the patch is installed or not, then I'm guessing there is some sort of issue with the dev version.
Try to apply the patch against the stable release of 7.x-2.2 and see if it works.
Also, the given patch will only work for node entities, and only the title. Another tip; if your new node title is "new node", this will match "anything new node anything"
Comment #6.0
micromegas commentedchanged "I have done Rules development" to "I haven't"
Comment #7
sonicthoughts commentedThe patch is against an older version of rules. I see this has been closed. any chance this feature can be committed?