I was working on a new module last week aimed at allowing users to choose files more easily and hit some roadblocks when it came time to update the Insert Media form.
What I wanted to do
Nest another level of tabs under the Insert Media > 'From CMS' tab. Keep the original fields there (under a new sub-tab) and add some additional options (recently uploaded and favourited files)
What I thought I could do
Access the 'From CMS' tab with $form->Fields()->fieldByName('FromCms')
What went wrong
fieldByName() didn't traverse deep enough and returned an empty result - I don't think these functions account for the possibility of nested composite fields. Failing that I thought of chaining multiple fieldByName() calls together but couldn't because of the presence of an unnamed composite field in the chain.
What I actually did
$fromCMSTab = $form->Fields()->items[1]->fieldByName('MediaFormInsertMediaTabs')->fieldByName('FromCms');
Messy and fragile - if the original form is rearranged later, the items[1] part (which is the only way I could think of to access an unnamed CompositeField) could fail and break my extension.
Fix or upgrade?
I might be able to fix some of this with a PR but I wondered if there's a need to make form traversal more robust and work like DOM traversal with jQuery?
$field->parent() : returns the FieldSet or CompositeField that is it's direct parent, or false
$field->moveTo($compositeFieldName) : move a field, tab or tabset to a different composite field, tab or tabset
I'm sure there are some others that could be useful but these two, in addition to fixing the fieldByName logic to find a field now matter how far down it's nested, could make it a lot easier to manipulate forms.
Is there an easy way of doing this already that I'm missing?