function flag_field_attach_form in Flag 7.2
Same name and namespace in other branches
- 7.3 flag.module \flag_field_attach_form()
Implements hook_field_attach_form().
Handles the 'show_on_form' flag option.
Warning: will not work on entity types that are not fieldable, as this relies on a field module hook.
See also
File
- ./
flag.module, line 384 - The Flag module.
Code
function flag_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
// Nodes have their own special form handling: see flag_form_alter().
if ($entity_type == 'node') {
return;
}
list($id) = entity_extract_ids($entity_type, $entity);
// Some modules are being stupid here. Commerce!
if (empty($id)) {
$id = NULL;
}
// Get all possible flags for this content-type.
$flags = flag_get_flags($entity_type);
// Filter out flags which need to be included on the node form.
$flags_in_form = 0;
$flags_visible = 0;
foreach ($flags as $flag) {
if (!$flag->show_on_form) {
continue;
}
// Get the flag status.
// We don't have per-bundle defaults on general entities yet.
$flag_status = $id ? $flag
->is_flagged($id) : FALSE;
// If the flag is not global and the user doesn't have access, skip it.
// Global flags have their value set even if the user doesn't have access
// to it, similar to the way "published" and "promote" keep the default
// values even if the user doesn't have "administer nodes" permission.
global $user;
$access = $flag
->access($id, $flag_status ? 'unflag' : 'flag');
if (!$access && !$flag->global) {
continue;
}
$form['flag'][$flag->name] = array(
'#type' => 'checkbox',
'#title' => $flag
->get_label('flag_short', $id),
'#description' => $flag
->get_label('flag_long', $id),
'#default_value' => $flag_status,
'#return_value' => 1,
);
// If the user does not have access to the flag, set as a value.
if (!$access) {
$form['flag'][$flag->name]['#type'] = 'value';
$form['flag'][$flag->name]['#value'] = $flag_status;
}
else {
$flags_visible++;
}
$flags_in_form++;
}
if ($flags_in_form) {
$form['flag'] += array(
'#weight' => 1,
'#tree' => TRUE,
);
}
if ($flags_visible) {
$form['flag'] += array(
'#type' => 'fieldset',
'#title' => t('Flags'),
'#collapsible' => TRUE,
);
}
}