0

If given the node id, how may I get that node's menu's render array?

I'm almost looking for an API function such as

function get_menu_for_node($nid) {}

Where this would return the render array to which node/$nid belongs.

1 Answer 1

0

I did the following to achieve this. I'm still not happy with the results but given time pressures, it'll have to do. I'm posting to see if anyone can suggests a better alternative.

I grab the nid of the parent group first and use it to do a query against menu_links. Once I have a result of a menu_id(s), we loop through, get the links, use theme(), and then drupal_add_region_content().

<?php
/**
 *
 *
 * This module creates a block that looks for any parent OG group and displays the menu of that
 * group. 
 */

define('_OGPN_NODETYPE', 'user_posted_content');
define('_OGPARENTNAME_TITLE', 'OG Parent Menu');


/**
 * Implements hook_block_info().
 */
function ogparentmenu_block_info() {
    // This example comes from node.module.
    $blocks['_ogparentmenu'] = array(
        'info' => t(_OGPARENTNAME_TITLE),
        'cache' => DRUPAL_NO_CACHE,
    );


    return $blocks;
}

/**
 * Implements hook_block_view().
 */
function ogparentmenu_block_view($delta = '') {
        return [
            'subject' => t(_OGPARENTNAME_TITLE),
            'content' => __ogparentmenu(),
        ] ;
}


/**
 * @return string
 * @throws Exception
 */
function __ogparentmenu() {

    $node = menu_get_object();

    /* Set up menu on UserPosts  */
    if ($node->type == _OGPN_NODETYPE) {

        // get parent link of og
        $parent_og = filter_var($node->og_group_ref[LANGUAGE_NONE][0]['target_id'], FILTER_SANITIZE_NUMBER_INT);

        // Query menu_links based on node/$nid path
        $menu_query = db_select('menu_links', 'ml')
            ->fields('ml', ['menu_name', 'mlid'])
            ->condition('link_path', "node/$parent_og", '=')
            ->execute();

        // if single
        if ($menu_query->rowCount() == 1) {
            $menu_id = check_plain($menu_query->fetchAs()['menu_name']);
            $links = menu_navigation_links($menu_id);

            $themed_menu = theme('links__system_main_menu', [
                '#title' => t('title'),
                'links' => $links,
                'attributes' => [
                    'class' => ['user_post_menu_injected', 'section']
                ],

            ]);

            return $themed_menu;





        }
        // TO DO : handle multiple
        elseif ($menu_query->rowCount() > 1) {

            return 'null';
        }



    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.