【问题标题】:Arbitrary Form Processing with Drupal使用 Drupal 进行任意表单处理
【发布时间】:2011-02-09 20:34:27
【问题描述】:

我正在为我的组织编写一个模块,用于将静态文件的 XML 提要缓存到我们网络服务器上的任意位置。我是 Drupal 开发的新手,想知道我是否以正确的方式处理这个问题。

基本上我:

  1. 通过菜单挂钩公开一个 url,用户可以在其中进入网络服务器上的输出目录并按下“转储”按钮,然后让 PHP 转到 drupal 并获取提要 xml。我不需要有关该功能的帮助,因为我实际上有一个在 Python 中工作的原型(在 Drupal 之外)..
  2. 使用表单参数为表单提供回调,我可以在其中执行我的逻辑。

这里是菜单钩子:

function ncbi_cache_files_menu() {
    $items = array();


    $items['admin/content/ncbi_cache_files'] = array(
        'title' => 'NCBI Cache File Module',
        'description' => 'Cache Guide static content to files',
        'page callback' => 'drupal_get_form',
        'page arguments' => array( 'ncbi_cache_files_show_submit'),
        'access arguments' => array( 'administer site configuration' ),
        'type' => MENU_NORMAL_ITEM,
    );

    return $items;
}

我在以下位置生成表单:

function ncbi_cache_files_show_submit() {

    $DEFAULT_OUT = 'http://myorg/foo';
    $form[ 'ncbi_cache_files' ] = array(
        '#type' => 'textfield',
        '#title' => t('Output Directory'),
        '#description' => t('Where you want the static files to be dumped.
            This should be a directory that www has write access to, and
            should be accessible from the foo server'),
        '#default_value' => t( $DEFAULT_OUT ),
        '#size' => strlen( $DEFAULT_OUT ) + 5,
    );

    $form['dump'] = array( 
        '#type' => 'submit',
        '#value' => 'Dump',
        '#submit' => array( 'ncbi_cache_files_dump'),
    );

    return system_settings_form( $form );
}

那么功能就在回调中:

function ncbi_cache_files_dump( $p, $q) {
    //dpm( get_defined_vars() );
    $outdir = $p['ncbi_cache_files']['#post']['ncbi_cache_files'];
    drupal_set_message('outdir: ' . $outdir );

}

问题:这是在 Drupal 中处理任意表单的一种不错的方式吗?我真的不需要监听任何 drupal 钩子,因为我基本上只是在做一些 URL 和文件处理。

我在回调 ($q) 中得到的那些参数是什么?那是我猜的表单数组,带有帖子值?这是获取表单参数的最佳方式吗?

感谢您的建议。

【问题讨论】:

    标签: forms drupal module


    【解决方案1】:

    您可以分两个阶段处理表单,验证和提交。

    验证用于验证某些用户提供的信息,并在某些用户输入无效(如无效的 url 或电子邮件地址)时引发表单错误

    如果表单通过所有验证,则调用您使用的提交,因此,如果您进行了适当的验证,您将知道用户提供的数据是可以的。

    您的提交函数应如下所示:

    function ncbi_cache_files_dump(&$form, &$form_state) {
        // $form: an array containing the form data
        // $form_state: data about the form, like the data inputted in the form etc.
        // code...
    }
    

    【讨论】:

      【解决方案2】:

      我认为您需要 2 个单独的表格:

      1. 用于设置目录(您现在拥有的);
      2. 用于制作转储(另一种使用配置路径的形式)。

      将之前保存的路径发布为设置表单中的默认值(而不是硬编码路径)似乎也是合乎逻辑的。

      一般来说,你应该从提交回调的第二个参数中检查表单输入数据:

      function ncbi_cache_files_dump(&$form, &$form_state) {
          $outdir = $form_state['values']['ncbi_cache_files'];
          // ...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-27
        • 1970-01-01
        相关资源
        最近更新 更多