2

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

3
  • 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. Commented Jan 28, 2018 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? Commented Jan 28, 2018 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/… Commented Jan 28, 2018 at 13:40

2 Answers 2

2

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
  • I've been using Drupal for 10 years and never seen this module :-) this solved a similar issue for me. Commented Dec 4, 2021 at 23:24
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; } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.