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.
Would be nice if the FormState object could determine whether a form's values have changed, so to say that whether it is dirty.
Comment | File | Size | Author |
---|---|---|---|
#11 | interdiff_2-11.txt | 5.91 KB | _Archy_ |
#11 | forms_system-add_is_dirty-2847821-11-D86.patch | 5.39 KB | _Archy_ |
#2 | forms_system-add_is_dirty-2847821-2-D83.patch | 1.53 KB | _Archy_ |
Comments
Comment #2
_Archy_ CreditAttribution: _Archy_ at PitechPlus commentedAdded an implementation for this. Don't know for sure, but I didn't see any way that the FormState knows about the form, so I added as a parameter the form. But it would be nice if that parameter didn't exist. If anyone has an idea. Maybe implement a way for a form state to know about the forms default values?
Tests will be added when this approach is confirmed good.
Comment #3
shadcn CreditAttribution: shadcn at Chapter Three commented$this->getCompleteForm();
will get you the form.See https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21Fo...
Comment #5
_Archy_ CreditAttribution: _Archy_ at PitechPlus commented@arshadcn for that method to return the form, it must be set first with setCompleteForm() on the form state. It isn't always set. In the context I was testing this new method it wasn't set, so to use that I would have to set it before calling isDirty().
I don't really understand all this fails for the patch. It only introduces a new method. Also checked that I made it for the right branch (8.3.x). Maybe someone can explain?
Comment #6
xjmComment #7
tim.plunkettWhen was this? If it wasn't set, the form was not finished building, so it would be too early to check for all #default_value elements anyway, since all #process and hook_form_alters will not have run yet.
The error is:
Class Drupal\Core\Form\SubformState contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Drupal\Core\Form\FormStateInterface::isDirty)
Nit: should be the same line
This can be an infinitely nested array. Checking the form at the top level will not work here.
What is a use case for this?
Comment #8
_Archy_ CreditAttribution: _Archy_ at PitechPlus commentedThanks for looking into this. I will think of other ways when I'll have time.
Till then I'll link the issue from which this idea came. That will explain a use case and will give you the context in which the for wasn't set on the form-state.
Comment #11
_Archy_ CreditAttribution: _Archy_ at PitechPlus commentedDid some work for this. Refactored they way the checking works. Addressed comments from #7. Created tests, but only unit ones. Do we need functional ones?
Comment #12
_Archy_ CreditAttribution: _Archy_ at PitechPlus commentedThis method of checking is not the best performance wise as it is linearly taking all elements of the form and comparing the values till it gets one that has different value. It works well if we check for all the values of the form, but when we want just a subset of fields to check it still traverses the whole form. This could be improved if there is a way of converting #parents to #array_parents, but I am not sure that is possible.