3

I'm using inline entity form on a node edit form. The target is a custom entity.

There are two possible form widgets to use: Simple or Complex.

With Simple (example) the child entity fields are just embedded in the parent form. It's not possible to collapse the child fields, nor is it possible to delete child entities, but it does save the child entities upon submitting the entire node edit form.

With Complex (example) the child entities fields are collapsible and the child entities can be deleted. However the child entity is only saved if the section of the form is not collapsed when the node edit form is submitted. Is this a bug?

I've discovered that using hook_inline_entity_form_entity_form_alter I can add a callback to the $form['#entity_builders'] array that is called when the child entity sub form is collapsed (and is meant to be saved).

In this callback I've added some super generic code to just save the child entity.

E.g.

function save_ief_child_entity($entity_type, $entity, $entity_form, $form_state) {

  $delta = $entity_form['#ief_row_delta'];
  $field_name = $entity_form['#parents'][0];
  $values = $form_state->getValue($field_name);

  // The location of the entity values changes depending on if it's a new save
  // or an update.
  if(isset($values['form']['inline_entity_form']['entities'][$delta]['form'])) {
    $values = $values['form']['inline_entity_form']['entities'][$delta]['form'];
  }
  else {
    $values =& $values['form']['inline_entity_form'];
  }

  if(empty($values)) {
    return;
  }

  // Loop form values to update entity
  foreach($values as $key => $value) {

    // Images are in the wrong format for some reason - fix it.
    foreach($value as &$part) {
      if(is_array($part) && isset($part['fids'])) {
        $part['target_id'] = $part['fids'][0];
      }
    }

    $entity->{$key} = $value;

  }

  $entity->save();

  // IEF takes care of creating the relationship between parent and child when the main form is submitted
}

Why do I have to do this / why doesn't IEF do this itself internally?

5
  • Did you get anywhere with this? Just found the same bug. Nov 27, 2019 at 4:12
  • No - not revisited this since then I'm afraid.
    – Felix Eve
    Nov 27, 2019 at 7:57
  • Did this code snippet do the trick for you? Does it go in '.theme' file? Nov 27, 2019 at 12:02
  • Yes, this snippet fixes the problem. This doesn't really have anything to do with theming so I would reccomend putting it in a custom module. You can put the hook inline entity form alter in there too.
    – Felix Eve
    Nov 27, 2019 at 12:57
  • Right on. This should be a patch. Nov 27, 2019 at 12:58

1 Answer 1

1

If anyone would stumble on the issue of saving child nodes (entity) at inline entity form (ief) - newly added/created node not saved, here is a quickfix for that (D8/9):

function MODULENAME_inline_entity_form_entity_form_alter(&$entity_form, FormStateInterface $form_state) {
    $entity_form['#save_entity'] = true;
}

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.