用于分类管理的 Drupal hook_form_alter
我创建了一个模块来完成所有表单更改,称为“form_mods”。它适用于大多数情况,但不适用于分类页面。
我的目标是“taxonomy_overview_vocabularies”的表单 ID。我试图隐藏“网站管理员”和“DJ”角色的“编辑词汇”链接。
我的代码正确地取消了 $form 数组的设置,但 Drupal 仍然显示“编辑词汇表”链接。
function form_mods_form_alter($form, $form_state, $form_id) {
if($form_id == 'taxonomy_overview_vocabularies'){
global $user;
$hide=0;
$hideArray = array('webmaster', 'dj');
foreach($user->roles AS $key => $value){
if(in_array($value, $hideArray)){
$hide++;
}
}
if($hide){
foreach($form AS $vocab){
//print_r($vocab);
if(isset($vocab['edit']['#value'])){
unset($vocab['edit']['#value']);
}
}
}
}
}
I created a module to do all my form altering called "form_mods". It's working for most situations but not for the Taxonomy page.
I'm targeting the form id of "taxonomy_overview_vocabularies". I'm trying to hide the link "edit vocabulary" for roles of "webmaster" and "dj".
My code is unsetting the $form array correctly, but Drupal is still displaying the "edit vocabulary" link.
function form_mods_form_alter($form, $form_state, $form_id) {
if($form_id == 'taxonomy_overview_vocabularies'){
global $user;
$hide=0;
$hideArray = array('webmaster', 'dj');
foreach($user->roles AS $key => $value){
if(in_array($value, $hideArray)){
$hide++;
}
}
if($hide){
foreach($form AS $vocab){
//print_r($vocab);
if(isset($vocab['edit']['#value'])){
unset($vocab['edit']['#value']);
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
非常小的 PHP 错误,
当您想要更改 foreach 语句中的数组成员时,您必须通过引用传递它们 &
foreach($form AS &$vocab)
否则 $vocab 会只是数组的副本Very small PHP mistake,
when you want to change array members in a for each statement you have to pass them by reference &
foreach($form AS &$vocab)
otherwise the $vocab would be just a copy of the array除了 Amjad 的回答之外,如果您不喜欢使用引用,我会建议另一种选择:
这样您就可以避免使用引用以及它们可能导致的潜在问题。
另请注意,我删除了
if
语句,该语句没有用(PHP 可以计算出来)。还可以考虑
array_map
。In addition to Amjad's answer, if you don't like using references, I would suggest another alternative:
This way you avoid using references, and potential issues they may lead to.
Also note I removed the
if
statement, which is not useful (PHP can figure it out).An
array_map
could also be considered.