Drupal的6形式和OPTGROUP阵列 [英] Drupal 6 forms and optgroup arrays

查看:98
本文介绍了Drupal的6形式和OPTGROUP阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的阵列是由(使用xml2array)将XML转换到一个阵列产生。但是它不是完全正确的格式,我需要在一个Drupal 6形成OPTGROUP。

The following array is produced by converting xml to a array (using xml2array). However its not the exactly the right format I need for a optgroup in a Drupal 6 form.

Array (
   [root] => Array ([attr] => Array ([id] => 1) [label] => Array ([value] => My Root)
      [node] => Array (
         [0] => Array ([attr] => Array([id] => 2) [label] => Array([value] => Category 1)
            [node] => Array(
               [0] => Array ([attr] => Array ([id] => 14) [label] => Array ([value] => Sub-Category 1))
               [1] => Array([attr] => Array ([id] => 15) [label] => Array([value] => Sub-Category2))

我觉得阵列也可以减少这种格式ID值完整的子类别。不过,我无法与Drupal的文档证实了这一点,因为他们不提关于向选项赋值东西。

I think the array has too be reduced to this format with id values intact for the sub-categories. However I can't confirm this with the drupal docs as they don't mention anything about assigning values to a option.

Array (
   [Category 1] => Array(
      [14] => Sub-Category 1
      [15] => Sub-Category 2
   )
)

所以我的问题是:1)是什么,是为Drupal optgroups我指定的值正确的阵列格式和2)我怎么降低我的数组来匹配?

So my questions are 1) is what is the correct array format for Drupal optgroups with my specified values and 2) how do I reduce my array to match?

推荐答案

1)
一个选项组阵列的正确格式为(我将使用作为一个例子状态的列表):

1) The correct format for an option group array is (I will use a list of states as an example):

$state_list = array(
  'AL'=>"Alabama",  
  'AK'=>"Alaska",  
  'AZ'=>"Arizona",  
  'AR'=>"Arkansas",
  ...
);

这将由国家缩写作为键显示国家的名单,可访问的。

This would display a list of the state names, accessible by the state initials as keys.

为了使用亚类,你会修改上面的一样:

In order to use subgroups, you would modify the above like:

$state_list = array(
  'A'=> array(
    'AL'=>"Alabama",  
    'AK'=>"Alaska",  
    'AZ'=>"Arizona",  
    'AR'=>"Arkansas",
    ...
  ),
  'C'=> array(
    'CA'=>'California',
    'CO'=>'Colorado',
     ...
  ),
  ...
);

所以,你需要以下格式的转换数据给出:

So, you would need the following format for your translated data as given:

$options = array(
  'My Root' => array(
    'Category 1' => array(
      '14' => 'Sub-Category 1',
      '15' => 'Sub-Category 2'
    )
  )
)

不过,如果我的根是选择元素的名称,用这个来代替(我会告诉你下面每一个表单定义):

However, if 'My Root' is the name of the select element, use this instead (I'll show you a form definition for each one below):

$options = array(
  'Category 1' => array(
    '14' => 'Sub-Category 1',
    '15' => 'Sub-Category 2'
   )
)

您形式的API定义是这样的:

Your forms API definition would look like this:

function mymodule_form() {
  $options = array(
    'My Root' => array(
      'Category 1' => array(
        '14' => 'Sub-Category 1',
        '15' => 'Sub-Category 2'
      )
    )
  );
  ...
  $form['Select Box Name'] = array(
    '#type' => 'select',
    '#title' => 'Select a Category',
    '#options'=> $options,
  );
  ...
  return $form;
}

或者这样,如果我的根是选择元素的名称和它的类没有一个人:

Or like this, if 'My Root' is the name of the select element and not one of it's categories:

function mymodule_form() {
  $options = array(
    'Category 1' => array(
      '14' => 'Sub-Category 1',
      '15' => 'Sub-Category 2'
     )
  );
  ...
  $form['My Root'] = array(
    '#type' => 'select',
    '#title' => 'Select a Category',
    '#options'=> $options,
  );
  ...
  return $form;
}

在这个定义中,你的子类别使用键值整数 - 我不知道这是你想要的。很多时候,Drupal的开发人员使用的阵列具有相同的键和值(例如,'[CA} => [CA]'),使事情变得更容易。

In this definition, your subcategories are keyed using integers - I am not sure this is what you want. Many times Drupal developers use arrays with identical keys and values (e.g. '[CA}=>[CA]') to make things easier.

这种格式的一个副作用是,1类本身不是选择(因为您的XML源有两个根和1类键它似乎对我说,他们在自己的$ P可选$ pvious化身);我不知道,如果HTML选项组中的子类别标签甚至都是,所以我不知道这是否是一个问题或没有

A side-effect of this format is that 'Category 1' is not itself selectable (since your XML source has keys for both 'Root' and "Category 1' it would seem to me that they were selectable in their previous incarnation); I am not sure if HTML option group sub-category labels even are, so I am not sure if this is a problem or not.

这篇关于Drupal的6形式和OPTGROUP阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆