-3

I am trying to hide entity id from my autocomplete fields. I downloaded a jquery script that removes the bracketed id and updates the autocomplete element with the new element. It then writes the entire value to the element's data like this

$(event.target).data('real-value','6');

I even updated this to use attr instead of data, I cant read this information from my Drupal form_state.

I set a data attribute on my autocomplete element using jquery like this

$(event.target).attr('real-value','6');

How do I get this value from my $formstate or $form object in my form submit function? Using dpm($form['elementname']) doesn't even show this atrribute.

I've tried $formstate->getValue(). Doesn't work?

Here's the script I'm using...

hide autocomplete id jquery

Just wondering why does the jquery successfully change the value of the autocomplete textbox, but cant change an attribute on the same element?

3
  • 2
    Because JavaScript added the attribute I don't see how Drupal's Form system would know about it.
    – cilefen
    Apr 4, 2022 at 19:17
  • Thanks Cilefen. But I've also tried adding the attribute to the form element using Drupal '#attributes => ['test'] and then tried changing attribute value in jquery. But formstate still returns 'test' as the value.
    – ilechcod
    Apr 4, 2022 at 20:28
  • 1
    If you have tried this with different code then that is a different question. Also data attributes are not data attributes if they do not begin with data-. developer.mozilla.org/en-US/docs/Learn/HTML/Howto/…
    – cilefen
    Apr 4, 2022 at 20:40

1 Answer 1

1

This is impossible as written. Drupal's Form system does not have information about attributes added solely by JavaScript.

Drupal's Form system does not have information about attributes modified solely by JavaScript.

9
  • Thanks Cilefen, but this obviously doesn't help me at all. I need to know how to modify existing form element attributes and read it from Drupal 8. After all, ajax is commonly used to replace a textfield value using jquery. Pls how can I achieve this result?
    – ilechcod
    Apr 4, 2022 at 21:10
  • Your answer does not point me in the right direction of how to achieve my aim.
    – ilechcod
    Apr 4, 2022 at 21:11
  • This answer is correct: In Drupal, AJAX used in a form still involves PHP code. PHP won't know about an attribute added by JavaScript code.
    – apaderno
    Apr 4, 2022 at 21:58
  • 1
    The question asks how to do this with JavaScript in a specific way. I answered rightly that it is not possible. Ask a different question for a different answer. I don’t know what you are actually trying to do.
    – cilefen
    Apr 4, 2022 at 22:57
  • 1
    @ilechcod it feels like you might be fundamentally misunderstanding how HTML forms work - all that can be submitted is the current value of an input element, arbitrary attributes like the ones you’re adding are never submitted. You can do anything you want with JS to manipulate values before they’re sent of course, but a browser that’s behaving itself will never try to send attributes to the form action
    – Clive
    Apr 5, 2022 at 17:20

Your Answer

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

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