代码之家  ›  专栏  ›  技术社区  ›  Montana Harkin

是否可以将模块的复选框设置为“只读”?

  •  2
  • Montana Harkin  · 技术社区  · 17 年前

    我正在编写一个安全模块,允许为多站点drupal设置启用某些模块。现在,我们可以使用下面的代码片段轻松地禁用模块。

    我们希望将表单元素设置为只读,然后启用仅启用/禁用子集的功能。

    function disable_form_fields_form_alter(&$form, $form_state, $form_id) {
    
        //get handle on drupal user var
        global $user;
    
        //if we are uid == 1 then we can edit this stuff...
        if($user->uid !=1 && variable_get('osu_update_lockdown',1)==1){
    
            if ($form_id == 'system_modules'){
    
                //find all the modules not disabled
                $modules_to_disabled = array_diff_key($form['status']['#options'],     array_flip($form['status']['#disabled_modules']));
    
                //for each not in our disabled list lets add it in
                foreach($modules_to_disabled as $name => $value){
                    $form['status']['#disabled_modules'][] = $name;
                }
    
                //disable non-checked boxes
                foreach($form['status']['#options'] as $key=>$val){
                    $form['status']['#process']['system_modules_disable'][0][]=$key;
                }
                //disable checked boxes
                foreach($form['status']['#default_value'] as $key=>$val){
                    $form['status']['#process']['system_modules_disable'][0][]=$val;
                }
    
                unset($form['buttons']['submit']);
    
            }
        } 
    }
    
    2 回复  |  直到 12 年前
        1
  •  0
  •   gpilotino    17 年前

    你试过 #access '=>表单元素的假属性?

        2
  •  0
  •   q9f    12 年前

    是的,你可以用这个 #attributes 要将其设置为只读:

    '#attributes' => array('readonly' => 'readonly'),
    

    所以,对于你的表格来说,它是这样的:

    $form['status']['#attributes'] = array('readonly' => 'readonly');
    

    这将使您的表单只读。