How to populate text field from entity reference

Created on 11 June 2023, over 1 year ago
Updated 15 September 2023, over 1 year ago

Problem/Motivation

I have two element:
1. Entity reference autocomplete, will populate from Username (value from field name in user entity) Views Entity Reference.

By inputing this Entity Reference Outocomplete I can select username followed with field_first_name value.

2. Text Field (hopefully populated from Users field_first_name).

I know in advamced tabs, we can set default value using tokens, but i can not find any token matches with point 2 above.

Anyone please assist me how to achive it?

Thanks you before.

💬 Support request
Status

Closed: won't fix

Version

6.2

Component

Code

Created by

🇮🇩Indonesia tinohuda

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

Comments & Activities

  • Issue created by @tinohuda
  • Status changed to Closed: won't fix over 1 year ago
  • 🇺🇸United States jrockowitz Brooklyn, NY

    The feature is not available. You would have to write custom Ajax code to accomplish it.

  • 🇮🇩Indonesia tinohuda

    Hi @jrockowitz thank for your reply.
    i'm not a programmer, i try to add this in Webform > Settings > CSS/JS:

    jQuery(document).ready(function() {
      // Get the autocomplete element and the textfield element.
      var autocompleteElement = document.getElementById('edit-username-autocomplete');
      var firstNameElement = document.getElementById('edit-field_first_name');
    
      // When the user selects a username in the autocomplete element,
      // update the first name element with the user's first name.
      autocompleteElement.addEventListener('change', function() {
        var username = autocompleteElement.value;
        var user = Drupal.user.load(username);
        if (user) {
          firstNameElement.value = user.field_first_name;
        }
      });
    });

    But i got this error in console:
    Uncaught TypeError: Cannot read properties of undefined (reading 'load')
    at HTMLInputElement.

    I hope you can help ;-)

    and thank you again.

  • For help with programming, look into the resources I provided in comment #2.

Production build 0.71.5 2024