I have some require fields in some form,I want them be required in somewhere and in another place not be required (I want exactly them , maybe you suggest me use existance field in cck but this fields not in cck.)
I used profile2 module with some profile type, I want in profile type edit page as example name field be required and in user registration name field not be required.
I try to alter #require property of name field in form alter, it's changed correctly but in validation tell me the name field is required.
in
function mymodule_form_alter(&$form,&$form_state) {
if ($form_id == 'user_register_form') {
$form['profile_detailp']['field_name']['und']['#required']=0;
...
}
}
any idea ?
Your method is ok, and have to works but maybe in another module it require again.
function mymodule_form_alter(&$form,&$form_state) {
if ($form_id == 'user_register_form') {
$form['profile_detailp']['field_name']['und']['#required']=0;
...
}
}
Text field need do it like this
function mymodule_form_alter(&$form,&$form_state) {
if ($form_id == 'user_register_form') {
$form['profile_detailp']['field_name']['und'][0]['value']['#required']=0;
}
}
Related
I am trying to hide a field collection field that renders in an embedded form. This form renders on a few different nodes but only some nodes need all the fields, so I would like to hide them on ones that don't.
Right now I am trying to do it vis hook form alter but it doesn't work inside the field collection, it will work on a form rendered normally that shares fields with the collection.
function zenstrap_form_alter(&$form,&$form_state,$form_id){
//Form we want to target
case ($form_id==="coh_pow_node_form"):
//Appears in Normal form and Field Collection
//Hides in normal
$form['field_last_name']['#access']=FALSE;
//Appears in Normal form and Field Collection
//Hides in normal
$form['field_street']['#access']=FALSE;
//Appears in Field Collection
//Does nothing
$form['field_veteran_retired']['#access']=FALSE;
break;
}
To Hide field collection fields check the code below.
function YOURMODULE_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'YOURFORMID') {
$delta = 0;
$max_delta = $form['field_YOUR_field_collection'][LANGUAGE_NONE]['#max_delta'];
while ($delta <= $max_delta) {
$form['field_YOUR_field_collection'][LANGUAGE_NONE][$delta]['field_YOURfield'][LANGUAGE_NONE][0]['#access'] = FALSE;
$delta++;
}
}
}
Hope it helps you...
i'm on Drupal 7 and i have aspecific tpl.php file for a content field_image: "field--field_image.tpl.php". I need to create a preprocess function for this field and for my theme.
Supposing my theme name is "My Theme"
It should look like
function my_theme_preprocess_field(&$variables, $hook) {
$variables['classes_array'][] = 'aClassName';
}
but it doesn't work. I am wrong. But where?
Thanks
You can use template_preprocess_field() (like you do in your code above) but just test the particular field is the right one for you:
function my_theme_preprocess_field(&$variables, $hook) {
$element = $variables['element'];
if (isset($element['#field_name'])) {
if ($element['#field_name'] == 'field_image') {
$variables['classes_array'][] = 'aClassName';
}
}
}
Once you've implemented the hook don't forget to clear your caches, hook implementations are cached in Drupal 7 so won't be picked up until the cache is cleared.
You could declare a mytheme_preprocess_field(&$variables, $hook) in your theme's template.php where you can check your field and do operations on its label or markup, add classes, anything. So you wouldn't need field specific tpls. - eg.
function mytheme_preprocess_field(&$variables, $hook) {
if ($variables['element']['#field_name'] == 'field_machine_name') {
$variables['items'][0]['#markup'] = 'add custom markup';
}
}
Hope this helps someone.
In drupal 7 you can rewrite output of the field in template_preprocess_node() by altering "#markup" value of the field.
Also you can use regexp to change whatever you want in page content :)
I want to make the file attachment option as a required field for a specific content type, user could not submit the node form without an attachment.
the way i am doing this is not working, pls guide me if i am doing it wrong way.
function ims_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'media_content_node_form':
unset($form['buttons']['preview']);
if(is_numeric(trim(arg(3))))
{
$arg_nid = arg(3);
$form['field_media_model']['#default_value'][0]['nid'] = $arg_nid;
}
switch($form['#id'])
{
case "node-form":
$form['attachments']['#required'] = true;
break;
}
break;
}
}
I find that life in Drupal is easier when using FileField instead of Drupal's core Upload module. With FileField you can create a CCK field (FileField) on your content type and make that field required just like any other CCK field. This approach doesn't require writing a single line of code.
However, if you must use Drupal's core Upload module then you can use hook_form_alter to accomplish this, for example:
function my_module_form_alter(&$form, &$form_state, $form_id) {
switch ($form['#id']) {
case "node-form":
switch ($form['type']['#value']) {
case "my_node_type":
$form['attachments']['#required'] = true;
break;
}
break;
}
}
Yes it is possible.
Check for the form id. Usually you can get the name by inspecting the form and getting the id. Replace '-' by '_'. This gives you the form id for the form.
Put a conditional check if form id == . Use krumo($form) (enable devel module before this step).
krumo() give you the list of all the field names. Now, $form[<field_name>]['#required'] = 1;
Hope this helps!!
hey i got a contnet type that i made and i want to twweak it some , when i do form alter its all good but once i do submit its ignore my changes that i added a field for example, how can i do that changes and tell him to insert also that field? do i need to put all the fields again in hook_submit?
$function YOURMODULE_form_alter(&$form, &$form_state) {
//...
$form['#submit'][] = 'YOURMODULE_submitfunction';
//...
}
function YOURMODULE_submitfunction($form, &$form_state) {
// Save your own changes here to DB or something other
}
For example, in the registration form, there is "Username" and the text field for it which has the input type="text" name="name" ....
I need to know how can I get the title from the input field's name.
I'm expecting a function like:
$title = get_title_for_element('name');
Result:
assert($title == 'Username'); // is true
Is there something like this in Drupal?
Thanks.
You have the form and the form state variables available to your validation function. You should use form_set_error() to set the error.
There is no function that I am aware of which will map from the values array to the form array. But it is not dificult to work it out. Understanding the form data structure is one of the key skills you need when building drupal.
In this case the form in question is generated (in a roundabout way) by user_edit_form, you can see the data structure in there.
$form['account']['name'] is the username field. and the array key for the title is '#title' as it will be in most cases for form elements.
You can do it in two different ways as I see it. Let's create a module called mycustomvalidation.module (remember to create the mycustomvalidation.info file also).
Note: The code below has not been tested, so you might have to do some minor adjustments. This is Drupal 6.x code by the way.
1) Using hook_user()
What you need is a custom module containing your own implementation of hook_user() http://api.drupal.org/api/function/hook_user/6.
<?php
function mycustomvalidation_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'validate') {
// Checking for an empty 'profile_fullname' field here, but you should adjust it to your needs.
if ($edit['profile_fullname'] != '') {
form_set_error('profile_fullname', t("Field 'Fullname' must not be empty."));
}
}
}
?>
2) Using form_alter() and a custom validation function
Personally, I would go for this option because I find it cleaner and more "correct". We're adding a custom validation function to our profile field here.
<?php
function mycustomvalidation_form_alter(&$form, $form_state, $form_id) {
// Check if we are loading 'user_register' or 'user_edit' forms.
if ($form_id == 'user_register' || $form_id == 'user_edit') {
// Add a custom validation function to the element.
$form['User information']['profile_fullname']['#element_validate'] = array('mycustomvalidation_profile_fullname_validate');
}
}
function mycustomvalidation_profile_fullname_validate($field) {
// Checking for an empty 'profile_fullname' field here, but you should adjust it to your needs.
if ($field['#value'] != '') {
form_set_error('profile_fullname', t("Field %title must not be empty.", array('%title' => $field['#title']));
}
}
?>