By josh123123 on
is there any way to unset a drupal 7 form field like username, email, etc... or to make it invisible ?
best regards
is there any way to unset a drupal 7 form field like username, email, etc... or to make it invisible ?
best regards
Comments
Did you find a solution? I
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:
But it does not affect what is displayed. (In D6 the solution was unset($form['_author']) ).
Apparently one should not do
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!
works
$form['subject']['#access']=FALSE;
worked for me! thanks.
Drupal 7
thank you, that worked for me too. I had used unset and got error messages on the form validation page.
Use hide()
I think the right way to do this in Drupal 7 is to use the new hide() function:
For example:
hide() does not render
The previous snippet will work. Example:
Worked fine for me -- I was
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.one ways as good as another
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....
You NEVER
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".
The Correct Solution
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.
That`s the correct way
I was looking for this solution from a long time.
Many Thanks.
A solution
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:
I know several others have
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.
Yes, this is one of the cases
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.
Please help
..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..
You can do something like
You can do something like this (show only to logged in users):
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.
What file in the system
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.
You don't edit files in
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.
I am using drupal 7, and the
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?
Again, in Drupal, you don't
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.
But the code you have given
So the code you have given earlier should I put it in my theme folder. I am using garland theme
In that case, you'll want to
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.
How can I do that?
How can I do that?
Lots of information on that:
Lots of information on that: http://www.google.com/search?sclient=psy-ab&hl=en&tbo=d&site=&source=hp&...
Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
Where should I put this
Where should I put this codes?
In the template.php file of
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.
can I just edit the original
can I just edit the original garland theme template.php?
You can do whatever you want
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.
I already created a
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
Can't help without any code.
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.
..I already fix the error.. I
..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..
http://drupal.org/documentati
http://drupal.org/documentation/modules/file
Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.