conditional_fields_form_after_build() is way too long.

My IDE is telling me that:

Method length is 94 lines (20 allowed)

It's so long, in fact, that I can't even figure out what's going on, which is exactly why functions and methods should do one thing (as per Clean Code).

Typically, my hook implementations consist of no more than a few lines, just enough to get whatever needs to be done in OO, where all of the work is handled. Why? Because you can make small methods allowing for understandability, maintainability, ease of development.

Having said all of that, I'd rather not make this function any longer, which would otherwise be required by a couple of issues, notably #2891276: checked, !checked don't work because states set on checkbox, radiobutton wrapper divs, not HTML input elements and #2859667: Conditional Required Field not Evaluating on Save with Autocomplete widget. We need to refactor this in a cleaner way with objects and methods before adding anything else.

Help with this would be greatly appreciated (especially from those that already understand the code).

Continuing as before would make this module much more challenging to work with in the long run.

Comments

colan created an issue. See original summary.

colan’s picture

Priority: Normal » Major
colan’s picture

colan’s picture

After taking another look, probably the best way to go here would be to delete that entire file and move just about everything to src/. conditional_fields_evaluate_dependency() is actually longer.

A minimal #after_build function can move back into the .module file, just to get things going.

dillix’s picture

Any news on it?

colan’s picture

I don't have time to work on it myself, but it needs to get done. Patches welcome!

marvil07’s picture

Status: Active » Needs review
StatusFileSize
new54.94 KB
new73.78 KB

I have followed colan suggestion.
I have started by just moving things around, and changing related code to support the change.
I have used static methods on a helper class, we may want to convert that into an actual instantiatable class, but this patch is not trying to do that yet.
Next step would be to actually re-organize the content of the after build callback.

Moving to needs review for feedback.
Maybe the change is worth to be added and we can later do a follow-up, or a new commit here.

Two patches are attached, the first is a simple one, the second is step by step patches that could be imported via git am if someone want to review the steps.

marvil07’s picture

StatusFileSize
new56.78 KB
new84.9 KB
new8.8 KB

New changes since last patch:

  • Move _conditional_fields_element_add_property() to a helper class.
  • Fix indentation for switch.
  • Tweak a bit afterBuild logic for easier readability at the start.
  • Move states preparation logic out of afterBuild().

  • colan committed 2053c4f on issue-2926132 authored by marvil07
    Issue #2926132 by marvil07: Replaced conditional_fields.api.inc with a...
colan’s picture

Title: Refactor conditional_fields_form_after_build() » OOify contents of conditional_fields.api.inc and then delete it
Assigned: Unassigned » colan
Status: Needs review » Needs work

Thanks for all of your work on this. I'm working in a branch because this is conflicting with #3068112: The tests are deprecated and some conditionals are not working., which was unfortunately committed before this one. The above commit handles everything except for conditional_fields.api.inc changes that were applied in the other issue. I'm working on that now so there should be another commit soon (same branch, other issue), which applies those changes to the new class.

Maybe the change is worth to be added and we can later do a follow-up, or a new commit here.

I'm happy either way, but for now, I'd like to get this stuff committed before anything else does. But yes, we'll still need to make the new class instantiable (i.e. switch to non-static methods) in a follow-up issue or in here.

  • colan committed 2053c4f on 8.x-1.x authored by marvil07
    Issue #2926132 by marvil07: Replaced conditional_fields.api.inc with a...
  • colan committed 63fbb0a on 8.x-1.x
    Issue #2926132 by colan: Merge branch 'issue-2926132' into 8.x-1.x
    
colan’s picture

Status: Needs work » Fixed

Done. See #3077803: Make ConditionalFieldsFormHelper class instantiable for the follow-up.

Please reopen this if there are any problems.

colan’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.