function MenuDevelGenerate::generateMenus

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

Generates new menus.

Parameters

int $num_menus: Number of menus to create.

int $title_length: (optional) Maximum length of menu name.

Return value

array Array containing the generated menus.

1 call to MenuDevelGenerate::generateMenus()
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 312

Class

MenuDevelGenerate
Provides a MenuDevelGenerate plugin.

Namespace

Drupal\devel_generate\Plugin\DevelGenerate

Code

protected function generateMenus($num_menus, $title_length = 12) {
    $menus = [];
    for ($i = 1; $i <= $num_menus; $i++) {
        $name = $this->randomSentenceOfLength(mt_rand(2, $title_length));
        // Create a random string of random length for the menu id. The maximum
        // machine-name length is 32, so allowing for prefix 'devel-' we can have
        // up to 26 here. For safety avoid accidentally reusing the same id.
        do {
            $id = 'devel-' . $this->getRandom()
                ->word(mt_rand(2, 26));
        } while (array_key_exists($id, $menus));
        $menu = $this->menuStorage
            ->create([
            'label' => $name,
            'id' => $id,
            'description' => $this->t('Description of @name', [
                '@name' => $name,
            ]),
        ]);
        $menu->save();
        $menus[$menu->id()] = $menu->label();
    }
    return $menus;
}