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
$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.field_qualification
reference to prepopulate. Is the line$entity_form['field_qualification']['widget'][0]['target_id']['#default_value'] = $node['nid'];
correct?