2

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?

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

2 Answers 2

0

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).

3
  • Thanks for your suggestion, but the paragraph is not actually made yet. It's when filling in the form.
    – Jessica IX
    Jul 24, 2018 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. Jul 25, 2018 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, 2018 at 3:41
0

This is a quite old question, I'm not sure if it's still relevant but you can use parent node title from the userInput array.

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") {
    $userInput = $form_state->getUserInput();
    $entity_form['title']['widget'][0]['value']['#default_value'] = $userInput['title'][0]['value'];
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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