function og_form_add_og_audience in Organic groups 5.2
Same name and namespace in other branches
- 5.8 og.module \og_form_add_og_audience()
- 5 og.module \og_form_add_og_audience()
- 5.3 og.module \og_form_add_og_audience()
- 5.7 og.module \og_form_add_og_audience()
- 6.2 og.module \og_form_add_og_audience()
- 6 og.module \og_form_add_og_audience()
Helper method to add OG audience fields to a given form. This is lives in a separate function from og_form_alter() so it can be shared by other OG contrib modules.
1 call to og_form_add_og_audience()
- og_form_alter in ./
og.module
File
- ./
og.module, line 1488
Code
function og_form_add_og_audience($form_id, &$form) {
global $user;
if ($_SERVER["REQUEST_METHOD"] == 'GET') {
$gids = $_GET['gids'];
}
elseif ($_POST['og_groups_hidden']) {
$gids = unserialize($_POST['og_groups_hidden']);
}
$node = $form['#node'];
$required = variable_get('og_audience_required', 0) && !user_access('administer nodes');
// determine the list of groups that are shown. node admins see all of them
if (user_access('administer nodes')) {
$options = og_all_groups_options();
}
else {
$subs = og_get_subscriptions($node->uid);
foreach ($subs as $key => $val) {
$options[$key] = $val['title'];
}
}
// get the visibility for normal users
$vis = variable_get('og_visibility', 0);
// override visibility for og admins and when author only has 1 group
if (user_access('administer organic groups') && $vis < 2) {
$vis = $vis == OG_VISIBLE_GROUPONLY ? OG_VISIBLE_CHOOSE_PRIVATE : OG_VISIBLE_CHOOSE_PUBLIC;
}
elseif (!count($options)) {
// don't show checkbox if no subscriptions. must be public.
$vis = OG_VISIBLE_BOTH;
}
switch ($vis) {
case OG_VISIBLE_BOTH:
$form['og_nodeapi']['og_public'] = array(
'#type' => 'value',
'#value' => 1,
);
break;
case OG_VISIBLE_GROUPONLY:
$form['og_nodeapi']['og_public'] = array(
'#type' => 'value',
'#value' => 0,
);
break;
//user decides how public the post is.
case OG_VISIBLE_CHOOSE_PUBLIC:
$form['og_nodeapi']['visible']['og_public'] = array(
'#type' => 'checkbox',
'#title' => t('Public'),
'#default_value' => $node->nid ? $node->og_public : 1,
'#description' => t('Show this post to everyone, or only to subscribers of the groups checked above. Posts without any groups are always <em>Public</em>.'),
'#weight' => 2,
);
break;
case OG_VISIBLE_CHOOSE_PRIVATE:
$form['og_nodeapi']['visible']['og_public'] = array(
'#type' => 'checkbox',
'#title' => t('Public'),
'#default_value' => $node->nid ? $node->og_public : 0,
'#description' => t('Show this post to everyone, or only to subscribers of the groups checked above. Posts without any groups are always <em>Public</em>.'),
'#weight' => 2,
);
break;
}
// show read only item if we are non-admin, and in simple mode (i.e. non-checkboxes) and at least one group is in querystring
$simple = !user_access('administer organic groups') && !variable_get('og_audience_checkboxes', TRUE) && count($gids);
// determine value of audience multi-select
if (count($options) == 1 && $required) {
$gids = array_keys($options);
$gid = $gids[0];
$groups = array(
$gid,
);
// also show read only mode if user has 1 option and we are in required mode
$simple = TRUE;
}
elseif ($gids) {
// populate field from the querystring if sent
$groups = $gids;
if (!user_access('administer nodes')) {
// filter out any groups where author is not a member. we cannot rely on fapi to do this when in simple mode.
$groups = array_intersect($gids, array_keys($options));
}
}
elseif ($node->nid || $node->og_groups) {
$groups = $node->og_groups;
}
else {
$groups = array();
}
if ($simple) {
// 'simple' mode. read only.
if (count($groups)) {
foreach ($groups as $gid) {
$titles[] = $options[$gid];
$item_value = implode(', ', $titles);
}
$form['og_nodeapi']['visible']['og_groups_visible'] = array(
'#type' => 'item',
'#title' => t('Audience'),
'#value' => $item_value,
);
$assoc_groups = drupal_map_assoc($groups);
// this hidden element persists audience values during a Preview cycle. avoids errors on Preview.
$form['og_nodeapi']['invisible']['og_groups_hidden'] = array(
'#type' => 'hidden',
'#value' => serialize($assoc_groups),
);
// this 'value' element persists the audience value during submit process
$form['og_nodeapi']['invisible']['og_groups'] = array(
'#type' => 'value',
'#value' => $assoc_groups,
);
}
}
elseif ($cnt = count($options)) {
drupal_add_js(drupal_get_path('module', 'og') . '/og.js');
// show multi-select. if less than 20 choices, use checkboxes.
$type = $cnt >= 20 ? 'select' : 'checkboxes';
$form['og_nodeapi']['visible']['og_groups'] = array(
'#type' => $type,
'#title' => t('Audience'),
'#attributes' => array(
'class' => 'og-audience',
),
'#options' => $options,
'#required' => $required,
'#description' => format_plural(count($options), 'Show this post in this group.', 'Show this post in these groups.'),
'#default_value' => $groups,
'#required' => $required,
'#multiple' => TRUE,
);
}
else {
if ($required) {
form_set_error('title', t('You must !join before posting a %type.', array(
'!join' => l(t('join a group'), 'og'),
'%type' => node_get_types('name', $node->type),
)));
}
}
if (count($form['og_nodeapi']['visible']) > 1) {
$form['og_nodeapi']['#type'] = 'fieldset';
$form['og_nodeapi']['#title'] = t('Groups');
$form['og_nodeapi']['#collapsible'] = TRUE;
$form['og_nodeapi']['#collapsed'] = $gids ? TRUE : FALSE;
}
}