You are here

function og_menu_node_form_validate in Organic Groups Menu (OG Menu) 7.3

Same name and namespace in other branches
  1. 6.2 og_menu.module \og_menu_node_form_validate()
  2. 6 og_menu.module \og_menu_node_form_validate()
  3. 7.2 og_menu.module \og_menu_node_form_validate()

Validation handler for OG group node forms.

We will only end up here if we have confirmed that the node is a group type content

1 string reference to 'og_menu_node_form_validate'
og_menu_form_node_form_alter in ./og_menu.module
Implements hook_form_FORMID_alter().

File

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

Code

function og_menu_node_form_validate($form, &$form_state) {

  // If the user didn't ask for a menu, we have nothing to do.
  if (!isset($form_state['values']['menu']['enabled']) || $form_state['values']['menu']['enabled'] !== 1) {
    return;
  }

  // Available menus were discovered in og_menu_node_prepare().
  $menus = $form['#node']->storage['og_menu'];
  $parents = explode(':', $form_state['values']['menu']['parent']);
  $parent = $parents[0];
  $gids = array();
  $has_menu_access = FALSE;
  $group_is_set = FALSE;
  foreach (og_get_group_audience_fields('node', $form_state['values']['type']) as $field_name => $label) {
    if (isset($form_state['values'][$field_name][LANGUAGE_NONE])) {
      $group_is_set = TRUE;
      foreach ($form_state['values'][$field_name][LANGUAGE_NONE] as $item => $gid) {
        $gids[] = $gid['target_id'];
      }
    }
  }
  if (!$group_is_set) {

    // It's possible that the user doesn't have permission to change
    // the group audience once a piece of content has been created.
    // In this case, we need to look it up.
    $gids = og_get_entity_groups('node', $form_state['node']);
  }
  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 links to this menu. Choose another parent menu.'));
  }
}