1

I'm trying to hide a CCK field in my node-edit form. I followed this tutorial:

http://dominiquedecooman.com/blog/drupal-how-disable-form-element-cck-form-using-form-alter

but i'm stucked at the implementing hook_form_alter function, it seems that this hook doesn't recognize my form, i've tried this:

function video_tutorial_form_alter(&$form, &$form_state, $form_id) {
  print_r($form_id);     
}

but it only prints "search_block_form search_theme_form";

3

You have 3 options if you wish to hide a form item:

  1. Use .css and make your life simple using display:none;
  2. create a custom module (see below)
  3. Why even have the cck field if you are going to hide it? What's the point of that?

Please note that the above tutorial DISABLES the field and does not HIDE it. Disabled fields can not be edited, but they are still displayed!

To point 2: Is this function in your themes template.php? If yes, I presume your theme is called 'video_tutorial', if not, you should rename your function so it is THEMENAME_form_alter.

However, since you're using the edit node form it will actually be using the admin theme, not your front end theme - unless you have changed permissions for certain usergroups not to be able to access the admin theme or you are using the front end theme as your admin theme (unlikely) -, so you're better off creating your own module to override the form rather than using the themes template.php file:

How to create your custom module:

  1. Create a folder, call it whatever you like - but it should make sense - eg:

    custom_admin_overrides
    
  2. In it, create two blank text files:

    custom_admin_overrides.info
    custom_admin_overrides.module
    
  3. Open custom_admin_overrides.info and add the basics so drupal can see your module:

    name = Custom Admin Overrides
    description = Custom Module that provides Admin Area Overrides
    package = Custom
    core = 7.x
    

Save the .info file and add the following to your custom_admin_overrides.module file:

<?php
function custom_admin_overrides_form_alter(&$form, $form_state, $form_id) {
 if($form_id == 'YOUR_FORM_ID') {
  $form['FIELD_NAME']['#disabled'] = TRUE;
 }
 // YOU CAN UNCOMMENT THE BELOW FOR EXTRA HELPFUL DETAILS
 // GET FORM ID UNCOMMENT BELOW
 drupal_set_message("YOUR_FORM_ID is : " . $form_id);
 // GET ALL FORM ELEMENTS UNCOMMENT BELOW
 // drupal_set_message(dprint_r($form, TRUE));
}

Note that we aren't closing the

Save and upload the folder to your site/all/modules/custom/ folder.

Now enable the module and from that point onwards you should see the form id's displayed on all admin pages in the drupal message box at the top.

Now go to your specific node create/edit page and make a note of the form id and the field name you wish to disable. Now reopen your custom_admin_overrides.module file and replace the values for

YOUR_FORM_ID

and

FIELD_NAME

Save and now your field should be uneditable. Uncomment the following line to stop displaying the form id's:

drupal_set_message("YOUR_FORM_ID is : " . $form_id);
  • Thank you tecjam for response, your solution is right. Problem was that hook_form_alter() didnt get form_id of form when i tried to submit it with not properly disabled cck field - i kept refreshing page with red "field is required" message. When i loaded edit form again everything works correctly – Peter Nov 21 '11 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.