I am using inline entity forms to create a paragraph in a node form. When I add a node I want the title of the node form to be copied to the paragraph when I click on "Add paragraph". I use hook_inline_entity_form_entity_form_alter to prefill the title field. That works, only I can't get the values from the node form. Not with the below code.

function example_forms_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
  $node = $form_state->getFormObject()->getEntity();
  if ($node->getType() == "task" && $entity_form['#bundle'] == "improvement") {
    \Drupal::logger('Form state values')->debug("<pre>" . print_r($form_state->getValues(), TRUE) . "</pre>");
    \Drupal::logger('Node values')->debug("<pre>" . print_r($node, TRUE) . "</pre>");
    $entity_form['title']['widget'][0]['value']['#default_value'] = 'This is Jessica';

Can someone help me solve this?

  • Did you find a solution? – Orion Feb 18 at 21:37

Perhaps you should try altering the paragraph form instead. When the paragraph is created, you will have access to it's host entity. Then just get the label of the host entity and assign it to the form element as a default value or value(can't remember which one).

  • Thanks for your suggestion, but the paragraph is not actually made yet. It's when filling in the form. – Jessica IX Jul 24 '18 at 21:02
  • When you're using the form alter for the paragraph, even if it's not created, it will still have the host entity set in the object that later on will be saved as a paragraph. – Postovan Dumitru Jul 25 '18 at 8:51
  • 1
    The parent object is empty. Also when I try with an entity reference which is a node. Any other suggestions? – Jessica IX Aug 5 '18 at 3:41

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.