function devel_generate_links

Generates menu links in a tree structure.

2 calls to devel_generate_links()
devel_generate_menu_form_submit in devel_generate/devel_generate.module
FormAPI submission to generate menus.
drush_devel_generate_menus in devel_generate/devel_generate.drush.inc
Command callback. Generate a number of menus and menu links.

File

devel_generate/devel_generate.inc, line 381

Code

function devel_generate_links($num_links, $menus, $title_length, $link_types, $max_depth, $max_width) {
    $links = array();
    $menus = array_keys(array_filter($menus));
    $link_types = array_keys(array_filter($link_types));
    $nids = array();
    for ($i = 1; $i <= $num_links; $i++) {
        // Pick a random menu.
        $menu_name = $menus[array_rand($menus)];
        // Build up our link.
        $link = array(
            'menu_name' => $menu_name,
            'options' => array(
                'devel' => TRUE,
            ),
            'weight' => mt_rand(-50, 50),
            'mlid' => 0,
            'link_title' => devel_generate_word(mt_rand(2, max(2, $title_length))),
        );
        $link['options']['attributes']['title'] = t('Description of @title.', array(
            '@title' => $link['link_title'],
        ));
        // For the first $max_width items, make first level links.
        if ($i <= $max_width) {
            $depth = 0;
        }
        else {
            // Otherwise, get a random parent menu depth.
            $depth = mt_rand(1, max(1, $max_depth - 1));
        }
        // Get a random parent link from the proper depth.
        do {
            $link['plid'] = db_select('menu_links', 'm')->fields('m', array(
                'mlid',
            ))
                ->condition('m.menu_name', $menus, 'IN')
                ->condition('m.depth', $depth)
                ->range(0, 1)
                ->orderRandom()
                ->execute()
                ->fetchField();
            $depth--;
        } while (!$link['plid'] && $depth > 0);
        if (!$link['plid']) {
            $link['plid'] = 0;
        }
        $link_type = array_rand($link_types);
        switch ($link_types[$link_type]) {
            case 'node':
                // Grab a random node ID.
                $select = db_select('node', 'n')->fields('n', array(
                    'nid',
                    'title',
                ))
                    ->condition('n.status', 1)
                    ->range(0, 1)
                    ->orderRandom();
                // Don't put a node into the menu twice.
                if (!empty($nids[$menu_name])) {
                    $select->condition('n.nid', $nids[$menu_name], 'NOT IN');
                }
                $node = $select->execute()
                    ->fetchAssoc();
                if (isset($node['nid'])) {
                    $nids[$menu_name][] = $node['nid'];
                    $link['link_path'] = $link['router_path'] = 'node/' . $node['nid'];
                    $link['link_title'] = $node['title'];
                    break;
                }
            case 'external':
                $link['link_path'] = 'http://www.example.com/';
                break;
            case 'front':
                $link['link_path'] = $link['router_path'] = '<front>';
                break;
            default:
                $link['devel_link_type'] = $link_type;
                break;
        }
        menu_link_save($link);
        $links[$link['mlid']] = $link['link_title'];
    }
    return $links;
}