Problem/Motivation
A missing piece of Rules functionality is the ability to respond to the event of an entity being prepared for editing. A common use would be to assign a value to an entity property, e.g., give a new node a default title.
Proposed resolution
Unfortunately there is no general entity hook we can use here. However, there is hook_node_prepare(), which is invoked on a node object that is about to be passed to the create/edit form.
The current patch adds a new rules event, 'node_prepare', and implements hook_node_prepare() to respond to it.
Remaining tasks
Current patch needs review. See #40 for a sample rule to be used in testing.
User interface changes
Provides a new event type that users can select when creating or editing a rule.
API changes
Provides a new event type, node_prepare.
Original report by benone
Is it possible to add this kind of event ?
I let user create only one instance of node type with pre-populated node reference field.
If he already created once using Node Reference URL Widget, instead of create form, I would like to redirect him to the page saying: You already created...
Hope you understand my concept.
Am I missing something or it can be a feature request ?
Thanks
| Comment | File | Size | Author |
|---|---|---|---|
| #67 | rules-entity_prepare_form_event-D8-762450-67.patch | 5.27 KB | vdsh |
| #65 | 762450-65-node-prepare-event.patch | 1.54 KB | tr |
Comments
Comment #1
benone commentedStill have no idea is it possible or not ?
I use now 'Content is going to be saved' , but I would like something like 'Node is going to be created (edited)'.
Am I missing something ?
Help please.
Comment #2
fagoUse the content is new condition + Content is going to be saved
Comment #3
benone commentedThanks for your response but this will trigger the rule AFTER pressing a save button.
I would like to trigger the rule before I get in to the new node edit form.
This what you wrote I already use but it's not what I want to achieve because user fill the form and after that he gets info that he cant create it. I would like to inform user about it BEFORE he start filling the form.
Any ideas ?
Comment #4
fagoAh, I see. I fear thats currently not possible as there is no suiting event.
Comment #5
benone commentedWould be difficult or time-consuming to add this kind of event to Rules ?
Comment #6
jwilson3So there is a core event that's part of hook_nodeapi called 'prepare' which triggers when "The node is about to be shown on the add/edit form." So, maybe this would work?
Comment #7
jwilson3Comment #8
benone commentedoh great, is it already in dev ?
Comment #9
jwilson3@benone, no this is not in dev (version 6.x at least), so to help get it in, it would be great if you could test the patch in #6 and provide your feedback.
Comment #10
mitchell commented@jrguitar21: That's really sweet you posted this patch. It looks very elegant and appropriate for full node_api coverage. Fortunately/Unfortunately, fago is almost laser focused on D7, so AFAIU, the 6.x-1.x-dev branch is mostly there for backports and bugfixes. Therefore, I highly encourage you to post this as a patch to Rules 7.x-2.x-dev along with the 6.x-1.x-dev ver.
In my best guess, incredibly similar code would go into events.inc and wrap around hook_prepare or hook_node_prepare.
Comment #11
jwilson3Ok.. so I havent looked at d7 much at all and definitely haven't looked at Rules D7. A quick look at the api, seems to suggest to me that we'd use hook_node_prepare, instead of hook_prepare, 1) because this action is node specific, and 2) hook_node_prepare runs later, providing an opportunity for other modules to react to this event first? Or is it better to get rules to react first?
Comment #12
fagoThat sounds neat. Let's implement it for D7 first, then we can add an analogous d6 version.
#11 sounds good. Rules should always react after the modules.
Comment #13
Coupon Code Swap commented+1 subscribe
Comment #14
kiwad commented+1
(Workflows in D6 with moderation based on taxonomy terms would benefit a lot from this. See : http://drupal.org/node/362105)
Comment #15
slerby commented+1 for this new event (D6)
Comment #16
jwilson3The maintainer has already said he wants it done in d7 first. there is a working patch for d6 above. please leave this set to the d7 branch.
Comment #17
jwilson3D7 patch, tested here and confirmed to work, it triggers on node add and node edit forms and you can use the 'entity is new' or 'node is new' conditions to single out for new versus edit form.
Now that I've looked at the d7 code, I'd bet that with the new entity system we could add in a complete set of rules triggers for all the CRUD methods for any entity type (nodes, comments, users, etc).that was a stupid comment :/ sorry. I see that all crud triggers have been implemented for users/nodes/comments.Comment #19
jwilson3grrr. is this patch serious flubbed? try again...
Comment #20
kiwad commentedAbout the patch in #6 (for D6)
Applied it manually and it works, although I think it would need to be re-rolled to sync with latest version (+ - lines changed)
Comment #21
klausiPatch does not apply.
Comment #22
jwilson3reroll.Please Ignore this patch. :D
Comment #23
kiwad commentedhmm... opening the patch in #22 it is written :
name = Wordpress import
modules/wordpress_import
Comment #24
jwilson3HAHA. oops sorry. working on two things at once.
Comment #25
jwilson3the **right** module this time...
Comment #26
klausitrailing white space
Powered by Dreditor.
Comment #27
fagoThanks. Also I don't think we should add the unchanged content variable, as it probably will just confuse users (what's the difference to the "edited content"?). I don't think one wants to compare the edited content with the unchanged content anyway, so having just "edited content" should be fine.
Also, could we get a small rule in the tests (core integration test case) making use of the event? test coverage ++
Comment #28
altavis commentedsubscribing
Comment #29
Docc commentedIs this going the be in anytime soon?
Comment #30
jwilson3Updated the D7 patch to use the Entity API, I looked at the testEntityIntegration() test in tests/rules.test but don't have the first clue about how to create a test for this. Perhaps someone with a little more experience with Rules and the test framework could provide some help.
Comment #31
jwilson3Forgot the actual patches....
Comment #32
jwilson3fixed whitespace issue on patch in #31.
Comment #33
klausiThere is no hook_entity_prepare(), only hook_entity_prepare_view() or am I missing something?
Also: that hook is triggered when entities are viewed, not edited?
You invoke the event for all this types, but you register the event for nodes only?
Comment #34
fagoThe event sounds very useful, sad there is only the hook for nodes.
Comment #35
jwilson3Hrm, yes. That sucks. I'm considering filing a core issue about this. From a quick glance over system.api.php, I see no other obvious way to hook in before displaying an entity edit/add form. The only thing I can come up with is executing the trigger inside a hook_form_alter that checks to see if its a node-edit node-add form. Thoughts?
Comment #36
klausijust implement hook_node_prepare() and add the event "Content is going to be edited", so that it at least works for nodes.
Comment #37
Docc commentedThere is http://drupal.org/project/entity wich extends the entity API. But there is no prepare in there either....
Comment #38
jantoine commented@fago, per #34, could this be implemented for user profiles via the Profile2 module, or is that modules hooks dependent on user hooks?
Comment #39
nedjoAttached patch uses
hook_node_prepare(), in a parallel way to the existing user-specific event hooks likehook_user_login().Yes, it's a shame we don't have an equivalent hook with other entity types. But for nodes this is very handy, e.g., for conditionally assigning default values to fields on new nodes.
Comment #40
nedjoHere's a sample rule to test the event with. Assumes a 'page' content type. Import the rule and then visit node/add/page. You should see the new node has a default title of "New page by [your username]".
Comment #40.0
nedjoAdd summary.
Comment #41
nedjoThe rule in #40 doesn't work as intended. When testing it, I get a new node created before the node/add form is created. Does setting a value on a node trigger a save operation? Is there a way to set an entity value without triggering a save?
This issue doesn't necessarily invalidate the patch, but does limit its use cases and provide potential confusion.
Comment #42
nedjoTried to address the issue noted in #41 by setting
'skip save'toTRUEfor the node_prepare event, updated patch attached, but that didn't seem to fix it. Other testers or suggested fixes?Comment #43
colanI just added the core issue #1537452: Generalize hook_node_prepare() with hook_entity_prepare().
Comment #44
kaizerking commentedsorry for trying to push in more in to this, but it should be a good idea to put it here at least I feel so,
"The going to be" actions may be extended to 'going to be created' , 'going to be edited(updated)', 'going to be deleted'. these feature requests were in request que. and should be applicable to nodes and entities as well.Please consider.
Comment #45
nedjo@kaizerking: support for all entities would require a solution to the core issue colan posted, #1537452: Generalize hook_node_prepare() with hook_entity_prepare(). To distinguish between add and edit, you can use an entity is new condition in your rule. "going to be deleted" is probably not relevant here since
hook_node_prepare()applies only to add/edit. It's true that there is a delete form for a node, but IIRC only the ID and title are used and I'm not sure there's a strong use case to alter either.Comment #46
kaizerking commentedMy point is with reference to this request here Entity, node going to be deleted Since the current issue is for an action"going to be"I have high lighted.
Comment #47
zhangtaihao commented@kaiserking: The "is going to be" convention seems to have been D6-only. The first commit of
node.rules.incin D7 uses the "Before" convention. The fact that "Content is going to be saved" occurs inrules.api.phpmay not mean much.@nedjo: The word "passing" is not really user-friendly. On account of hook_node_prepare(), I propose "Before showing content on the add/edit form".
Comment #48
zhangtaihao commentedI guess the component terms have changed.
Comment #49
zhangtaihao commentedAlso, the
'skip save'attribute should be on the variable. Something along the lines of:Comment #50
ymmatt commentedAny chance this made it into 6.x-1.5?
By the way the D6 patch in #31 worked great for me.
Comment #51
brightboldPatch in #42 solved my problem in D7 allowed me to create a rule that fires when someone of a certain role tries to edit a particular node type. By combining this patch's "Before passing content to the add/edit form" event with a "NOT Entity is new" condition and a "node:status" text comparison matching "1" condition, I can now allow creation of the node and editing of unpublished nodes, but prevent editing of nodes once they've been published.
Thanks! I had been banging my head up against this for a while.
Comment #52
fagoSee #49 - skip save needs to move.
Comment #53
jerenus commentedImprove #42 with #49. New patch.
Comment #54
jerenus commentedbot
Comment #55
alibama commentedThis is exactly what I need, however it only runs on node content - anyone have any pointers on how to include entityforms? a basic pointer would be immensely appreciated
Thanks!
Comment #56
nedjo@alibama: see the issue summary above for explanation of why this patch addresses only node entities.
Comment #57
jwilson3@alibama #55: way back in #35, i suggested that a trigger could be added inside a form alter hook, though it would take some experimentation on how and when to trigger it correctly.
Comment #57.0
jwilson3Explain more about current patch.
Comment #58
kitikonti commentedWhat is the status of this feature? Is it already in a dev version?
Comment #59
colan@kitikonti: Changes aren't in dev releases until they're marked "Fixed". Feel free to move things along by reviewing the code (or having someone else do it) and testing it. If everything is okay, the state can be changed to RTBC.
Comment #60
fagoHas some whitespace here.
Let's better just assign that for node_prepare *below*, so it's obvious this only applies to node_prepare.
Comment #61
nwom commentedHere is a re-rolled patch of #53, which includes the first change noted at #60. I have not added the second suggested change, since I was not entirely what was supposed to be edited. Leaving it as Needs Work for now.
Comment #62
sano commentedPatch #61 works for me*. Thank you.
* if I disable the jammer module - the "Generic Jammer" in particular
Comment #63
Abelito commentedThis patch #61 works for me too. It is super helpful!
I'll see if I can find the time to figure out what fago wants us to do in the 2nd part of #60
Comment #64
tr commentedPostgreSQL test fails because of #1627174: Error in rules Scheduler module on PostgreSQL, it's not the fault of this patch.
Comment #65
tr commentedThis is a re-roll of #61 against HEAD.
I have also addressed @fago's second comment from #60.
I added a "Needs tests" tag.
We are not really adding new features to D7 anymore, but since there's been a patch here for a long time I'll still consider it. BUT at a minimum this patch should include a test for this new event, in RulesTestCase::testNodeIntegration().
Comment #66
vdsh commentedFollowing TR's feedback on the related thread I created for D8 #3130326 - I am continuing the discussion here.
I have partly ported this patch to D8 following the same principles (but using the hook_entity_prepare_form).
2 main things remaining:
- Tests: I have added the Unit test for that Event, but I am not expert in Kernel tests. What would be the best way to simulate the form going to be displayed (to trigger the hook)?
- I have the same issue as reported in #41 that the entity is created if you set a value, which may not be the desired behavior. I am wondering what the best way to overcome that issue would be, and I thought that maybe it would be best to do it in RulesAction:dataSet where we can add an option to enable or disable the autoSaveContext. What do you think?
Comment #67
vdsh commentedAttaching patch & running tests
Comment #68
vdsh commentedComment #69
delacosta456 commented###EDIT###
SORRY IT IS WORKING AS EXPECTED... there was another condition from custom module that was not correctly working... so IT IS WORKING correctly for ADD and EDIT
Comment #70
tr commentedComment #71
nwom commentedI would love to use #67, but I just read that the following would still need to be added in a D9 equivalent way:
$variables['node']['skip save'] = TRUE;Any ideas on how we can achieve this? I really miss this feature from D7.
Comment #72
nwom commented@vdsh: Is it possible that the solution provided in #3172088: The "Set a date value" action results in an infinite loop if it's using in the "Before saving a content item" event at comment #7 could be the fix for this issue as well?