Hi all,

I am trying to hide some fields from a form under certain circumstances.
In particular, i am developing a custom module where i want a certain node creation form to:
1) get some fields prefilled (i fill in the default value from the hook_alter_form)
2) and i want such field not to be editable on the form

I am unable to achieve the 2), i can't find the way to get these fields fixed and non editable in the form from code.
Can anyone help me?

Thank you in advance!

Comments

nevets’s picture

You could use the field permissions module and for each of the fields you want to hide enable the create and edit fields. This will hide them from all but user 1.

MeInc’s picture

savithac’s picture


//use this code in your code .
Specify field name. For example the field name is "field_myform".

$form['field_myform']['#disabled']= TRUE;

Hidden fields:
//use this code to hide the field.

$form['field_myform']['und']['#type']= 'hidden';

crytekker’s picture

I just forgot to thank you, i just used it successfully just once you posted it. Thanks!

hoff331’s picture

Where should this code be added to not allow users to see/change fields in the node/add form?

savithac’s picture

This code added in (hook_form_alter)functions.php file.

angel_g’s picture

I have to modify the inline entity form so certain elements are disabled. Tried '#disabled' and it didn't have any effect.
'#access' works and hides the field, but sometimes we want to show the information, just prevent it from modification.
For a text field I was able to disable with $form[$field]['und'][0]['value']['#attributes']['readonly'] = 'readonly'; but unfortunately this code does *not* work for select lists. They want 'disabled' attribute when rendered.

jwjoshuawalker’s picture

Another option among the hundreds, since certain fields won't work after their #type being changed to hidden (like Date fields)

For Drupal 7, there is a CSS rule in system.base.css for "element-invisible", you've probably seen it all over your HTML.


  $form['field_xxx']['#disabled'] = TRUE;
  $form['field_xxx']["#attributes"]['class'][] = 'element-invisible';


  //There is also:
  $form['field_xxx']['#access'] = FALSE;

silas.xie’s picture

function woger_allegro_form_alter(&$form, &$form_state, $form_id) {
	if ($form_id == 'woger_product_node_form') {
		$form['#after_build'][] = 'woger_allegro_check_properties';
	}
}
function woger_allegro_check_properties(&$form, &$form_state) {
	$form['group_allegro']['field_allegro_product_id'][0]['value']['#attributes']['readonly'] = 'readonly';
	$form['group_allegro']['field_allegro_offer_id'][0]['value']['#attributes']['readonly'] = 'readonly';
	return $form;
}
qqboy’s picture

Can some one help us to understand the difference between
-disable
-access no
-hidden
-element-invisible

For example
now a field only need to be hidden from normal user, but not administrator, and also code functions.
What should we do ?

Jaypan’s picture

Disable will show the field, but users won't be able to change the value in it.
#access = FALSE will not output the field to the browser
hidden will create an <input> field of type 'hidden'. It will be sent to the browser, and will be editable, but it won't be visible.
element-invisible will output the field to the browser, but it will be hidden through CSS

Alen Simonyan’s picture

Only this helps me
$form['field_xxx']["#attributes"]['class'][] = 'element-invisible';

surendra77c’s picture

function alter_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'article_node_form': // content type article check with dsm($form)
global $user;
if (in_array('editor', array_values($user->roles))) {
$form['title']['#disabled'] = TRUE;
}
break;
}
}