Adding Fields to Inline Entity Form Table
Inline Entity Form is a useful module for reference entities and being able to edit them in place.
Here’s what the edit form looks like out of the box for an unlimited value entity reference:
Often it’s helpful to provide additional information to your editors.
If you have a look at inline_entity_form.module you will find a function called theme_inline_entity_form_entity_table(). Within this you will see how this table is built, and how you can manipulate the form to add additional columns of information.
Here’s an example (mymodule.module) showing how I am able to add two additional fields to the table:
- sow = top level content type
- field_sow_er_lis = entity reference field in sow content type that references sowli nodes
- sowli = content type being referenced
- field_sowli_timeline = term reference field in sowli content type
- field_sowli_source = term reference field in sowli content type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
use Drupal\taxonomy\Entity\Term; /** * Implements hook_form_FORM_ID_alter(). */ function mymodule_form_node_sow_edit_form_alter(&$form, &$form_state, $form_id) { // See inline_entity_form.module theme_inline_entity_form_entity_table(). $cols = &$form['field_sow_er_lis']['widget']['entities']['#table_fields']; $cols['field_sowli_timeline'] = [ 'type' => 'field', 'label' => t('Timeline'), 'weight' => 105, ]; $cols['field_sowli_source'] = [ 'type' => 'field', 'label' => t('Source'), 'weight' => 106, ]; $keys = Element::children($form['field_sow_er_lis']['widget']['entities']); if (!empty($keys)) { foreach ($keys as $key) { $entity = &$form['field_sow_er_lis']['widget']['entities'][$key]; if ($timeline_tid = $entity['#entity']->field_sowli_timeline->target_id) { $entity['field_sowli_timeline'] = [ '#markup' => Term::load($timeline_tid)->getName(), ]; } if ($source_tid = $entity['#entity']->field_sowli_source->target_id) { $entity['field_sowli_source'] = [ '#markup' => Term::load($source_tid)->getName(), ]; } } } } |