4

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?

enter image description here

5 Answers 5

8
+50

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):

screenshot

I've tested both add and edit node forms and they seem to work perfectly using this method.

5
  • you are a genius! Your solution worked perfectly! Thanks a bunch!!!
    – uwe
    Commented Jun 2, 2012 at 3:29
  • @MotoTribe No worries, glad it helped :)
    – Clive
    Commented Jun 2, 2012 at 8:07
  • Any idea at all how do you do this for a file? The upload / remove is broken before it seems to hit form validate... Seems to be a nightmare
    – WestieUK
    Commented Jun 22, 2012 at 13:14
  • @WestieUK I'm struggling to picture what you're trying to do...would you be able to post a new question with a screenshot of what-you-want-get-where (the same as Mototribe has done would be v helpful). I'll be glad to run a couple of tests if you do. I suspect it's to do with the order validation functions are run in
    – Clive
    Commented Jun 22, 2012 at 13:18
  • 1
    I would add to this answer in validation function: $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
0

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.

2
  • The problem is that $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
    – Clive
    Commented May 29, 2012 at 17:20
  • Ugh, what a pain.
    – cam8001
    Commented May 29, 2012 at 17:21
0
$('#field-whatevet').appendTo('fieldset#whatever');

eh eh

I hope this helps you.

3
  • What? Don't you like jQuery? Commented Jun 1, 2012 at 8:22
  • I think this is a slick shortcut, especially given how complex the "Drupal way" is ;-)
    – uwe
    Commented Jun 2, 2012 at 3:30
  • Of course it is a shortcut. It is usefull for the meantime you find the php solution. Cheers Commented Jun 6, 2012 at 10:57
0
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();
0

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.