I would like to alter an entity-create/edit form, but only when it's used as an entity form in an entity browser. I've tried a hook_form_alter and a hook_form_FORM_ID_alter, but haven't found the built-out form array to alter, containing the actual fields of the target entity. What I specifically want to do is set the value of a select list and hide it. Could someone point me in the right direction for doing this type of alteration?

Comments

nadavoid created an issue. See original summary.

nadavoid’s picture

Issue summary: View changes
smartparty’s picture

Solution below worked out using form pre render.

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) {
	/* dpm($element); */
	/* examples */
	$element['widget']['inline_entity_form']['title']['widget'][0]['value']['#title'] = t('This is a test');
	$element['widget']['inline_entity_form']['title']['widget'][0]['#prefix'] = t('This is a test');
	$element['widget']['inline_entity_form']['title']['widget'][0]['value']['#maxlength'] = 9;
	$element['widget']['inline_entity_form']['field_my_field']['widget'][0]['value']['#value'] = t('This is a test');
	$element['widget']['inline_entity_form']['field_my_field']['widget'][0]['value']['#default_value'] = t('This is a test');
	$element['widget']['inline_entity_form']['field_my_field']['widget'][0]['#access'] = FALSE;  
	return $element;
}
karthikeyan-manivasagam’s picture

#3 is not working for entity browser (EB) have two tabs one for selecting entities from EB and another to create a new entity from EB and the above method is not working

chucksimply’s picture

I had a similar issue... and solved it myself by altering the inline entity form instead of the entity browser. Hope it helps.

https://www.drupal.org/project/entity_browser/issues/3198075