用数组制作数组
我想在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“正确”的方法是这样的(参见 Drupal 编码标准):
就是这样Drupal 核心模块可以做到这一点(以及我见过的所有贡献模块)。
编辑
如果您担心重复代码,您能做的最好的事情就是设置一些默认值...应该节省几百行:)
希望这是有道理的
The 'correct' way to do it is like this (see Drupal coding standards):
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 :)
Hope that makes sense
尝试采用 DRY(不要重复自己)规则。你可以这样做,例如。通过创建将返回正确元素以输入到更大数组中的函数,如下所示:
然后您可以像这样使用它:
请参阅 此键盘作为证明。
附言。当然,您应该为辅助函数发明一些更有意义且冲突更少的名称,但该方法是最短的方法之一。
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:
and you can then use it like that:
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.