What's the code to remove the display of a 开发者_Go百科cck field from a content-type create/edit form page?
Set the "#access
" field to "0
" or "FALSE
".
For example, this removes the "Site mission" field from the site informations page:
function example_form_system_site_information_settings_alter(&$form, &$form_state){
$form['site_mission']['#access'] = 0;
}
(where "example" is the name of a custom module)
Another example, this time it removes the Sticky option from add/edit page form:
function example_form_page_node_form_alter(&$form, &$form_state){
$form['options']['sticky']['#access'] = 0;
}
You probably know that already, but you will need to know the id (and the structure) of the form you wish to alter.
Most of the time, hook_form_THEID_alter(&$form, &$form_state)
is the good place to modify a specific form, however some fields are created after this hook is called, so in such cases, you need to use the generic hook_form_alter(&$form, $form_state, $form_id)
instead.
If you don't know the structure or even the form id (and don't want to install the Devel module), you could use this snipplet, so that when you're viewing the page you want to modify, it will display the form id and its structure at the top of the page.
function example_form_alter(&$form, $form_state, $form_id){
echo "<h1>Form ID: $form_id</h1>";
echo "Form Structure: <pre>".print_r($form, true)."</pre>";
}
(where "example" is the name of your custom module).
By the way, if you're just trying to prevent regular users from editing admin-only fields, you could simply use the Field Permissions module instead.
Check this snippet http://drupal.org/node/336355#comment-1192845 and use described in previous comments ['#access'] = FALSE
精彩评论