You are here

protected function MenuDevelGenerate::generateMenus in Devel 8.3

Same name and namespace in other branches
  1. 8 devel_generate/src/Plugin/DevelGenerate/MenuDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\MenuDevelGenerate::generateMenus()
  2. 8.2 devel_generate/src/Plugin/DevelGenerate/MenuDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\MenuDevelGenerate::generateMenus()
  3. 4.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 315

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()
        ->name(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;
}