1

Using Drupal 8...

I have a content type, called "Qualification".

In Qualification is an entityreference field called "Specification Sections" (field_specification_sections).

The entityreference (field_specification_section) field uses an inline entity form to create a node of type "Specification Text Section" (specification_text_section).

In other parts of my site, I need to be able to call the 'parent' of Specification Text Section, so there is a entityreference field called that refers back to qualification.

I need to prepopulate, the field_qualification entityreference, when I create the Specification Text Section.

I've been struggling with using hook_inline_entity_form_entity_form_alter, trying to get the field_qualification to prepopulate.

I am currently using:

function mymodule_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
     // get qualification node id from request
     $nodeId = \Drupal::request()->attributes->get('node');

     // load the node entity
     $node = is_numeric($nodeId) ? \Drupal\node\Entity\Node::load($nodeId) : null;

     if ($entity_form['#entity_type'] == 'specification_text_section'){
           $form['field_qualification']['widget'][0]['target_id']['#default_value'] = $node;

    }
}

But, this is not prepopulating the field in the inline entity reference.

If I still had hair, I'd be pulling it out!

Can anyone point me in the right direction?

Thanks

  • The first line loads a node $node = \Drupal::request()->attributes->get('node');, so the second line doesn't work, results in NULL, although you have already the node and don't need this line. – 4k4 Jan 28 '18 at 9:03
  • OK. I can see where my second line trying to load the node is unnecessary, but I'm still not getting the field_qualification reference to prepopulate. Is the line $entity_form['field_qualification']['widget'][0]['target_id']['#default_value'] = $node['nid']; correct? – gmak Jan 28 '18 at 11:51
  • Why you've changed it? The first version of this line seems to be correct (concerning $node, can't check the array path), see api.drupal.org/api/drupal/… – 4k4 Jan 28 '18 at 13:40
1

I tend to use CER (Corresponding Entity References) for things like this.

As an added benefit, you can hide the reference field on specification_text_section giving a slightly cleaner editor experience.

I have a couple of similar setups where I have IEFs on the grandparent, parent, and child, so you can create all 3 from any level and CER handles reverse reference. So, if I create a parent node, I have 2 IEFs, one for the grandparent and child, but don't show the field referencing the parent in either IEF forms.

1

Since I was, ultimately, wanting to hide the reverse reference from the user, I decided to use a hook_entity_presave() function.

This is what I ended up with:

$parent = \Drupal::routeMatch()->getParameter('node');
if ($parent instanceof \Drupal\node\NodeInterface) {
     $nid = $parent->id();
     }
//if the field is not already filled...
if( empty($entity->field_qualification->target_id) ){
     $entity->field_qualification->target_id = $nid;
     }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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