Problem/Motivation

Currently the IEF simple widget does not provide an 'instance' property in the form_state storage (like the complex widget does) and doesn't make it's `ief_id` value availible to the `inline_entity_form` element it builds (like the complex widget does).

This means that it is imposible to reference the correct widget state for an simple `inline_entity_form` in `hook_inline_entity_form_entity_form_alter`, and even if you can there if no information about the inplimenting field avalible as the 'instance' propery is missing.

Proposed resolution

Allow the IEF Simple widget to set the correct ief_id on the `inline_entity_form` element and provide a 'instance' property in the widget storage.

Once the cahnges have been implements developers will be able to do the following:

/**
 * Implements hook_inline_entity_form_entity_form_alter()
 */
function hook_inline_entity_form_entity_form_alter(array &$entity_form, FormStateInterface &$form_state) {
  // Currently, can't do this for IEF Simple widgets
  $widget_state = $form_state->get(['inline_entity_form', $entity_form['#ief_id']]);

  // Currently, no easy way to access the definition of the field responsible for this inline_entity_form
  $field_definition = $form_state->get(['inline_entity_form', $entity_form['#ief_id'], 'instance']);
  if ($field_definition->getName() === 'field_custom') {
    // Do stuff..
  }
}
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

lind101 created an issue. See original summary.

lind101’s picture

Title: Make IEF Simple Widget set ief_id on inline_entity_form element » Make IEF Simple Widget set ief_id on inline_entity_form element and provide instance context via form_state storage
Issue summary: View changes
lind101’s picture

Title: Make IEF Simple Widget set ief_id on inline_entity_form element and provide instance context via form_state storage » Make IEF Simple Widget set ief_id on inline_entity_form element and provide instance context in widget state
lind101’s picture

Issue summary: View changes
lind101’s picture

Title: Make IEF Simple Widget set ief_id on inline_entity_form element and provide instance context in widget state » DX improvements to the Simple Widget State and it's accessibility
Issue summary: View changes

lind101’s picture

Status: Active » Needs review

MR in #6 privides the DX improvements mentioned on the ticket.

lind101’s picture

Issue summary: View changes
geek-merlin’s picture

Status: Needs review » Needs work

Thanks for raising this and providing a MR!

Let's take this apart...
# IefId
So you say the complex widget provides this in the $entity_form array, but not the simple, and we can fix this if we set $this->iefId?

If yes, that's uncontroversal, and i dimply remember which commit killed that. Can you provide a test for both widgets so this won't break in the future?
You can dig through the existing tests and add a method to one that seems fit.

# field instance
I'm a bit hesitant to add new APIs, as we must support them forever, and forever is a quite long time, esp. towards the end.
That said, i looked into this and don't have an idea how that use case can be done today (at least without killing lots of kittens).
So yes, so be it.

Can you
- first finish a MR with only the iefId fix, and test
- move the other to a second issue and MR
- add a test for that too?

Tha'd be great.