| Project: | Flag |
| Version: | 7.x-2.x-dev |
| Component: | Miscellaneous |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Issue Summary
On the node create/edit form, is it possible to relocate the checkbox for a flag from out of its normal "Flags" fieldset, and into a different fieldset on the node form where it would be most applicable?
For instance, I have a custom fieldset/group in my content type called "Slideshow" where a variety of fields can be filled in if the user wants to show the node in the front page slideshow on the site. In order to get into the slideshow in the first place though, a node also has to have its "Slideshow" flag checked. However, right now the Slideshow flag is in a separate fieldset from the rest of my related settings, and therefore very easy for content editors to overlook or get confused.
From within my module, I see I can access the Flag checkbox with hook_form_alter and $form['flag']['slideshow'];. I can unset it here, or change it in various ways, though I'm not sure if or how to relocate it into a different fieldset in the form.
Any ideas would be greatly appreciated! :) Thanks!
Comments
#1
Just move it to the new location:
<?php$form['some_fieldset']['slideshow'] = $form['flag']['slideshow'];
unset($form['flag']['slideshow']);
?>
However this will assign new parents to the element since it's been moved, you'll probably need to do the above plus adding in the original parents:
<?php$form['some_fieldset']['slideshow'] = $form['flag']['slideshow'];
$form['some_fieldset']['slideshow']['#parents'] = array('flag', 'slideshow');
unset($form['flag']['slideshow']);
?>
#2
Works like a charm - thanks very much Nathan :)
#3
I ran into a similar issue when I was asked to combine two checkboxes within a single fieldset. One of those fieldsets was a CCK field, and other was created by the flag module. Initially they didn't want to play together. If I used hook_form_alter to move the CCK field into the flag's existing "flag" fieldset, I got an error message upon form submission. (I think this is because flag_nodeapi() assumes that each element inside $node->flag is going to be a flag, and therefore barfs if the flag fieldset contains anything that isn't a flag.) However, I got a different error if I tried to go the other way -- moving the flag into a fieldset with the CCK field.
After reading this discussion, I was able to combine the CCK field and the flag into a single fieldset using the following implementation of hook_form_alter. My CCK field is named "field_hide_upcoming_events", and my flag is named "feature_on_front_carousel":
function mymodule_form_alter(&$form, $form_state, $form_id) {if ( $form['#id'] == 'node-form' && $form['type']['#value'] == 'initiative' ) {
// Some trickery to put CCK field_hide_upcoming_events in the same fieldset
// as the feature_on_front_carousel flag
$form['flag_plus'] = array(
'#type' => 'fieldset',
'#title' => t('Flags'),
'#weight' => 0,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['flag_plus']['feature_on_front_carousel'] = $form['flag']['feature_on_front_carousel'];
$form['flag_plus']['feature_on_front_carousel']['#parents'] = array('flag', 'feature_on_front_carousel');
unset($form['flag']);
$form['flag_plus']['field_hide_upcoming_events'] = $form['field_hide_upcoming_events'];
unset($form['field_hide_upcoming_events']);
}
}
#4
Am I dreaming, or there is a module that listed the Flag like a field in a content type Manage Fields list?
This allows you drag and drop it to relocate it in the list.
I am pretty sure I used this on a previous site.
#5
I must be crazy ... The Flag field is there in the site I work on, I believe it's a built in feature. I only seen it after I posted the above :-s
#6
Does this also work when you create a forms API field (in this case an item field) and you want to insert it into an existing cck built fieldset?
If so, where would I get the info to populate the parent array that needs to be moved over, if, as far as I know, it has never been assigned to a specific prior parent?
Rob
#7
How would this solution differ for Drupal 7?
#8
I am also interested in a Drupal 7 version (got errors using the above mentioned code).