Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Simple feature request to bypass the required scheduler field on a node when the delete button is hit. Since you wish to delete the node, you shouldn't have to bother.
Comment | File | Size | Author |
---|---|---|---|
#16 | 1614880-16-node_delete_form_validation-test_only.patch | 2.32 KB | pfrenssen |
#16 | 1614880-16-node_delete_form_validation.patch | 4.59 KB | pfrenssen |
Comments
Comment #1
Jasonrj CreditAttribution: Jasonrj commentedI have this problem as well.
To elaborate a little: If you have a node which requires a scheduled publish date, but you have not filled in a date, you cannot delete the node without first putting in a publish date some time in the future.
This is a problem when you edit a node type and add the scheduler requirement, then all previously published nodes of that type cannot be deleted without adding a scheduled date first. Annoying and doesn't make sense.
Comment #2
jonathan1055 CreditAttribution: jonathan1055 commentedYes that is a problem. I think it is similar to #1198788: Editing a published node with "Publishing date/time is required" forces users to set a new "Publish on" date/time about not validating the date/time when you just want to edit the node after it is already published. There is a patch in that issue - maybe it can be worked to cover both scenarios?
Jonathan
Comment #3
jonathan1055 CreditAttribution: jonathan1055 commentedI did not know this before, but after a bit of searching I found the excellent feature of the form API in D7 #limit_validation_errors which limits validation to specific sections depending on the button clicked. So in scheduler_form_alter we only need to add:
Here is a patch, against 7.x-1.1+10
Jonathan
Comment #4
pfrenssenBe careful because this patch disables ALL validation. There are use cases where validation is absolutely necessary on the delete action. Example:
Comment #5
jonathan1055 CreditAttribution: jonathan1055 commented:-)
Yes all field validation is bypassed, but the user still gets the required "Are you sure you want to delete ... this action cannot be undone" message, and they need to click 'Delete' again.
I cannot think of any validation which should be done on data fields if the node is about to be deleted.
Jonathan
Comment #6
pfrenssenWell, the use case I described above is a pattern that I have seen before. I can come up with more :) What about a site that requires a captcha to be filled in to prevent mass deleting by a bot? Or a site that requires editors to provide a log message when they are deleting content? A frontend for an asynchronous backup tool which is only allowed to delete a backup job when it has been flagged with "backup successfully completed"?
It's impossible to predict all use cases. I would only skip the validation of fields that the Scheduler module itself provides.
Comment #7
jonathan1055 CreditAttribution: jonathan1055 commentedYes, I take your point, we should only remove validation from the scheduler fields. Do you know the best way to do this, as the validation for the 'required' fields is done before the scheduler's own hook_node_validate(). Is there a standard pre-validate hook like I've see on date_popup? The nicest way would be to manipulate the form if the user clicked delete and remove the 'required' attributes, but I don't know offhand if that is possible.
Comment #8
pfrenssenHmm I don't believe it is possible to use #limit_validation_errors on individual elements.
Now that I am thinking about this, we should not mark this field as "required" at all times, it should only be required when the node is not already published. This matches the use case in comment #2.
But this seems to be the case already, I see this line in scheduler_form_alter():
I just looked through the git history and this has been fixed in commit 2e2f3b3 as part of #1198788: Editing a published node with "Publishing date/time is required" forces users to set a new "Publish on" date/time. I'm marking this issue as a duplicate of that issue. That issue also has some more discussion and advanced use cases on this topic.
Note that if this setting is changed on a site that already has nodes, then this field will be marked as required on unpublished nodes but that is normal, that would also happen if you mark any other field as required using the Field UI module. It is the responsibility of the developer making the change to provide an update hook or script to loop over all nodes and make sure that all fields are valid.
Comment #9
jonathan1055 CreditAttribution: jonathan1055 commentedHi Pieter,
I don't think this is a fair comment. I'm sure a large proportion of our users are not coders or developers and would not have the means to write a script or an update hook. They just use Scheduler 'out of the box' and I think it is very likely that sites will have many nodes added, probably before they even install Scheduler, then they set the 'required' option. We need to provide a nicer way for users to delete nodes.
Luckily, I have found another way of doing this, better than before. We can use the #value_callback function on the dates, which gets triggered before field validation. It can check if the button pressed was "delete" and then set #required to FALSE.
In schduler_form_alter, for each of the dates we add:
and then the new function:
See forms_api_reference.html/7#value_callback for more info on the value_callback function
Patched against 1.1+20
Comment #11
jonathan1055 CreditAttribution: jonathan1055 commentedThe failed test reported:
Undefined offset: 8192 PHP Notice scheduler.module line 430
Line 430 is
Do we need to have Date Popup in the test dependencies even if those functions are not actually going to be called during the tests? The current .test file does not do anything with date popup. It runs locally fine and I do not have date_popup in my test dependencies. I even removed the date popup module from my test environment and it still runs ok. Or is it something to do with calling $form_state by reference not by value? Annoying because it all runs perfectly locally.
Comment #12
jonathan1055 CreditAttribution: jonathan1055 commentedChanged &$form_state to $form_state in the call to date_popup_element_value_callback()
Comment #13
jonathan1055 CreditAttribution: jonathan1055 commentedThat's better. I see that Pieter had actually committed a change to .info to add the test dependency on Date before the failed run in #9, so it was nothing to do with that. The call by reference was the problem, and it was wrong anyway, but interestingly it was not highlighted when I ran the tests using Drupal's built in testing on my site. It shows the benefit of also utilising the qa.drupal.org automated tests.
Comment #14
jonathan1055 CreditAttribution: jonathan1055 commentedRe-roll was required after 1.1+34
Comment #15
jonathan1055 CreditAttribution: jonathan1055 commentedThe patch above failed to apply after 1.1+35, so here's a re-roll.
Comment #16
pfrenssenI've written a test. It seems that in the latest 7.x-1.x-dev version the problem only occurs when trying to delete published nodes. Deleting unpublished nodes works fine without the patch.
Comment #18
jonathan1055 CreditAttribution: jonathan1055 commentedThe "failure" was expected, there is nothing wrong with the code. I tested your tests manually too, all good. Yes I think we have been helped by #1198788: Editing a published node with "Publishing date/time is required" forces users to set a new "Publish on" date/time in the case of unpublished nodes. RTBC
Comment #19
jonathan1055 CreditAttribution: jonathan1055 commentedComment #20
pfrenssenThanks for the quick review!!
Committed to 7.x-1.x: commit ebfb1bb.
Comment #21
jonathan1055 CreditAttribution: jonathan1055 commentedthank you too for all the commits