You are here

function groupmenu_block_view in Group Menu 7

Implements hook_block_view().

File

./groupmenu.module, line 1028
Integrates menu with Group.

Code

function groupmenu_block_view($delta = '') {
  $block = array();

  // Blocks can only be used when using the groupcontext module.
  // @see https://www.drupal.org/sandbox/mike.davis/2568111.
  if (!module_exists('groupcontext')) {
    return array();
  }
  $gid = groupcontext();
  if (empty($gid)) {
    return array();
  }
  $group = group_load($gid);
  if ($delta == 'groupmenu_single_block') {
    $menus = groupmenu_get_group_menus(array(
      $group->gid,
    ));
    $menu = array_shift($menus);
    if ($menu) {
      if (variable_get('groupmenu_block_links', FALSE)) {
        $block['subject'] = l($menu['title'], 'group/' . $menu['gid']);
      }
      else {
        $block['subject'] = check_plain($menu['title']);
      }
      $block['content'] = menu_tree($menu['menu_name']);

      // Add contextual links.
      if (!empty($block['content']) && in_array($menu['menu_name'], array_keys(menu_get_menus()))) {
        $block['content']['#contextual_links']['group_menu'] = array(
          'admin/structure/menu/manage',
          array(
            $menu['menu_name'],
          ),
        );
      }
    }
  }
  elseif ($delta == 'groupmenu_multi_block') {
    $menus = groupmenu_get_group_menus(array(
      $group->gid,
    ));
    $plural = count($menus) > 1 ? TRUE : FALSE;
    $block['content'] = '';
    foreach ($menus as $menu) {
      if (!isset($block['content']['#contextual_links'])) {

        // Add contextual links for first menu.
        // @TODO Find a proper solution for displaying contextual links for all menus.
        if (in_array($menu['menu_name'], array_keys(menu_get_menus()))) {
          $block['content']['#contextual_links']['group_menu'] = array(
            'admin/structure/menu/manage',
            array(
              $menu['menu_name'],
            ),
          );
        }
      }
      $title = check_plain($menu['title']);
      if (variable_get('groupmenu_block_links', FALSE)) {
        $title = l($menu['title'], 'group/' . $menu['gid']);
      }
      if ($tree = menu_tree($menu['menu_name'])) {
        if ($plural) {
          $block['subject'] = '';
          if (!isset($block['content']['#markup'])) {
            $block['content']["#markup"] = '';
          }
          $block['content']['#markup'] .= '<div class="block-group-menu-inset">' . '<h2 class="block-title">' . $title . '</h2>' . render($tree) . '</div>';
        }
        else {
          $block['subject'] = $title;
          $tree = menu_tree($menu['menu_name']);
          $block['content'] = $tree;
        }
      }
    }
  }
  return empty($block['content']) ? array() : $block;
}