Score:0

How can I set a default value to show for a taxonomy term name in its edit form?

in flag

I have a site where I have a taxonomy term. I want to set a default value to the term name and display it when the form loads. In hook_form_alter(), I tried to set the default value to Check term name in the form. The form loads with this value, but even after changing the value in the text field, the form gets submitted with Check term name.

This is the code I am using to set the default value.

$form['name']['widget'][0]['value']['#value'] = 'check term name';

How do I set the default value?

Kevin avatar
in flag
You can't set #value, it should be #default_value.
Raja Chakraborty avatar
in flag
But how come user can see this default value in the form? I want to prepopulate the title field with something and change the value according to what the user provides.
cn flag
That's exactly what default values are for, is there a reason you can't use them?
Score:0
in flag

As @Kevin and @Clive noted, you must use #default_value to do this.

$form['name']['widget'][0]['value']['#default_value'] = 'check term name';

[Property] #default_value:

Description: The value of the form element that will be displayed or selected initially if the form has not been submitted yet. Should NOT be confused with #value, which is a hard-coded value the user cannot change!

Source: Form API Reference

(Yes, this is a reference page for D7, but this section still applies.)

Raja Chakraborty avatar
in flag
Thanks, it worked.
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.