function og_menu_node_prepare in Organic Groups Menu (OG Menu) 7.3
Same name and namespace in other branches
- 7.2 og_menu.module \og_menu_node_prepare()
Implements hook_node_prepare().
File
- ./
og_menu.module, line 687 - Integrates Menu with Organic Groups. Lots of menu forms duplication in OG context.
Code
function og_menu_node_prepare($node) {
$type = $node->type;
// $node is a group type.
if (og_is_group_type('node', $type)) {
// New node.
if (empty($node->nid)) {
$node->og_menu = (bool) variable_get('og_menu_create_by_default', FALSE);
}
else {
$menus = og_menu_get_group_menus(array(
'node' => array(
$node->nid,
),
));
$node->og_menu = !empty($menus);
}
}
// $node is a group content type.
if (og_is_group_content_type('node', $type) && variable_get('og_menu_enable_' . $type, FALSE)) {
$context = og_context();
$menus = array();
$groups = array();
// Get all groups that have menus we can add links to.
if (!variable_get('og_menu_context_limit', FALSE)) {
$og_fields = og_get_group_audience_fields('node', $type);
foreach ($og_fields as $field_name => $label) {
$field = field_info_field($field_name);
$target_type = $field['settings']['target_type'];
// The handler delivers all available targets for each content type, skip
// the ids if we already have that type's results.
if (empty($groups[$target_type])) {
$instance = field_info_instance('node', $field_name, $type);
// Using the handler allows us to get user options from OG without
// running through all the user's groups.
$ids = entityreference_get_selection_handler($field, $instance)
->getReferencableEntities();
if (!empty($ids)) {
$field_gids = array();
foreach ($ids as $key => $values) {
$field_gids += $values;
}
// Users with global administer menu or administer og menu permission can access everything.
if (!(user_access('administer menu') || user_access('administer og menu'))) {
if (!empty($field_gids)) {
foreach ($field_gids as $gid => $name) {
// Check if user has access to the specific menu
if (!og_user_access($target_type, $gid, 'administer og menu')) {
unset($field_gids[$gid]);
}
}
}
}
// Create an array similar to what og_get_entity_groups() returns.
if (!empty($field_gids)) {
$groups[$target_type] = array_keys($field_gids);
}
}
}
}
}
else {
if (!empty($context)) {
if (user_access('administer og menu') || og_user_access($context['group_type'], $context['gid'], 'administer og menu')) {
$groups[$context['group_type']] = array(
$context['gid'],
);
}
}
}
// Get all menus for available groups.
if (!empty($groups)) {
$menus = og_menu_get_group_menus($groups);
}
// Store the menus for later use in form_alter and form_validate
$node->storage['og_menu'] = $menus;
// $node is not a new node and menu link is not set.
if (!empty($node->nid) && empty($node->menu['link_title']) && !empty($menus)) {
$menu_names = array();
foreach ($menus as $menu) {
$menu_names[] = $menu['menu_name'];
}
// This query comes from menu.modules node_prepare, and is how it does it.
$mlid = db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = :path AND module = 'menu' AND menu_name IN (:type_menus) ORDER BY mlid ASC", 0, 1, array(
':path' => 'node/' . $node->nid,
':type_menus' => $menu_names,
))
->fetchField();
if ($mlid) {
// We've found something, so load the item and set that in the node
// form.
$item = menu_link_load($mlid);
$options = menu_parent_options(array(
$item['menu_name'],
), $item);
if (!empty($options)) {
$node->menu = $item;
// Find the depth limit for the parent select.
if (!isset($node->menu['parent_depth_limit'])) {
$node->menu['parent_depth_limit'] = _menu_parent_depth_limit($node->menu);
}
}
}
}
}
}