Score:0

Custom form, add admin editable instructions from settings area to form

za flag

I have a custom form that is working great. I'd like to add a field "instructions" right under the title, above the first form field. This "instructions" field is editable in the module settings area. I have tried to get this data like:

$config = $this->config('myform.settings');
$instructions = $config->getValue('instructions');

however, it throws an error:

Call to undefined method Drupal\Core\Config\ImmutableConfig::getValue()

How do I pass the data in a textfield to the form the users are filling out on render?

cn flag
It's `$config->get('instructions')`
Score:2
us flag

The method to get a value from an immutable configuration object or an editable configuration object is get().

In a form class that extends ConfigFormBase, the code to use for your case is the following one.

$instructions = $this->config('myform.settings')
  ->get('instructions');

getValue() is a FormState method, which is used to get a value submitted in the form. See AccountSettingsForm::submitForm() as example of how that method is used.

$this->config('user.settings')
  ->set('anonymous', $form_state
  ->getValue('anonymous'))
  ->set('register', $form_state->getValue('user_register'))
  ->set('password_strength', $form_state->getValue('user_password_strength'))
  ->set('verify_mail', $form_state->getValue('user_email_verification'))
  ->set('cancel_method', $form_state->getValue('user_cancel_method'))
  ->set('notify.status_activated', $form_state->getValue('user_mail_status_activated_notify'))
  ->set('notify.status_blocked', $form_state->getValue('user_mail_status_blocked_notify'))
  ->set('notify.status_canceled', $form_state->getValue('user_mail_status_canceled_notify'))
  ->save();
za flag
Thanks for the solid explanation!
I sit in a Tesla and translated this thread with Ai:

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.