9

This has been troubleing me for a while.. When i try to hide fields like the group audience field, or the publishing setting from a form, on validation the form set both of these field to a null value, even though the defaut value as been correctly declared.

I tried to use Invisible, #access, unset hidden nothing work! This is very troubleing because for my current project users can edit only small bits of their profile at the time, for instance just the address or the picture, so if i hide group audience the user ends up kicked out of the groups he belongs at every edit.

I ended up using the code below to hide these field while still keeping the user in their group.

$variables['user_edit_about']['group_audience']= $temp['group_audience'];       
$variables['user_edit_about']['group_audience']['#prefix'] = '<div style="display:none;">';
$variables['user_edit_about']['group_audience']['#suffix'] = '</div>';

Would anyone think of a better way to achieve this ?

Thanks

1
  • This answer is not perfect because use can still change the value with DOM manipulation
    – iStryker
    Commented Jul 17, 2011 at 13:32

2 Answers 2

6

Edit:

Ok I figured it out. Put this in your custom module:

function example_form_alter(&$form, &$form_state, $form_id) {
    if($form_id == 'story_node_form') {
        $form['title']['#value'] = 'hot stuff';
        $form['title']['#access'] = FALSE;

    }
}

This will set the title of the story node to 'hot stuff' and it will NOT be render on the page because #access = FALSE;

You can add more granularity control inside hook_form_alter() like

  global $user;

  // Check to see if $user has does not have the administrator role.
  if (!in_array('administrator', array_values($user->roles))) {
    $form['title']['#access'] = FALSE;
  }

This works for Drupal 6 & 7.

Old Answer (ignore):

Its been awhile since I did something like this, so I may have it wrong. If you set the [#value] of a form item, it will not be visible on the form.

I did this in Drupal 6, hook_form_alter() in a custom module. I don't know if it will work in Drupal 7 and I don't know if you can do it in a preprocess function

4
  • Thank you for your answer, although it unfortunately did not work in my case, the form fields already have a #value pre filed as they are edit forms
    – silkAdmin
    Commented Jul 17, 2011 at 2:40
  • @silkAdmin Please see new edits
    – iStryker
    Commented Jul 17, 2011 at 13:25
  • Thanks Stryker for taking the time to dig around that, event though your above techinic does work do hide fields it seems that the default value is not taken into account on submit either. So i end up with user still getting kicked out of their group at every profile edit. So far only the #prefix # suffix trick seems to make it behave normally.
    – silkAdmin
    Commented Jul 18, 2011 at 13:07
  • do $form['title']['#value'] = $form['title']['#default_value']
    – iStryker
    Commented Jul 18, 2011 at 13:55
5

You can also just add the 'element-hidden' CSS class to the form element

$form['user_edit_about']['group_audience']['#attributes']['class'][] = 'element-hidden';

As iStryker mentioned previously, it would still be possible to change the value through DOM manipulation. However, this is a nice, easy way to hide a form field (or any other render element). The element-hidden class is defined in system.base.css.

1
  • Buddy, you saved me. Thanks. All the other methods I've seen don't output the elements on the form itself. In my case, I want to hide form elements in a contrib module which expects those elements to be in the form otherwise it fails to work properly. Your CSS fix hides the unwanted elements but everything still works. Thanks for also noting that this is not "hacker-proof" and only works whenever all you need is to simplify the interface you present to trusted editor users.
    – Mario Awad
    Commented Apr 23, 2015 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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