Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By crytekker on
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
You could use the field
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.
Disabling the field is
Disabling the field is another option:
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....
non editable fields..
thanks
I just forgot to thank you, i just used it successfully just once you posted it. Thanks!
Added to .tpl files of functions.php file
Where should this code be added to not allow users to see/change fields in the node/add form?
This code added in
This code added in (hook_form_alter)functions.php file.
#disabled didn't work for me
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.Another option among the
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.
here is my soluction for drupal6
Can some one help us to
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 ?
https://www.drupal.org/project/image_captcha_indicator
https://www.drupal.org/project/ext_js
Disable will show the field,
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
Thank You drastik
Only this helps me
$form['field_xxx']["#attributes"]['class'][] = 'element-invisible';
Node type article : title will be non editable for editor role
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;
}
}