When using the Inline Entity Form module (IEF) to load a standard user form for a node content type (not commerce related), I found that IEF does not appear to respect the "hidden" field settings from "Manage Display" settings on the content type definition. Based on my research it appears this is by design, and IEF offers a hook_alter function to manipulate these forms instead. Within the IEF module folder there is an API file (inline_entity_form.api.php) that documents available hooks, linked here for reference.
In my case, I just wanted to hide two fields from users when exposed through a normal user form with IEF. In the example* below, we 1) define the form alter function for our own custom module; 2a) check that we are dealing with a node entity and 2b) that the bundle is the correct content type, then we 3) unset the two fields, which removes them from the user form completely. The hook_alter allows access to the entire from object, so we could just as easily have changed the field label, set a default field value, or any other field manipulation you can do through a form alter.
function my_module_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) { // use this at most one time per module
// conditional statement targets one specific content type's inline entity form.
if ($entity_form['#entity_type'] == 'node' && $entity_form['#bundle'] == 'my_content_type_machine_name') {
unset($entity_form['field_first_name']); // remove "first name" field from inline entity form
unset($entity_form['field_last_name']); // remove "last name" field from inline entity form
}
}
*This assumes you already have a good understanding of custom modules and the use of form alters, which is beyond the scope of this thread. Suffice it to say that the code sample above would need to go into your own custom module, and aspects of it would need to be modified according to your use case, module name, entity type(s), fields, etc. (It's easier than it sounds). Also note that only one category of form alter is allowed per module, so if you wanted to modify multiple inline entity forms from a single custom module, you would use the same single function call, but use conditional statements within it in order to discern between forms.
It took me a little bit of research to resolve my related issue, so I wanted to create this issue thread for future users. I apologize if this is a dup.
Comments
Comment #1
jweirather commentedComment #2
jweirather commentedComment #4
joshtaylor commentedYou should use:
$entity_form['field_foobar']['#access'] = FALSE;Instead as this will hide the field from access.
Comment #5
martin_klimaI disagree with #4.
$entity_form['field_foobar']['#access'] = FALSE;not work in this hook.unset($entity_form['field_foobar']);work.Comment #6
martin_klimaExample:
Comment #7
jiri.motejlek commented#4 works with 1.8 and is a bit more flexible therefore should be the preferred choice.
Comment #8
scotwith1tonly unset works for me as well. perhaps it depends on the context, but my issue was with drupal commerce, the inline entity form for adding line items to an order. i am also trying to add data from a related entity and haven't figured out how to do so...any tips? for instance, i know i can add the commerce_product field and specify the formatter, but what about accessing a specific property of the related entity?
Comment #9
matsjacobsson commentedThis code works for Drupal 8 too.. Except with using title as:
$fields['label']['label']But is it the right way to go with Drupal 8?
Comment #10
charlie1volley commentedHi,
Thanks to those for posting this code, has helped me greatly. Just want to point out the hook in the original post (hooks the form) is a different hook than in #6 (hooks the table). Both sets of code are working in my 8.8.5 installation. I am using 'unset' to hide fields.
Complete documentation can be found at https://git.drupalcode.org/project/inline_entity_form/-/blob/8.x-1.x/inl.... -c