1

I have an entity ref autocomplete field (reference to bundle A) in one of my bundles (bundle B). I want to add a button below this field which will open a modal for a bundle A create form.

This code works great:

  $form['#attached']['library'][] = 'core/jquery.form';
  $form['#attached']['library'][] = 'core/drupal.ajax';
  $form['field_ref_to_bundle_a']['#suffix'] = '
    <div><a href="/node/add/bundle_a"
      class="use-ajax button"
      data-dialog-type="modal"
      data-dialog-options="{&quot;width&quot;:800,&quot;height&quot;:500}">' .
        t('Add New Bundle A') .
    '</a></div>';

When I hit Create in the modal, the new entity is created; but I need to assign this new entity to the field on the parent form (bundle B). I have added a #submit callback with a form alter on the modal form and in that callback I have:

  $response = new AjaxResponse();
  $response->addCommand(new CloseModalDialogCommand());
  $form_state->setResponse($response);

which closes the modal. In this call i know the NID of the newly created entity and I am guessing there must be some way to set the value on that field in the parent form.

Although i don't think it should be necessary, i have seen code suggesting i can use a $response->addCommand(new ReplaceCommand()) to swap out the entire original field with one that is populated with the default value; but i have had no luck in doing this. I have been able to use this $response call to swap out the entire autocomplete with a text string; so does seem like i am on the right path. All i really need to do is set the default value of the autocomplete field; but not sure how to do this through ajax.

Going with the idea that i need to replace the entire field (as opposed to just setting the default value); i found this code:

  $node = Node::load($nid);
  $elem = [
    '#type' => 'entity_autocomplete',
    '#target_type' => 'node',
    '#default_value' => $node,
    '#attributes' => [
      'id' => ['#edit-field-ref-to-bundle-a-0-target-id'],
    ],
  ];
  $renderer = \Drupal::service('renderer');
  $renderedField = $renderer->render($elem);

Although this does render something:

<div class="js-form-item form-item js-form-type-entity-autocomplete form-type-entity-autocomplete js-form-item- form-item- form-no-label">
<input id="#edit-field-ref-to-bundle-a-0-target-id" type="text" size="60" maxlength="128" class="form-text" />
</div>

It doesn't have a default value set. But perhaps I am close??

1

Figured out it was the InvokeCommand i needed and to set "val" to the referenced entity title with id in ().

  $nid = $form_state->getValue('nid');
  $node = Node::load($nid);
  $value = $node->getTitle() . ' (' . $nid . ')';

  $response = new AjaxResponse();
  $response->addCommand(new CloseModalDialogCommand());
  $response->addCommand(new InvokeCommand('#edit-field-ref-to-bundle-a-0-target-id', 'val', [$value]));
  $form_state->setResponse($response);
| improve this answer | |

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.