Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By muschpusch on
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
I found a solution at
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;
Please note that if you
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:
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:
Changing type to hidden
Changing type to hidden also prevents setting values via hook_form_alter() calls. Guess we're left with hiding via CSS.
-cdmo
I guess so!
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.
.
.
Best,
Vishal Kumar
How to hide it, keep the value, without CSS
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.
Works for creating and editing!
Thank you
This was a massive help and saved me loads of time
+1
This is a great help for selectively hiding fields for a given
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:
the function get_machine_readable_fields_in_a_content_type is
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:
Darrell Duane
d@duane.com
Setting the field to disabled
Setting the field to disabled and hidden autofills it with the set default value set in the UI.
At least for a date field.
Freelance Drupal developer, contact me if you want help on your Drupal project.
YMMV
YMMV for other field types.
It works. Thanks
+1
Thank you for the solution .
Thank you for the solution .. :)
Thank you for the solution .
Thank you for the solution .. :)