1

My end goal is to put the Inline Entity Form inside of a custom widget, and have it submittable without submitting the parent form inside of which it has been placed.

I'm not sure how to do this programmatically. I looked for an example in the tests/ subdirectory of IEF, and found a form like this:

$form['inline_entity_form'] = [
  '#type' => 'inline_entity_form',
  '#entity_type' => 'node',
  '#bundle' => 'ief_test_custom',
  '#form_mode' => $form_mode,
];
$form['submit'] = [
  '#type' => 'submit',
  '#value' => t('Save'),
];

But if I take this code and put it inside an existing form via hook_form_alter or a custom widget, it will submit the entire form, when in reality I just want it to save the values for the reference. I also tried to experiment by putting a rendered form into an iframe, but it wouldn't load the form code inside it.

$form = \Drupal::formBuilder()->getForm(\Drupal\user\Form\UserLoginForm::class);

$ief = [
  '#type' => 'inline_entity_form',
  '#entity_type' => 'node',
  '#bundle' => 'article',
  '#default_value' => \Drupal\node\Entity\Node::load(1),
  '#form_mode' => 'default',
];

$element['inline_entity_form'] = [
  '#type' => 'inline_template',
  '#template' => '<iframe srcdoc="<div id="iframe-ief">{{ ief|escape }}</div>"></iframe>',
  '#context' => [   // Tried the above with & without |escape
    'ief' => $form, //<--tried both $form and $ief here.
  ],
];

Is there any example of doing this that I can look for, or any suggestions or which direction to look?

1

i would do it via ajax submit handler. yes you will recieve the full node but you can just take the necessary values and save it directly yourself.

see this post for how to do ajax in forms: ajax_example_add_more - Drupal 8

0

I ended up making a Form like this:

public function buildForm(array $form, FormStateInterface $form_state, $form_mode = 'default') {
  $node_id= \Drupal::request()->get('node_id');

  $form['ief'] = [
    '#type' => 'inline_entity_form',
    '#entity_type' => 'node',
    '#bundle' => 'article_reference',
    '#form_mode' => $form_mode,
    '#default_value' => \Drupal\node\Entity\Node::load($node_id),
    '#save_entity' => true,
  ];
  $form['ief_submit'] = [
    '#type' => 'submit',
    '#value' => t('Save'),
    '#ajax' => array(
      'event'=>'click', 
      'callback' => '::ajaxSubmit',
      'name' => 'submit1',
     ),
  ];
  return $form;
}

public function ajaxSubmit(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $response = new AjaxResponse();

  if ($form_state->getErrors()) {
  }
  else {
    $response->addCommand(new \Drupal\Core\Ajax\CloseModalDialogCommand());
  }

  return $response;
}

/**
 * {@inheritdoc}
 */
public function submitForm(array &$form, FormStateInterface $form_state) {
  \Drupal\inline_entity_form\Element\InlineEntityForm::submitEntityForm($form['ief'], $form_state);
  drupal_set_message(t('Updated @entity_type @label.', ['@entity_type' => $form['ief']['#entity']->getEntityType()->getLabel(), '@label' => $form['ief']['#entity']->label()]));
}

And then called that form (by it's route in the routing.yml) using the modal method I found here: http://www.innoraft.com/blog/popups-drupal-8-no-problem

If anyone has a more Drupal-y way to do this, feel free to let me know. But, I like the modal, it feels very clean.

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.