Hi,

I know how to populate the form fields in hook_form_alter but i have no idea how to hide them...

regards Volkan

Comments

muschpusch’s picture

I found a solution at lullabot

http://www.lullabot.com/articles/modifying-forms-drupal-5-and-6#comment-...

Too lazy to read try to add '#access' = false to your form element

$form['author']['name']['#access'] = FALSE;

westbywest’s picture

Please note that if you restrict the access permissions for a form field, then you are also restricting the active user's ability to submit any value for that field, whether via manual entry or via hook_form_alter() 'fu.

That is, this hook_form_alter() code wouldn't work:

$form['author']['name']['#access'] = FALSE;
$form['author']['name']['#value'] = 'something';

As an alternative, try simply hiding the field (or change to type hidden) if you still want the acting user to modify a form's field in some controlled way:

$form['author']['name']['#prefix'] = '<div style="display:none;">';
$form['author']['name']['#value'] = 'something';
$form['author']['name']['#suffix'] = '</div>';
cdmo’s picture

Changing type to hidden also prevents setting values via hook_form_alter() calls. Guess we're left with hiding via CSS.

-cdmo

ehsankhfr’s picture

I checked both unset($form[...]) and $form['...']['#access']=false , both didn't work.

I guess styling hidden is the only option remained for this issue.

xangy’s picture

.

Best,
Vishal Kumar

alesr’s picture

If you use "'#disabled' => 'TRUE'", you keep the value, but the fields are visible in the form. Sometime you don't want to show that to a user (data leak).
If you use "'#access' => 'FALSE'", you hide the field in the form, but lose the value of it.

To keep the default_value and hide the field you need to store the default value, unset field item and create it again with the value before you unset it.
Here's an example.

// Store default value in a variable.
$my_field_value = $form['my_field'][LANGUAGE_NONE]['#default_value'];  
 // Unset "old" field item.
unset($form['my_field']);
// Create new hidden field with the stored value.
$form['my_field'] = array(
   '#type' => 'hidden',
   '#value' => $my_field_value,
);

Works for creating and editing!

Thangobrind’s picture

This was a massive help and saved me loads of time

+1

DarrellDuane’s picture

I added a little bit of error prevention code and am using your suggestion two comments earlier as a part of a bigger tool in D7 to selectively hide or show fields based on the configuration provided by another content type:

  $fields_in_reg = get_machine_readable_fields_in_a_content_type('reg');  // show all fields in reg to hide or show
      foreach ($fields_in_reg AS $idx => $field_name) {
         if ( !in_array(array('value' => $field_name),  $control_content_type->field_reg_fields_to show['und'])   {  
            $default_value_set = FALSE;
            if(isset($form[$field_name]['und']['#default_value'])) {
              $field_value = $form[$field_name]['und']['#default_value'];  // save the value in a variable  
              $default_value_set = TRUE;	
            }        
            unset($form[$field_name]);
	    if($default_value_set) {
       		$form[$field_name] = array( '#type' => 'hidden', '#value' => $field_value);
	     }
           }
      }

the function get_machine_readable_fields_in_a_content_type is

function get_machine_readable_fields_in_a_content_type($content_type){

  $fields = array();
  $field_map = field_info_field_map();
  $idx = 0;
  foreach ($field_map AS $field => $map) {
    if(!isset($map['bundles']['node'][0]))
      continue;
    if($map['bundles']['node'][0] == $content_type) {
      $fields[$idx] = $field;
      $idx++;
    }
  }
  return  $fields;

}  // done get fields in a content type

this requires that you create a field in the control content type that is of type List(text) which has unlimited values and has a check boxes widget with allowed values set using a function similar to the one above, with human readable values:

function get_human_readable_fields_in_a_content_type($content_type){

  $fields = array();
  $field_map = field_info_field_map();

  foreach ($field_map AS $field => $map) {
    if(!isset($map['bundles']['node'][0]))
      continue;
    if($map['bundles']['node'][0] == $content_type) {

      $field_info = field_info_field($field);
      $instance =  field_info_instance('node', $field, $content_type);

      $desc = trim(substr(strip_tags($instance['description']), 0, 80));
      if(strlen($desc)) {
        $fields[$field] = '<b>' . $instance['label'] . '</b>/' . $desc  . ': ' .   $field;
      } else {
        $fields[$field] = '<b>' . $instance['label'] . '</b>: ' .   $field;
      }
                                                                                                                                                       
    }

  }
  return  $fields;

}  

Darrell Duane
d@duane.com

scuba_fly’s picture

Setting the field to disabled and hidden autofills it with the set default value set in the UI.

At least for a date field.

function hook_form_FORM_ID_alter() {
    $form['field_fieldname']['#disabled'] = TRUE;
    $form['field_fieldname']['#type'] = 'hidden';
}

Freelance Drupal developer, contact me if you want help on your Drupal project.

forestmars’s picture

YMMV for other field types.

taieb’s picture

+1

plingamn’s picture

Thank you for the solution .. :)

plingamn’s picture

Thank you for the solution .. :)