drupal 7.如何重构表单数组

发布于 2024-12-17 04:27:13 字数 969 浏览 0 评论 0原文

我正在从 Drupal 示例文件创建一个向导,并希望重构在设置选项和单选之类的项目时重复的代码段。

我已经尝试过传递“普通”和“优先”的简单函数,但找不到使其工作的方法。

有人可以告诉我最好的方法吗?

未分解的代码如下:

function services_wizard_share_capital_classes($form, &$form_state) {

$form['share_classes']['type_of_class'] = array(
'#type' => 'select',
'#title' => t('What type of share will this class be?'),
'#options' => array(
  1 => t('Ordinary'),
  2 => t('Preferential'),
),
);

$form['ordinary']['share_type'] = array(
'#type' => 'item',
'#description' => t("You chose Ordinary Shares"),
'#states' => array(
  'visible' => array(
    ':input[name="type_of_class"]' => array('value' => '1'),
  ),
),
);

$form['preferential']['share_type'] = array(
'#type' => 'item',
'#description' => t("You chose Preferential Shares"),
'#states' => array(
  'visible' => array(
    ':input[name="type_of_class"]' => array('value' => '2'),
  ),
),
);
return $form;
}

I am creating a wizard from the Drupal example file and would like to refactor the segments of code that are repeated when setting up items like options and radios.

I have already tried a simple function passing "ordinary" and "preferential" but can't find a way to make it work.

Can someone give me an idea of the best way to do this?

unfactored code is as below:

function services_wizard_share_capital_classes($form, &$form_state) {

$form['share_classes']['type_of_class'] = array(
'#type' => 'select',
'#title' => t('What type of share will this class be?'),
'#options' => array(
  1 => t('Ordinary'),
  2 => t('Preferential'),
),
);

$form['ordinary']['share_type'] = array(
'#type' => 'item',
'#description' => t("You chose Ordinary Shares"),
'#states' => array(
  'visible' => array(
    ':input[name="type_of_class"]' => array('value' => '1'),
  ),
),
);

$form['preferential']['share_type'] = array(
'#type' => 'item',
'#description' => t("You chose Preferential Shares"),
'#states' => array(
  'visible' => array(
    ':input[name="type_of_class"]' => array('value' => '2'),
  ),
),
);
return $form;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拍不死你 2024-12-24 04:27:13

我不确定这是否是最好的方法,但它肯定是重构代码的一种方法:

function _services_wizard_share_capital_classes_add_el(&$form, $name, $description, $index) {
  $form[$name]['share_type'] = array(
    '#type' => 'item',
    '#description' => t($description),
    '#states' => array(
      'visible' => array(
        ':input[name="type_of_class"]' => array('value' => "$index"),
      ),
    )
  );
}

function services_wizard_share_capital_classes($form, &$form_state) {
  // Other code

  _services_wizard_share_capital_classes_add_el($form, 'ordinary', 'You chose Ordinary Shares', 1);
  _services_wizard_share_capital_classes_add_el($form, 'preferential', 'You chose Preferential Shares', 2);
  // etc...
}

I'm not sure about this being the best way but it's certainly a way to refactor your code:

function _services_wizard_share_capital_classes_add_el(&$form, $name, $description, $index) {
  $form[$name]['share_type'] = array(
    '#type' => 'item',
    '#description' => t($description),
    '#states' => array(
      'visible' => array(
        ':input[name="type_of_class"]' => array('value' => "$index"),
      ),
    )
  );
}

function services_wizard_share_capital_classes($form, &$form_state) {
  // Other code

  _services_wizard_share_capital_classes_add_el($form, 'ordinary', 'You chose Ordinary Shares', 1);
  _services_wizard_share_capital_classes_add_el($form, 'preferential', 'You chose Preferential Shares', 2);
  // etc...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文