I have a "Birthday" field and the "date" module automatically adds the countainer around it.
Now I'm adding another field "Show Birthday". How can I display that field within the container?
The fieldset is added by the theme_date_combo()
function, which is on the element itself, not the wrapper (i.e. $form['field_birthday']
).
To get this to work you'll need to temporarily move the Show Birthday field into the date_combo
field, like so:
function MYMODULE_form_article_node_form_alter(&$form, &$form_state, $form_id) {
$form['field_birthday'][LANGUAGE_NONE][0]['field_show_birthday'] = $form['field_show_birthday'];
unset($form['field_show_birthday']);
$form['#validate'][] = 'MYMODULE_article_node_form_validate';
}
Importantly, you'll also need to move that field back to where it originally was when the node form is validated. You won't get any errors if you don't, but the field_birthday
value simply won't be saved.
function MYMODULE_article_node_form_validate(&$form, &$form_state) {
$form_state['values']['field_show_birthday'] = $form_state['values']['field_birthday'][LANGUAGE_NONE][0]['field_show_birthday'];
unset($form_state['values']['field_birthday'][LANGUAGE_NONE][0]['field_show_birthday']);
}
The above assumes you're making changes to the Article node form, just replace article
with the name of your content type in the code. If your fields are inside another fieldset you might need to dig a bit further into the form array to find out how to access the precise field, but the logic will remain the same.
This is the result I get with that code (using admin theme):
I've tested both add and edit node forms and they seem to work perfectly using this method.
$form['field_show_birthday'] = $form['field_birthday']['und'][0]['field_show_birthday']; unset($form['field_birthday']['und'][0]['field_show_birthday']);
just to end clean. Otherwise locale module is giving a notice.
Commented
Feb 9, 2016 at 18:58
Some vague memory tells me that Date module does something weird with fieldsets, but this would work in most circumstances.
Implement hook_form_alter in your module, then move your field into the birthday container, something like this:
/**
* Implements hook_form_alter().
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if ($form_id = 'yourform') {
$field_to_move = 'fieldname';
$form['birthday'][$fieldname] = $form[$fieldname];
unset($form[$fieldname]);
}
}
You'll need to use the correct field names of course - an easy way to figure them out is to use dpm($form)
inside your 'if' to see the array keys and values in the form array.
$form['birthday']
and $form[$fieldname]
will both have '#tree' => TRUE
as a property, so you'd have to manipulate the $form_state
during validation and move the field back to the root, so the submit handler will still work properly
$('#field-whatevet').appendTo('fieldset#whatever');
eh eh
I hope this helps you.
var $date_field = $('#edit-field-deadline');
$date_field.find('label').first().html($date_field.find('.fieldset-legend').html());
$date_field.find('.date-padding').appendTo($date_field);
$date_field.find('fieldset').hide();
Check out the drupal form api.
As far as I know, those kind customization fit into the hook_form_FORM_ID_alter in a custom module (you have to create it, but a base module is really easy to do).
Then, as cam8001 suggested, you have to choose how to move the show birthday field where you want - I can suggest you to do a var_dump($form);
to see the form array structure.
Simple example (replace [yourmodulename] and [FORM_ID]!):
function [yourmodulename]_form_[FORM_ID]_alter(&$form, &$form_state){
$form['#after_build'][] = '_choose_a_custom_function_name';
}
function _choose_a_custom_function_name($form, $form_state)
{
// var_dump($form); // need to see the form structure?
// lets move the title and body fields in a group
$form['group_whatever']['title'] = $form['title'];
$form['group_whatever']['body_field'] = $form['body_field'];
// delete the original
unset($form['title']);
unset($form['body_field']);
// remember to return the form!
return $form;
}
I can't give you more specific help because I don't know exactly the field structure used by the date module, so give out a dump and see.
Also, moving a key usually means to duplicate it - remember to unset the original one.