is there any way to unset a drupal 7 form field like username, email, etc... or to make it invisible ?

best regards

Comments

boran’s picture

Did you find a solution?
I had a similar question. In _form_alter() I want to hide the username of the form when one adds a comment.
The following changes the form variable:

  if ($form_id == 'comment_node_article_form') {
    unset($form['author']['_author']);      // hide user
  }

But it does not affect what is displayed. (In D6 the solution was unset($form['_author']) ).

boran’s picture

Apparently one should not do a unset() to hide form elements, but set .
(from reading the book Drupal-7-Module-Development).
So I tried folllowing, but the username/subject is still not hidden...
$form['author']['_author']['#access']=FALSE;
$form['subject']['#access']=FALSE;

Update: found the issue for me. I had :
_form_alter($form, $form_state, $form_id)
instead of
_form_alter(&$form, $form_state, $form_id)
(note the missing "&").
So using "#acces => FALSE" does work!

tinny’s picture

$form['subject']['#access']=FALSE;

worked for me! thanks.

mototribe’s picture

thank you, that worked for me too. I had used unset and got error messages on the form validation page.

weri’s picture

I think the right way to do this in Drupal 7 is to use the new hide() function:

For example:

  hide($form['subject']);
neurobotic’s picture

The previous snippet will work. Example:

$form['field_mycustomfield']['#access'] = FALSE;
Saoirse1916’s picture

Worked fine for me -- I was doing <?php $form['author']['_author']['#type'] = 'hidden'; ?> but the hide() function works just as well and I'm glad to know there's an official Drupal way to do this. Since the OP was asking about unset, just a word of caution -- using unset on the author field means the post will be submitted as anonymous, which was a problem for me.

__andrew__’s picture

Ok so there is ...

unset($form['field_foo']);
hide($form['field_foo']);
$form['field_foo'] = null;
$form['field_foo'] = 'hidden';

any others?
Perhaps there is a standard or should we start a new web site devoted to hiding a fields in drupal....

bojanz’s picture

You NEVER do:
unset($form['field_foo']);
hide($form['field_foo']);
$form['field_foo'] = null;
$form['field_foo'] = 'hidden';

If you're altering a form (hook_form_alter()), you set #access to FALSE.
If you're in the theming layer (theme function or file), you can use hide().
The semantics are different even them, #access means "can't touch this", while hide() means "ignore this for now, I'll just render it later myself".

chrisroane’s picture

Especially when modifying a content type form in Drupal 7, you DO NOT want to unset the variable. This is the correct way, like what was previously mentioned:

$form['title']['#access'] = FALSE;

In the above case I am hiding the title field from the form, and I am also adding a submit handler that I then use to programmatically set the node title.

nitin.k’s picture

I was looking for this solution from a long time.

Many Thanks.

cuebix’s picture

I know several others have commented that you shouldn't remove a field from $form, but no one has explained why.

I had a use case for removing the field because #access => false wasn't working as expected and hiding it caused problems, too.

In my case I wanted to remove the default field that Drupal adds when you have a field with unlimited cardinality, regardless of whether you're editing an existing node or adding a new one.

Here's what I did:

function mymodule_form_FORM_ID_form_alter(&$form, &$formState) {
	if ($form['#node_edit_form'] && !$formState['rebuild']) {
		$lang = $form['language']['#value'];
		foreach ($form['field_myfield'][$lang] as $key => &$f) {
			if (is_int($key) && array_key_exists('#entity', $f) && !$f['#entity']->item_id) {
				$unsetKey = $key;
			}
		}

		if (isset($unsetKey)) {
			// remove field from form
			$field = $form['field_myfield'][$lang][$unsetKey];
			unset($form['field_myfield'][$lang][$unsetKey]);
			$form['field_myfield'][$lang]['#max_delta']--;

			// update field state
			$fieldState = field_form_get_state(
				$field['#field_parents'], $field['#field_name'], $field['#language'], $formState
			);
			$fieldState['items_count']--;
			field_form_set_state(
				$field['#field_parents'], $field['#field_name'], $field['#language'], $formState, $fieldState
			);
		}
	}
}
jaypan’s picture

I know several others have commented that you shouldn't remove a field from $form, but no one has explained why.

Validate and submit functions will often expect a value, so if it's been unset, rather than just hidden, the system will often throw up warnings and/or errors.


Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
alfaguru’s picture

Yes, this is one of the cases where unsetting the form entry is pretty harmless. You aren't really unsetting a field per se, just one of its component parts which you don't require.

gChen07’s picture

..I am currently using D7.. but I can't control access to a certain field.. how can I display field link.. but restrict access to unregistered user.. I found this forum but I can't find codes to restrict access to field depend upon the user status.. i also found field permission module but only works for drupal 6.. Thanks in advance..

jaypan’s picture

You can do something like this (show only to logged in users):

$form['element']['#access'] = user_is_logged_in();

user_is_logged_in() is a function that returns TRUE or FALSE, so if the user is logged in they will be allowed to see the form element, and if they are not logged in, they won't.


Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
gChen07’s picture

What file in the system folder should I edit? I created a new node with field for uploading files. I need to restrict the link to unregistered user but let them view the link.. Thanks for help, I see hope now in my project.

jaypan’s picture

You don't edit files in Drupal, you create new modules or themes to accomplish the behavior. If it's functionality you want to persist even if you change the theme, then you will put it in a module. If it's code that should only be there for the current theme, you will put it in your theme.


Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
gChen07’s picture

I am using drupal 7, and the funtionality is not yet included on the available theme and module.. so im trying to edit the actual php code. I saw the field permission module but works only in drupal 6.. Should I downgrade my site to d6?

jaypan’s picture

Again, in Drupal, you don't edit the files. It's basically the number one rule of Drupal modifications.

Drupal is not downgradable, so if you wanted to use D6, you'd have to start from scratch.


Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
gChen07’s picture

So the code you have given earlier should I put it in my theme folder. I am using garland theme

jaypan’s picture

In that case, you'll want to create a sub-theme of garland, and put your code in there.


Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
gChen07’s picture

How can I do that?

jaypan’s picture


Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
gChen07’s picture

Where should I put this codes?

jaypan’s picture

In the template.php file of the sub-theme you create. You'll want to implement hook_form_alter().


Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
gChen07’s picture

can I just edit the original garland theme template.php?

jaypan’s picture

You can do whatever you want - the files are yours. But if you want to do it the correct way, you will need to do what I said above. If you edit the files in garland, they will be overwritten on your next update, and you'll have to do it again (each time you update). If you create a sub-theme, you'll don't have to worry about overwriting your code.


Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
gChen07’s picture

I already created a sub-theme.. but I am having this problem..
Undefined index: #secondary in gretch_preprocess_page() (line 60 of C:\xampp\htdocs\ORRUCU\themes\gretch\template.php).
I am trying to fix it now.. but my main concern is where to edit restriction to field.. since it is the main problem.. thanks your a big help

jaypan’s picture

Can't help without any code. What does the code around that error look like?


Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
gChen07’s picture

..I already fix the error.. I have now new sub-theme. My problem now is still how can I restric access to only one filed in my new content type(node). It is a file field containing pdf file and it should not be accessible to unregistered user..

jaypan’s picture


Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.