You are here

function og_menu_node_form_validate in Organic Groups Menu (OG Menu) 6.2

Same name and namespace in other branches
  1. 6 og_menu.module \og_menu_node_form_validate()
  2. 7.3 og_menu.module \og_menu_node_form_validate()
  3. 7.2 og_menu.module \og_menu_node_form_validate()
1 string reference to 'og_menu_node_form_validate'
og_menu_form_alter in ./og_menu.module
Implementation of hook_form_alter().

File

./og_menu.module, line 522
Integrates Menu with Organic Groups. Lots of menu forms duplication in OG context.

Code

function og_menu_node_form_validate($form, &$form_state) {

  // If parent is empty, we remove the link title to force menu module to
  // not create a menu entry.
  if (empty($form_state['values']['menu']['parent'])) {
    $form_state['values']['menu']['link_title'] = '';
  }
  else {
    $menus = og_menu_get_group_menus();
    $parents = explode(':', $form_state['values']['menu']['parent']);
    $parent = $parents[0];
    $gids = array();
    $has_menu_access = FALSE;

    // Populate $gids with selected groups
    foreach ($form_state['values']['og_groups'] as $gid => $enabled) {
      if ($enabled) {
        $gids[] = $gid;
      }
    }
    foreach ($menus as $menu) {
      if ($menu['menu_name'] == $parent) {

        // Check if user has access to the chosen menu parent
        $has_menu_access = TRUE;

        // Check if menu belongs to one of the selected groups
        if (!in_array($menu['gid'], $gids)) {
          form_set_error('og_groups', t('The menu you chose does not belong to the selected groups.'));
        }
      }
    }
    if (!$has_menu_access) {
      form_set_error('menu][parent', t('You cannot add menu items to this menu. Choose another parent menu.'));
    }
  }
}