用数组制作数组

发布于 2024-12-15 13:48:57 字数 705 浏览 1 评论 0原文

我想在我的 Drupal 文件夹中创建很多数组。但我不知道该怎么做。基本上,它总是相同的结构。

$form['actions']['saveasdraft']['#type'] = 'submit';
$form['actions']['saveasdraft']['#access'] = true;
$form['actions']['saveasdraft']['#value'] = 'Save as Draft';
$form['actions']['saveasdraft']['#weight'] = 11;
$form['actions']['saveasdraft']['#submit'][0] = 'node_fiche_form_submit'; 

$form['actions']['saveascurrent']['#type'] = 'submit';
$form['actions']['saveascurrent']['#access'] = true;
$form['actions']['saveascurrent']['#value'] = 'Save as New version';
$form['actions']['saveascurrent']['#weight'] = 12;
$form['actions']['saveascurrent']['#submit'][0] = 'node_fiche_form_submit'; 

...

有没有更简单的方法来做到这一点?

I would like to create a lot of arrays in my Drupal folder. But I'm not sure how to do this. Basically, it's always the same structure.

$form['actions']['saveasdraft']['#type'] = 'submit';
$form['actions']['saveasdraft']['#access'] = true;
$form['actions']['saveasdraft']['#value'] = 'Save as Draft';
$form['actions']['saveasdraft']['#weight'] = 11;
$form['actions']['saveasdraft']['#submit'][0] = 'node_fiche_form_submit'; 

$form['actions']['saveascurrent']['#type'] = 'submit';
$form['actions']['saveascurrent']['#access'] = true;
$form['actions']['saveascurrent']['#value'] = 'Save as New version';
$form['actions']['saveascurrent']['#weight'] = 12;
$form['actions']['saveascurrent']['#submit'][0] = 'node_fiche_form_submit'; 

...

Is there an easier way to do this?

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

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

发布评论

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

评论(2

青衫儰鉨ミ守葔 2024-12-22 13:48:57

“正确”的方法是这样的(参见 Drupal 编码标准):

$form['action']['saveasdraft'] = array(
  '#type' => 'submit',
  '#access' => TRUE,
  '#value' => 'Save as Draft',
  // etc...
);

$form['action']['saveascurrent'] = array(
  '#type' => 'submit',
  '#access' => TRUE,
  '#value' => 'Save as New version',
  // etc...
);

就是这样Drupal 核心模块可以做到这一点(以及我见过的所有贡献模块)。

编辑

如果您担心重复代码,您能做的最好的事情就是设置一些默认值...应该节省几百行:)

$defaults = array('#type' => 'submit', '#access' => TRUE, /* etc... */);

$form['action']['saveasdraft'] = $defaults + array(
  '#value' => 'Save as Draft'
);

$form['action']['saveascurrent'] = $defaults + array(
  '#value' => 'Save as New version'
);

希望这是有道理的

The 'correct' way to do it is like this (see Drupal coding standards):

$form['action']['saveasdraft'] = array(
  '#type' => 'submit',
  '#access' => TRUE,
  '#value' => 'Save as Draft',
  // etc...
);

$form['action']['saveascurrent'] = array(
  '#type' => 'submit',
  '#access' => TRUE,
  '#value' => 'Save as New version',
  // etc...
);

That's the way Drupal core modules do it (and all contributed modules that I've ever seen).

EDIT

If you're worried about repeating code the best you're going to be able to do is set up some defaults...should save a few hundred lines :)

$defaults = array('#type' => 'submit', '#access' => TRUE, /* etc... */);

$form['action']['saveasdraft'] = $defaults + array(
  '#value' => 'Save as Draft'
);

$form['action']['saveascurrent'] = $defaults + array(
  '#value' => 'Save as New version'
);

Hope that makes sense

分分钟 2024-12-22 13:48:57

尝试采用 DRY(不要重复自己)规则。你可以这样做,例如。通过创建将返回正确元素以输入到更大数组中的函数,如下所示:

function form_element($value, $weight, $type = 'submit', $access = true,
    $submit = 'node_fiche_form_submit') {
    return array(
        '#type' => $type,
        '#access' => $access,
        '#value' => $value,
        '#weight' => $weight,
        '#submit' => array($submit),
    );
};

然后您可以像这样使用它:

$form['actions']['saveasdraft'] = form_element('Save as Draft', 11);
$form['actions']['saveascurrent'] = form_element('Save as New version', 12);
// ...and so on

请参阅 此键盘作为证明。

附言。当然,您应该为辅助函数发明一些更有意义且冲突更少的名称,但该方法是最短的方法之一。

Try to employ a rule of DRY (Don't Repeat Yourself). You can do it eg. by creating function that will return proper element to be entered into bigger array like that:

function form_element($value, $weight, $type = 'submit', $access = true,
    $submit = 'node_fiche_form_submit') {
    return array(
        '#type' => $type,
        '#access' => $access,
        '#value' => $value,
        '#weight' => $weight,
        '#submit' => array($submit),
    );
};

and you can then use it like that:

$form['actions']['saveasdraft'] = form_element('Save as Draft', 11);
$form['actions']['saveascurrent'] = form_element('Save as New version', 12);
// ...and so on

See this codepad for a proof.

PS. Of course you should invent some more meaningful and less-conflicting name for the helper function, but the approach is one of the shortest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文