I've installed the following module - http://drupal.org/project/og_reg_keys This module adds an additional field to your Organic Group Node types, to allow auser to specify a registration key for users to use to join the group.
The problem is that field is not required to be entered by the user.How can one make this field a required field ?
I found the below code, which makes the CCK field mandatory for users of a specific role, but being a non PHP person I have no idea how to change this to:
Make the Group registration key a required field (not sure what the $form element would be called or where to find this)
To remove the section on the code where it applies to users of a specific role, so that it always applies.
Code:
function mymodule_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'profile_node_form':
global $user;
if(in_array('targetrole', $user->roles)) {
$form['field_profile_pic'][0]['#required'] = 'TRUE';
$form['#field_info']['field_profile_pic']['required'] = '1';
break
Any help would be greatly appreciated. Sorry for the code being so messy, I couldn't seem to paster it correctly, it kept getting cut off.
This should make it required for all users:
function mymodule_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'profile_node_form':
$form['field_profile_pic'][0]['#required'] = 'TRUE';
$form['#field_info']['field_profile_pic']['required'] = '1';
break ;
}
}
Related
I added a Language custom field ( a taxonomy term ) to my user entity and another entity (Activity).
This field can have 2 values, English and French. (User can have both, activity can only have one)
On the Activities view, i added an exposed filter (select) so the user can filter French or English activities.
Result :
I want to hide (or disable) this select if the user only has 1 Language.
How to properly achieve this ? i tried the pre_render hooks but i can't find the right one i guess.
Thank you.
you should try this hook:
function themename_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (in_array($form_id, ['views_exposed_form'])) {
if ($form['#id'] == "views-exposed-form-custom-search-page-1") { // your form id
// your filter logic and return filter form value like
// $form["langugae"]["#options"] = $options;
}
}
}
I've build a custom field with several values. I've to make this field required. But I want to pass the validation if at least one field is filled and the last one is empty.
But my problem is Drupal warn me that the last (empty) field is required. I've thought that the hook_field_is_empty() solved the problem, but, even if return true, the form cannot be validated.
Many thanks for your help.
Implementation :
function MYMODULE_field_widget_form(...) {
$element['address']+=[
...
'#required' => $instance['required'],
];
...
}
function MYMODULE_field_is_empty($item, $field) {
if (empty($item['address']) && empty($item['other'])) {
return true ;
}
return false ;
}
To solve this problem, I've made my field not required (in the definition of the node's fields). Then, I added a callback in the #validate using the hook_form_alter(). In that callback, I test if at least one field is defined and call a form_set_error() if none is defined.
This makes sense because it's not the field itself that could know all the data of the node. But, something is a bit wrong because it's not possible to mark this field as required (e.g. asterisks).
<?php
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($formid == ...) { // a specific case
array_unshift($form['#validate'], '_MYMODULE_validate_form') ;
}
}
function _MYMODULE_validate_form(&$form, &$form_state) {
if (empty($form_state['values']['field_geo'][LANGUAGE_NONE][0]['address'])) {
form_set_error('field_geo', t('You have to define at least one address.')) ;
}
}
?>
i have a cck custom type that is created by people and the fields are filled.
Then someone else edit those nodes and adds more data. I want to save the username of the user editing the content into an hidden field.
i know i can get the user with this:
global $user;
$a = $user->name;
return array(
0 => array('value' => $a)
);
and i have put this as default code for the hidden field, but the field now is filled with the creator of the node, and then is not replaced with the editor.
How can i solve my issue?
i do research on your problem, here is a solution enjoy!!!
Create a custom module and use the following code.
//Implementation of hook_nodeapi()
function hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch($op) {
case 'presave':
if($node->type == "Your content type name")
{
global $user;
//In my case
//$node->field_username[0]['value'] = $user->name;
//In your case it will be like
$node->hidden_field_name[0]['value'] = $user->name;
}
break;
}
}
When you test editing the node yourself, does the field contain your own username or the original author?
An alternative solution is to form_alter the specific node edit form and on node_save, fill in the hidden field with the user name.
Drupal 7. Webforms 3.x.
I am trying to modify a webform component value on submit. I made a custom module called 'mos' and added this code to it.
function mos_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'webform_client_form_43') {
dsm($form['#node']->{'webform'}['components']['1']);
$form['#submit'][] = 'mos_contact_us_submit';
}
}
function mos_contact_us_submit($form, &$form_state) {
$form['#node']->{'webform'}['components']['1'] = 'working#mos.com';
}
However when I look at the results in the database the regular, non-overridden value is stored. Can you help let me know what I am doing wrong?
Eventually I want to take the input value and output an email address based on what was provided (for example. 24 turns into bob#somewhere.com) But I think I can figure this part out myself.
You should to place your submit first.
array_unshift(
$form['actions']['submit']['#submit'],
'mos_contact_us_submit'
);
However, if you want to change some variables in form_state, you should to using custom _valadate function.
I got it! BIG Thanks to #dobeerman for pointing me in the right direction. Here is the code that ended up working:
function mos_form_alter(&$form, &$form_state, $form_id) {
if ('webform_client_form_43' == $form_id) {
//dsm($form);
$form['#validate'][] = 'mos_check_email';
}
}
function mos_check_email(&$form, &$form_state, $form_id) {
$emailVal = $form_state['values']['submitted']['to'];
switch($emailVal) {
case 1: $emailVal = 'email#test.com'; break;
case 2: $emailVal = 'email2#test.com'; break;
case 3: $emailVal = 'email3#test.com'; break;
......
}
$form_state['values']['submitted']['to']=$emailVal;
//dpm($form_state);
}
This way I can keep email address private, but still pass variables to the form with _GET. Kind of a weird situation... but we are trying to keep some existing code intact, so it seemed like the best route.
I accidentally messed up my account creation, so I can't give you the credit dobeerman but I emailed the admins and hopefully I will get it straightened out to get you some rep!
I got dpm($form) working. Nice! This is much better way to view data. I am still trying to figure out where stuff is coming from eg: location longitude & latitude. The word 'longitude' is referenced in 20 different places. I thought this was a likely place to isolate text box for this input field. dpm($form['#field_info']['field_store_latitude']['location_settings']['form']['fields']);
Any tips on how to track down individual input elements?
** this is not an answer, but a supplement to my first question **
hi googletorp -
I am trying to modify existing forms using hook_form_alter.
After several hours of poking around, I can now turn off location (longitude/latitude) section of a form like this:
unset($form['field_store_latitude']);
However, turning off just the latitude like this, does not work:
unset($form['field_store_latitude']['0']['#location_settings']['form']['fields']['locpick']);
I cannot find a easy way to link id and names in html source with arrays produced by Krumo.
In this case id is called "edit-field-store-latitude-0-locpick-user-latitude".
I need a recipe or guidelines for identifying form elemets in Drupal form.
I think I nailed down a solution
<?php
// allows you to alter locations fields, which are tricky to access.
// this will require a patch in location module described here:
// http://drupal.org/node/381458#comment-1287362
/**
* Implementation of custom _element_alert() hook.
*/
function form_overrides_location_element_alter(&$element){
// change some location descriptions
$element['locpick']['user_latitude']['#description'] = ' ' . t('Use decimal notation.');
$element['locpick']['user_longitude']['#description'] = ' ' . t('See <a href=!url target=_blank>our help page</a> for more information.', array('!url' => url('latlon_help')));
// or make them disappear entirely
unset($element['locpick']['user_longitude']);
unset($element['locpick']['user_latitude']);
}
/**
* Implementation of form_alter hook.
*/
function form_overrides_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'user_profile_form':
// change titles in user profile form
$form['account']['name']['#title'] = t('Login Name');
$form['account']['mail']['#title'] = t('Email');
break;
case 'retailer_node_form':
// let's check what is supposed to be here...
print '<pre>';
//print_r($form);
dsm($form);
print '</pre>';
// this works to remove the city
unset($form['field_myvar_latitude']['0']['#location_settings']['form']['fields']['city']);
// let's try #after_build property
$form['#after_build'][]='mymodule_after_build_mynode';
break;
}
}
function mymodule_after_build_mynode($form, $form_values) {
// This will not work for locations fields
return $form;
}`enter code here`
So there is sneaky way to alter the location field, what you need to do is to use the #after_built callback:
/**
* Implements hook_form_alter().
*/
function mymodule_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'x_node_form') {
// alter the location field
if (isset($form['locations'])) {
$form['locations']['#after_build'][] = 'mymodule_alter_location_field';
}
}
}
/**
* Remove the delete checkbox from location element.
*/
function mymodule_alter_location_field($form_element, &$form_state) {
$location = $form_element[0]; // The location field which you can alter
return $form_element;
}