简体   繁体   中英

Drupal 7: how do I save module settings?

I am trying to create some settings for a module and am following the guide on Drupal.org. I am also comparing my work to existing modules.

The config menu appears in the correct place with the correct fields but when I hit save no input is saved. (I have run cache clear and registry rebuild.)

Would anyone know what I am doing wrong here? I cannot see how my stuff differs.

On my .admin.inc file I have set the form as such:

function contact_page_settings() {
  $form = array();

  $config = contact_page_default_settings();

  $form['#tree'] = TRUE;

  $form['contact_page_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Top Section'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    'top-title' => array(
      '#type' => 'textfield',
      '#title' => t('Top title'),
      '#default_value' => !empty($config['top-title']) ? $config['top-title'] : '',
    ),
    'top-left' => array(
      '#type' => 'text_format',
      '#title' => t('Top left'),
      '#default_value' => !empty($config['top-left']) ? $config['top-left'] : '',
    ),
    'top-right' => array(
      '#type' => 'text_format',
      '#title' => t('Top right'),
      '#default_value' => !empty($config['top-right']) ? $config['top-right'] : '',
    ),
  );

  return system_settings_form($form);
}

On my .module file I have:

function contact_page_menu() {
  $items = array();
  $items['admin/settings/contact-page'] = array(
    'title' => 'Contact Page content',
    'page callback' => 'drupal_get_form',
    'access arguments' => array('administer site configuration'),
    'page arguments' => array('contact_page_settings'),
    'type' => MENU_NORMAL_ITEM,
    'file' => '/admin/contact_page.admin.inc',
  );
  return $items;
}

function contact_page_default_settings() {
  $defaults = array(
    'top-tile' => 'Top title',
    'top-left' => 'Top left',
    'top-right' => 'Top right',
  );
  $config = variable_get('contact_page_settings', array());
  return array_merge($defaults, $config);
}

OK, so I found out the values were saving - just they were not appearing in my text areas when I reloaded the config page.

The problem was that I was using text_format fields which save data as an array. I needed to get the value property of that array as my default value.

So, I just added ['value'] to the default_value ternary expression:

'#default_value' => !empty($config['top-right']['value']) ? $config['top-right']['value'] : '',

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM