Problem
hook_node_validate() and hook_node_submit() are hooks which can be considered already deprecated in d7: No other entity types have them and it's not best practice to use them: Instead, you should use a form element validation and a form builder callback to extract form values.
But in Drupal 8 hook_node_validate() is even worse: It bypasses the entity validation API and thus *may not* be used (e.g. think of REST). Therefor it should be removed. For validation you should use constraints with the validation API instead.
Proposed resolution
Remove hook_node_validate() and hook_node_submit() and convert their usages to constraints, added via hook_entity_base_field_info_alter()
Remaining tasks
Add minor fixes to the patch and write draft change notice.
User interface changes
-
API changes
hook_node_validate() and hook_node_submit() are removed.
Comment | File | Size | Author |
---|---|---|---|
#22 | 2406103.21-interdiff.txt | 553 bytes | Berdir |
#22 | 2406103.21.patch | 19 KB | Berdir |
#19 | 2406103.19-interdiff.txt | 8.62 KB | Berdir |
#19 | 2406103.19.patch | 18.89 KB | Berdir |
#15 | 2406103.15-interdiff.txt | 10.88 KB | Berdir |
Comments
Comment #1
fagoalso opened related but uncritical #2406107: hook_node_submit() is depcrecated and should be removed
Comment #2
fagoComment #3
BerdirWe had one case where we had to use this hook. It is currently not possible to add constraints to a configurable field. We have a field that is just a simple string field, but it has to be unique. It would be annoying if we have to define a field type just for that, as we then also have to alter widgets and formatters to make them available.
Comment #4
fagoThe only usage in core is forum_node_validate(), which seems to add custom validation to taxonomy_forums. Thus, I guess that's what you are referring to.
Not being able to attach custom constraints to configurable fields is a generel problem though imo, or how would people attach custom validation logic to them else?
Comment #5
BerdirMy example was from a custom project, but yes, forum is similar.
Yes, it is a general problem, but it is also a blocker for removing this hook ;)
Comment #6
fagoRight. Thus, this needs to be fixed first then. Postponed on #2247085: Constraints cannot be added to configurable fields
Comment #7
fagoComment #8
BerdirPersonally, I'm fine with it either way, just wondering if we can justify an API change for removing it completely, or should we mark it as deprecated and say that you should use constraints instead?
We probably can, because security and stuff, but I still wanted to ask.
Comment #9
fagoImo, it should be removed, because any implementation would have security implementations and is bad practice. But that's probably something a branch maintainer should decide.
Comment #10
larowlanpretty sure forum_node_validate is removed in #2247085: Constraints cannot be added to configurable fields (party)
Comment #11
alexpottI agree with @fago - we should remove hook_node_validate() and hook_node_submit()
Comment #12
alexpottA starter for 10.
Comment #13
Berdirnot sure if submit belongs in this issue.
But when we touch it, then should be able to clean this up so that we don't have to attach $node->menu to the node anymore if we add the submit callback last and not at the beginning?
Comment #14
klausiI searched for node_submit and there is core/modules/node/src/Tests/NodeSaveTest.php with a strange "Function node_submit() preserves user ID", so we should probably also fix that comment in this patch.
I searched for node_validate and core/modules/system/entity.api.php still mentions it, so that should be fixed, too.
But otherwise the patch looks good. I think we should not try to also fix $node->menu in this critical issue, that can be a follow-up.
And we will need a change notice draft with instructions what developers should use instead.
Comment #15
BerdirWas very hard to stop with the menu_ui cleanup ;)
Comment #16
larowlanthis so feels like we want a hook_entity_base_field_info like path module and define a 'menu_link' ER field. Perhaps a follow up? Would be so much cleaner.
Comment #17
yched CreditAttribution: yched commentedYay ! Any chance we can stop doing
$node = $this->buildEntity($form, $form_state);
a couple lines above then ?See #1768526: NodeFormController::validate() calls buildEntity() twice :-)
Nitpick : if it's a helper, can we prefix it with '_' ?
The current name still looks damn close to a hook implementation :-)
Nitpick: the function name isn't very clear on "definition of what ?"
Comment #18
Berdir3. That's kind of the problem... menu_ui_get_weird_definition_that_is_somehow_supposed_to_represent_a_menu_link() ? ;) I kept that "definition" thing, but it really is pretty strange. menu_ui_get_menu_link_definition() doesn't make it much clearer? maybe menu_link_defaults() would be better? (which resembles the hook that we had some time ago ;))
Comment #19
Berdir1. Hm. We still have the changed validation there, but we should be able to fix that by returning $entity in parent::validate(), just like we already do for save()?
2. Added _, also renamed $definition there to $values.
3. Renamed to menu_ui_get_menu_link_defaults() as per my last suggestion, also renamed the relevant variables. Even more unrelated changes in this issue :)
Comment #20
yched CreditAttribution: yched commentedRight, I missed the
$node->getChangedTime()
at the very end of the line :-/That sound like a plan, yes, it feels silly to have each override of validate() re-do a buildEntity() because the results are lost.
Probably more a task for #1768526: NodeFormController::validate() calls buildEntity() twice than for this patch here, though :-)
Comment #21
klausiLooks good, I had to review with git diff --color-words, most of the changes are just variable renaming. One nitpick left then we only need the change notice draft.
Now the sentence above does not make sense with the ending ":" ;-)
Comment #22
BerdirHehe, nice.
No, they don't. At least nothing that's worth documenting here. So I just removed it...
Started with a CR: https://www.drupal.org/node/2420295
Comment #23
yched CreditAttribution: yched commentedYay. Looks RTBC to me then.
Comment #24
klausiRTBC+1, I updated the change notice with an example D7 version for hook_node_submit() (which does not make completely sense, but it should work to demonstrate the conversion).
Comment #25
fagoI don't think a #submit builder is a correct alternative to the #entity_builder, i.e. it would be missed from EntityForm::buildEntity() and thus during validation. Thus, I removed that "alternative" from the change record.
Patch looks good to me as well.
Agreed that this code use more clean-up, but this can be a non-critical follow-up and should not hold up this!
Comment #26
BerdirI disagree. Yes, #submit is not an alternative to #entity, but that is not what I said (or meant). But that hook_node_submit() should be converted to entity builder or #submit. This patch uses #submit, and it is correct in doing so. If you want to *do* something with the created entity, use #submit, if you want to help building it before saving, use #entity_builder.
If that was not clear enough then we need to improve it, but removing it is wrong.
Comment #27
fagoI see - right, there a valid cases and some of the previous hook_node_submit() cases might fit there. So it was the CR which hat a wrong example then and did not point out when to use what. I tried to improve this now, please check it.
Comment #28
catchOK. Committed/pushed to 8.0.x, thanks!
I think we have an issue somewhere to look at the menu links on node form (or that's one of the intended follow-ups from the NJ sprint at least), so we can revisit that code in there.
Comment #30
mikey_p CreditAttribution: mikey_p commentedI think this is the issue for the menu_links on the node form: #2315773: Create a menu link field type/widget/formatter.
Comment #31
BerdirThanks, restored and extend my initial example in the submit callback, to get the $node object and doing something with it, which is I think something that might be more common than getting values from $form_state, and people know how to do that.
Comment #32
fagoThanks, that looks good - I've fixed the CR version to be the next beta instead of the current one and published it.
Comment #34
cilefen CreditAttribution: cilefen as a volunteer commentedThis may have changed a UI behavior.