6

I've just started with Inline Entity Form and seem to have it working with a custom entity type that has fields. My problem is, none of these fields show in the Inline Entity Form widget, it just shows an empty ID column and Edit / Delete buttons:

enter image description here

I've looked through what little documentation there is, and browsed related issues and questions, but they all seem to be doing things more complex than what I'm trying to do.

Is there any simple way to just choose a field or two to display in the Inline Entity Form widget?

It seems to "just work" for nodes - how can I have it pick up the entity fields in a similar way?

enter image description here

4 Answers 4

8

Use hook_inline_entity_form_table_fields_alter() this is what I am using on a current site:

function MY_MODULE_inline_entity_form_table_fields_alter(&$fields, $context) {
  //Determine the bundle and entity type from $context

  unset($fields['id']); //<get rid of the id field

  //add any fields you need to the $fields array
  $fields['FIELD_NAME'] = array(
    'type' => 'field',
    'label' => t('FIELD'),
    'weight' => 2
  );

}
5

To add to Mojeferous' answer, you do this indeed by implementing hook_inline_entity_form_table_fields_alter() in your module which is documented in the modules' inline_entity_form.api.php.

You can add properties, custom fields or call backs as columns. See the below example.

Note: to prevent error messages, check for the entity types.

function custommodule_inline_entity_form_table_fields_alter(&$fields, $context) {
  if ($context['parent_entity_type'] == 'customentity' && $context['entity_type'] == 'anothercustomentity') {
    $fields['field_custom'] = array(
      'type' => 'field',
      'label' => t('Custom'),
      'weight' => 101,
    );
    $fields['title'] = array(
      'type' => 'property',
      'label' => t('Title'),
      'weight' => 1,
    );
  }
}

See: https://www.drupal.org/node/2091585 and https://www.drupal.org/node/2497289

1
5

I made a module - IEF Table View Mode that can help you with that using the view mode

This module defines a view mode to set up the columns of the table for the Inline Entity Form widget.

With this you could define which fields (with their settings) and in what order will be displayed in the table. Also includes the properties defined by the entity and IEF.

0

In case anyone is looking to output a taxonomy term ID...

/**
 * Implements hook_inline_entity_form_table_fields_alter().
 */
function HOOK_inline_entity_form_table_fields_alter(array &$fields, array $context) {
  if ($context['parent_entity_type'] === 'taxonomy_term') {
    $fields['tid'] = [
      'type' => 'field',
      'label' => t('Term ID'),
      'weight' => 2,
    ];
  }
}

Use of the type property threw me off as the latest version of the code does not actually have a condition that looks for property in type. See: https://git.drupalcode.org/project/inline_entity_form/-/blob/8.x-1.x/inline_entity_form.module#L393

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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