function MenuDevelGenerate::generateLinks

Same name in other branches
  1. 4.x devel_generate/src/Plugin/DevelGenerate/MenuDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\MenuDevelGenerate::generateLinks()
  2. 5.x devel_generate/src/Plugin/DevelGenerate/MenuDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\MenuDevelGenerate::generateLinks()

Generates menu links in a tree structure.

1 call to MenuDevelGenerate::generateLinks()
MenuDevelGenerate::generateElements in devel_generate/src/Plugin/DevelGenerate/MenuDevelGenerate.php
Business logic relating with each DevelGenerate plugin

File

devel_generate/src/Plugin/DevelGenerate/MenuDevelGenerate.php, line 322

Class

MenuDevelGenerate
Provides a MenuDevelGenerate plugin.

Namespace

Drupal\devel_generate\Plugin\DevelGenerate

Code

protected function generateLinks($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_title = $this->getRandom()
            ->word(mt_rand(2, max(2, $title_length)));
        $link = $this->menuLinkContentStorage
            ->create(array(
            'menu_name' => $menu_name,
            'weight' => mt_rand(-50, 50),
            'title' => $link_title,
            'bundle' => 'menu_link_content',
            'description' => $this->t('Description of @title.', array(
                '@title' => $link_title,
            )),
        ));
        $link->link->options = array(
            'devel' => TRUE,
        );
        // 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 {
            $parameters = new MenuTreeParameters();
            $parameters->setMinDepth($depth);
            $parameters->setMaxDepth($depth);
            $tree = $this->menuLinkTree
                ->load($menu_name, $parameters);
            if ($tree) {
                $link->parent = array_rand($tree);
            }
            $depth--;
        } while (!$link->parent && $depth > 0);
        $link_type = array_rand($link_types);
        switch ($link_types[$link_type]) {
            case 'node':
                // Grab a random node ID.
                $select = db_select('node_field_data', '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->uri = 'entity:node/' . $node['nid'];
                    $link->title = $node['title'];
                    break;
                }
            case 'external':
                $link->link->uri = 'http://www.example.com/';
                break;
            case 'front':
                $link->link->uri = 'internal:/<front>';
                break;
            default:
                $link->devel_link_type = $link_type;
                break;
        }
        $link->save();
        $links[$link->id()] = $link->link_title;
    }
    return $links;
}