1

I'm using Drupal 7 for my website. I created custom settings page with custom form in it, but the problem is I don't know proper way to load inserted value from database to my form after saving it, and every time I write something in it, information is written to database, but form is empty. My code looks like this for this:

function registration_pdf_form($form, $form_state) {
    $form['registration_body_twoside'] = array(
        '#type' => 'text_format',
        '#title' => t('Two-sided PDF'),
    );
    $form['registration_body_threeside'] = array(
        '#type' => 'text_format',
        '#title' => t('Three-sided PDF'),
    );
    $form['actions'] = array(
        '#type' => 'actions'
    );
    $form['actions']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Save settings')
    );
    return $form;
}
function registration_pdf_form_submit($form, $form_state){
    $values = array(
        'reg_twoside' => $form_state['values']['registration_body_twoside']['value'],
        'reg_threeside' => $form_state['values']['registration_body_threeside']['value'],
    );
    $insert = db_update('pdf_settings')
        -> fields(array(
            'reg_twoside' => $form_state['values']['registration_body_twoside']['value'],
            'reg_threeside' => $form_state['values']['registration_body_threeside']['value'],
        ))
        ->execute();
    drupal_set_message(t('Settings have been saved'));
}

How do I get values to my fields from database?

2
  • 1
    Query the database and set the default value of the field.
    – Alex Gill
    Apr 14, 2014 at 9:38
  • I tried using db_query for that, but ended up with this error: Warning: htmlspecialchars() expects parameter 1 to be string, object given - check_plain(). and dont get any values to a field.
    – irmantas
    Apr 14, 2014 at 9:41

1 Answer 1

1

You can try using system_settings_form which is simple way of storing settings in Drupal..

function registration_pdf_form($form, &$form_state) {
   $form['registration_body_twoside'] = array(
        '#type' => 'text_format',
        '#title' => t('Two-sided PDF'),
        '#default_value' => variable_get('registration_body_twoside', ''),
    );
    $form['registration_body_threeside'] = array(
        '#type' => 'text_format',
        '#title' => t('Three-sided PDF'),
        '#default_value' => variable_get('registration_body_threeside', ''),
    );
  return system_settings_form($form);
}

you can read values using

$twoside = variable_get('registration_body_twoside', '');
$threeside = variable_get('registration_body_threeside', '');

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.