0

I created a config form in the admin/config page. (Just a link field that needs to be saved for later retrieval) Im confused as to how to save and retrieve the form data.

When creating a custom form in a block I have been successful using:

variable_get('link_title', '')

However when creating a custom form it seems to require a different way. I attempted to save the data in hook_form_submit. However Im not sure how to retrieve when you return to the form at a later date.

function read_more_news_admin_form($form, &$form_state) {
        $form = array();
        $form['link_container'] = array(
            '#type' => 'container',
            '#attributes' => array('class' => array('some-class')),
            '#tree' => TRUE,
        );
        $form['link_container']['link'] = array(
            '#type' => 'link_field',
            '#field_name' => 'link_field',
            '#language' => 'und',
            '#field_parents' => array(),
            '#delta' => 0,
            '#default_value' => array(
                'title' => variable_get('link_title', ''),
                'url' => variable_get('link_url', ''),
            ),
        );
        return system_settings_form($form);
    }

    function read_more_news_form_submit($form_id, $form_values) {
      drupal_write_record('readmorelink', $form_state['values']); 
    }

2 Answers 2

2

I don't know how system_settings_form will handle form elements of which the parent element has the tree property set. So it is best to go with a submit handler and do it yourself.

function read_more_news_admin_form($form, &$form_state) {
  $form = array();
  $form['link_container'] = array(
    '#type' => 'container',
    '#attributes' => array('class' => array('some-class')),
    '#tree' => TRUE,
  );
  $form['link_container']['link'] = array(
    '#type' => 'link_field',
    '#field_name' => 'link_field',
    '#language' => 'und',
    '#field_parents' => array(),
    '#delta' => 0,
    '#default_value' => array(
      'title' => variable_get('link_title', ''),
      'url' => variable_get('link_url', ''),
    ),
  );
  $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
  return $form;
}

function read_more_news_admin_form_submit(&$form, &$form_state) {
  variable_set('link_title', $form_state['values']['link_container']['link']['title']);
  variable_set('link_url', $form_state['values']['link_container']['link']['url']);
  //drupal_write_record('readmorelink', $form_state['values']);
}
1

You don't need to use a separate submit handler if you are using system_settings_form() unless you need to do something out of the ordinary.

Setting the #default_value of each element with variable_get will retain the settings of your form. variable_get will check if the variable exists and use the stored variable if there is one, otherwise it will use the 2nd argument in the call.

A basic working example would be like this:

function HOOK_admin_form($node, &$form_state) {
  $form['MODULE_ELEMENT'] = array(
    '#type' => 'textfield',
    '#default_value' => variable_get('MODULE_ELEMENT', 'value'),
    '#title' => t('title'),
    '#required' => TRUE,
  );

  $form['MODULE_ELEMENT2'] = array(
    '#type' => 'textfield',
    '#default_value' => variable_get('MODULE_ELEMENT2', 'value'),
    '#title' => t('title'),
    '#required' => TRUE,
  );

  return system_settings_form($form);
}
1
  • Notice in my example that I am using variable_get in the default value but when I submit the form. I get a message that the form settings have been saved except that the field becomes empty and the values are never stored. Is there something that Im still missing? (I removed the submit handler after reading your post) Commented Sep 25, 2014 at 20:43

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.