Hi,

I have a content-type B (title , field_b_description) referenced in content-type A.
I am creating a multistep form ( form A ) with a preview page on the last step before submit.

For that, I am trying to get data from form_state but i do not find entity B (referenced in form A with a complex IEF widget) values.

I have tried using form_state->getValues() within hook_inline_entity_form_entity_form_alter but i steel not find referenced values (title, field_b_description) ?

Any ideas about inline_entity_form' s value access?

Think you for your help ans sorry for my english :)

Comments

gomezia created an issue. See original summary.

init90’s picture

Hi! Maybe this helps you:

$form_state->getFormObject()->getEntity();
gomezia’s picture

@DovganPavlo Thinks a lot for your suggestion but it returns only the parent entity values not referenced one.

I resolved my problem as follow :

foreach ($form_state->get('inline_entity_form') as &$widget_state) {
      //dpm($widget_state);
      $widget_state += ['entities' => []];

        foreach ($widget_state['entities'] as $delta => $entity_item) {
          if (!empty($entity_item['entity']) && !empty($entity_item['needs_save'])) {
            /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */

            // for nested entities, get entity bundle  example: referenced entity B, referenced entiy C in B ... 
            $entity_type = $entity_item['entity']->getType();

            if (!empty($entity_type)) {
              $inline_entities[$entity_type][$delta] = $entity_item['entity'];
            }          
            //dpm($inline_entities);
            
          }
        }
    }

Now, to access end values, use
$entity->get('field')->getValue()[0]['value'];
Example for titles:

$entity->get('title')->getValue()[0]['value'];

Hope it helps others.

gomezia’s picture

Correction of my past code:

foreach ($form_state->get('inline_entity_form') as &$widget_state) {
      //dpm($widget_state);
      $widget_state += ['entities' => []];

        foreach ($widget_state['entities'] as $delta => $entity_item) {
          if (!empty($entity_item['entity'])) {
            /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */

            // for nested entities, get entity bundle  example: referenced entity B, referenced entiy C in B ... 
            $entity_type = $entity_item['entity']->getType();

            if (!empty($entity_type)) {
              $inline_entities[$entity_type][$delta] = $entity_item['entity'];
            }          
            //dpm($inline_entities);
            
          }
        }
    }
gomezia’s picture

Category: Bug report » Support request
gomezia’s picture

Status: Needs work » Needs review
dawehner’s picture

Status: Needs review » Fixed

Another alternative is to go to the actual form element and then choose $element['#entity'], but yeah go with what you work.

When you want to adapt the behaviour of the IEF form I recommend you to try to use either a custom widget or even a custom form element type, depending on which level of the stack you have to change. This will give you the best flexibility while not having to deal with too many dirty details.

Marko B’s picture

Sorry for this, I am paying beer in drupalcon Dublin to anyone helping me with this :)

I am seasoned drupal 7 dev, but I am new to D8 and this is a bit confusing to me.
So how do you even get to an idea to use "get" on form_state and do $form_state->get('inline_entity_form')
what is the mindset to do that. I was looking at form_state a lot and couldnt figure out what to do with it and how to get this values. After that I see some looping, mostly standard thing.

Also wondering what is the idea that dawehner refered to with $element['#entity'], what would be approach here.

What I am trying to do is basically get IEF element values, like node name and use it in form validate function on node form. This above code works, but I am a bit dazzled how to think in drupal 8 way, so please give me some pointers.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.