donglv5269 2014-01-24 18:34
浏览 30
已采纳

限制多个字段的用户角色

I know that I can make two different field types and assign different roles to them. Because of using views, custom searches, and gmaps, and because this will be applied to Field collection I really need this kind of approach to the problem.

I tried to crate custom module that will limit role staff to only be able to insert two field_text into node but administrator can insert as many as he wont. field_text Number of values is set to Unlimited and Content type name is youtube. I found this for drupal 6 but I don't know how to code it in drupal 7.

function myformlimit_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'youtube') {
global $user;
// Only allow to insert 2 text for role = staff
if (in_array('staff', $user->roles)) {
  $text_limit = 2;
  $form['#field_info']['field_text']['multiple'] = $text_limit;
  $i = 1;
  foreach ($form['field_text'] as $key => $value) {
    if (is_numeric($key)) {
      if ($i > $text_limit) {
        unset($form['field_text'][$key]);
      }
      $i++;
    }
  }
}
}
 }
  • 写回答

1条回答 默认 最新

  • doumu4916 2014-01-26 09:01
    关注

    Well you are half way there. You can use the following code to get you the idea.

    Kindly note that I named my field field_test_field, you can replace that with your field's name.

    function myformlimit_form_alter(&$form, &$form_state, $form_id)
    {
        if($form_id == 'youtube_node_form')
        {
            global $user;
            if(in_array('staff', $user->roles))
            {
                if($form_state['field']['field_test_field'][LANGUAGE_NONE]['items_count'] >= 2)
                {
                    unset($form['field_test_field'][LANGUAGE_NONE]['add_more']);
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?