How to ajax pre populate link field from entity reference to another link field ?

Created on 9 December 2022, about 2 years ago
Updated 28 July 2024, 6 months ago

Problem/Motivation

I would like to populate link field from entity reference to another link field in my content edit.

Steps to reproduce

Create a content, add entity reference field from another content with link link field referenced by view.
Create module for ajax populate link field.

Here is my module code :

function _injector_autofill_fields($form, $form_state) {
  $eid = $form_state->getValue('field_entreprises_marque')[0]['target_id'];
  $event = Node::load($eid);
  $entreprise   = $event->title->value;
  $phone        = $event->field_telephone->value;
  $email        = $event->field_courriel->value;
  $adresse      = $event->field_adresse->value;
  $code         = $event->field_code_postal->value;
  $ville        = $event->field_ville->value;
  $web          = $event->field_site_web->value;
  
  $response = new AjaxResponse();
  $response->addCommand(new InvokeCommand('#edit-field-telephone-0-value', 'val', [$phone]));
  $response->addCommand(new InvokeCommand('#edit-field-courriel-0-value', 'val', [$email]));
  $response->addCommand(new InvokeCommand('#edit-field-adresse-0-value', 'val', [$adresse]));
  $response->addCommand(new InvokeCommand('#edit-field-code-postal-0-value', 'val', [$code]));
  $response->addCommand(new InvokeCommand('#edit-field-ville-0-value', 'val', [$ville]));
  $response->addCommand(new InvokeCommand('#edit-field-nom-de-l-entreprise-0-value', 'val', [$entreprise]));
  $response->addCommand(new InvokeCommand('#edit-field-site-web-0-value', 'val', [$web]));
  return $response;
}

I know that i can't do like this but, how to do it for :
$web = $event->field_site_web->value;
AND
$response->addCommand(new InvokeCommand('#edit-field-site-web-0-value', 'val', [$web]));

Thanks in advance!

πŸ’¬ Support request
Status

Closed: outdated

Version

11.0 πŸ”₯

Component
AjaxΒ  β†’

Last updated 1 day ago

Created by

πŸ‡«πŸ‡·France mankin

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024