Score:-3

How do I read element attribute from $form_state?

in flag

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?

id flag
Because JavaScript added the attribute I don't see how Drupal's Form system would know about it.
ilechcod avatar
in flag
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.
id flag
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-`. https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes
Score:1
id flag

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.

ilechcod avatar
in flag
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 avatar
in flag
Your answer does not point me in the right direction of how to achieve my aim.
apaderno avatar
us flag
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.
id flag
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.
id flag
I’m just guessing but you may need the Ajax API https://www.drupal.org/docs/drupal-apis/javascript-api/ajax-forms
ilechcod avatar
in flag
Thanks Cilefen. I have updated the question to show exactly what I am trying to do. Also I cant do this using Chosen Ajax since Im on Drupal 8. Thanks
cn flag
@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
ilechcod avatar
in flag
@Clive - so in other word, it's not possible to store something on an existing element attribute using jquery and then read the attribute from Drupal form system. Right. So then which other way can I achieve my purpose please? I know I can fire an ajax event from the autocomplete, set the value the attribute using invokecommand and then return an ajaxrespose. But i just felt it would be good to set the autocomplete's attribute the same place Its value was being set - in the selection handler. That's cleaner code. So what do you think? Thanks
cn flag
One approach would be to add a hidden field (`'#type' => 'hidden'`), and set its value with JS. That should get picked up in the form submission
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.