【问题标题】:Drupal 7: how do I save module settings?Drupal 7:如何保存模块设置?
【发布时间】:2018-02-20 01:36:13
【问题描述】:

我正在尝试为模块创建一些设置,并按照 Drupal.org 上的指南进行操作。我还将我的工作与现有模块进行比较。

配置菜单出现在正确的位置并带有正确的字段,但是当我点击保存时,没有保存任何输入。 (我已经运行缓存清除和注册表重建。)

有人知道我在这里做错了什么吗?我看不出我的东西有什么不同。

在我的 .admin.inc 文件中,我已将表单设置为:

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);
}

在我的 .module 文件中:

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);
}

【问题讨论】:

    标签: php drupal drupal-7


    【解决方案1】:

    好的,所以我发现值正在保存 - 只是当我重新加载配置页面时它们没有出现在我的文本区域中。

    问题是我使用text_format 字段将数据保存为数组。我需要将该数组的value 属性作为我的默认值。

    所以,我只是在default_value 三元表达式中添加了['value']

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      相关资源
      最近更新 更多