0

Need to unset a widget tab from media browser based on the condition of media type supported by the field.

Media types: Image and Video.

For example, There is a field which references image media type only. When the media browser appears on the form, it displays create embed tab as well to embed the videos.

As the field doesn’t reference the video media type, need to unset the tab programmatically to help the content author.

I have tried the form alter, but I don't get any information about the media type referenced by the field in the hook.

1

It should be possible with some code like this:

function MY_MODULE_form_entity_browser_MY_FORM_form_alter(&$form, FormStateInterface $form_state, $form_id) {   
  $form['#pre_render'][] = 'MY_MODULE_pre_render';  
}

function MY_MODULE_pre_render($element) {
  kint($element); //Find the tab you want to remove.
  if (...) {
    unset($element['...']);
  }
  return $element;
}

Another commonly used solution is to create 2 or 3 media browsers depending on your needs. So you will have an image media browser, a video media browser and an image + video media browser.

Then you just select which one to use in the field settings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.