I am trying to get some media fields to hide/show conditionally using $form fields #states API, based on other media fields (all the media fields are using the "Media Library" widget in form display.) I'm working with a node/add and edit for a content type in Drupal 9.5.4 and using hook_form_alter() to manage the states.
There are 3 media fields in total - if one has media added the other two should become invisible/hidden, but if the media that was added is removed then the other two fields should display again - which seems to be impossible (as far as showing them again).
After many hours of trial and error the only success I have achieved is hiding 2 of the 3 fields when the third one of them has media added.
- using
input[name="field_name[selection][0][target_id]
as the conditional controller which is problematic because technically that input doesn't exist in the form until media has been added thru the media library widget. So removing that media "will not" make the other media fields show again as the input is also removed from the form.
It was also kind of dumb luck that I managed to get that input to hide the 2 unused media fields on the form. As I was initially trying to write the #states like I was for some of the other (none media fields in the form).
For example: working with non-media field using the following worked great!
$form['field_business_name']['widget'][0]['value'][#states] = [
'visible' => [
'select[name="field_location_type"]' => ['value' => 1],
],
];
"but" trying to use the same format for the controller with a media field (using the media library widget) would not work.. For example:
$form['field_media_video_linked'][#states] = [
'invisible' => [
'input[name="field_media_video[selection][0][target_id]"]' => ['!value' => ''], // value not empty
],
];
However if the input was wrapped in another array (nested) it does work like this:
$form['field_media_video_linked'][#states] = [
'invisible' => [
['input[name="field_media_video[selection][0][target_id]"]' => ['!value' => ''],],
],
];
But without the nesting it doesn't work?
That struck me as odd and was the only reason I found that to work because I added an 'or',
to the controller which required I wrap or nest the different control options in another array (as I was testing just using another input from a different "none-media" field to at least make sure I could use states to hide media fields, which does work)
So as of now using one media field to hide another media field works except if the control field/input is removed by clicking "Remove" on the media item the hidden media fields no-longer show and won't return to a visible state "and" I believe that is because when you remove an added media item the input input[name="field_media_video[selection][0][target_id]"]
gets removed from the form as well and since it no-longer exists there is nothing to check that the value is now empty. Frustrating...
So using an 'or',
with 'visible'
for 'input[name="field_media_video[selection][0][target_id]"]' => ['value' => ''], //value is empty
Does nothing!
I'm either addressing this the wrong way or there is a better way to work with states on media fields when the conditional controller is also a media field (that uses the media library widget) and I'd really like to keep using the media library widget, even tho I did find I could switch the widget type to "Inline Entity Form Simple" and I could then use 'input[name="field_media_video[0][field_media_video_file][0][fids]"]' => ['!value' => ''], //not empty
as the controller to make other fields hidden/invisible and then 'input[name="field_media_video[0][field_media_video_file][0][fids]"]' => ['value' => ''], //is empty
to then make them visible again, being the actual input is always in the form (whereas with Media Library widget no input exists until an item/file is added. If all else fails I'll have no choice but to switch the widget type to make this work but it's not what I want to do as there are added benefits to using the library widget as a user could select an uploaded file that "already exists" in their account versus uploading the same content to a new field.
I've spent hours looking through documentation and there is literally nothing for examples or use cases with media related fields when dealing with states.
So, is there a better way "or" what do I need to do to get this working with the "expected" results (keeping the field widgets as media library)?
I can provide additional information if needed, I use dpm() to get the $form, $form_state and field info for the form on load.