0

So I've got a custom admin panel page in a module I've written, and I want to get a value out of it.

Let's say I have a text box on this admin page, with a value, and I want to get that at some other point.

Do I need to use variable_set and put that into the database and then variable_get it in the function I want to use it in - or do I call it some other way?

Basically this thread:

https://drupal.org/node/2074589

Essentially, I just want to have a user be able to select a picklist value in the admin, and then call that picklist value later on in the actual module, for display.

0

You need to utilize system_settings_form when you write your admin form.

function mymodule_admin_form($form, &$form_state) {
  $form['my_textfield'] = array(
    '#type' => 'text',
    '#title' => 'Title',
    '#default_value' => isset($form_state['values']['my_textfield']) ? $form_state['values']['my_textfield'] : '',
  );

  return system_settings_form($form);
}

Submitting this form will call system_settings_form_submit which saves all the form fields as a variable, so you need to be careful when naming the form fields.

Now all you need to do is call:

variable_get('my_textfield', 'default value to return if no value is set');

from any function after drupal is bootstrapped.

Here's more information on how to create a configuration form: Creating the configuration form

2
  • Is the second argument optional?
    – Jack Ryan
    Aug 28 '13 at 16:55
  • for variable_get? Second argument is optional, as it defaults to NULL. See variable_get
    – Beebee
    Aug 29 '13 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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