function MenuDevelGenerate::generateLinks

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

Generates menu links in a tree structure.

Return value

array<int|string, string> Array containing the titles of the generated menu links.

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 299

Class

MenuDevelGenerate
Provides a MenuDevelGenerate plugin.

Namespace

Drupal\devel_generate\Plugin\DevelGenerate

Code

protected function generateLinks(int $num_links, array $menus, int $title_length, array $link_types, int $max_depth, int $max_width) : array {
  $links = [];
  $menus = array_keys(array_filter($menus));
  $link_types = array_keys(array_filter($link_types));
  $nids = [];
  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)));
    /** @var \Drupal\menu_link_content\MenuLinkContentInterface $menuLinkContent */
    $menuLinkContent = $this->menuLinkContentStorage
      ->create([
      'menu_name' => $menu_name,
      'weight' => mt_rand(-50, 50),
      'title' => $link_title,
      'bundle' => 'menu_link_content',
      'description' => $this->t('Description of @title.', [
        '@title' => $link_title,
      ]),
    ]);
    $link = $menuLinkContent->get('link');
    $options['devel'] = TRUE;
    $link->setValue([
      'options' => $options,
    ]);
    // For the first $max_width items, make first level links, otherwise, get
    // a random parent menu depth.
    $max_link_depth = $i <= $max_width ? 0 : mt_rand(1, max(1, $max_depth - 1));
    // Get a random parent link from the proper depth.
    for ($depth = $max_link_depth; $depth >= 0; --$depth) {
      $parameters = new MenuTreeParameters();
      $parameters->setMinDepth($depth);
      $parameters->setMaxDepth($depth);
      $tree = $this->menuLinkTree
        ->load($menu_name, $parameters);
      if ($tree === []) {
        continue;
      }
      $menuLinkContent->set('parent', array_rand($tree));
      break;

    }
    $link_type = array_rand($link_types);
    switch ($link_types[$link_type]) {
      case 'node':
        // Grab a random node ID.
        $select = $this->database
          ->select('node_field_data', 'n')
          ->fields('n', [
          'nid',
          'title',
        ])
          ->condition('n.status', 1)
          ->range(0, 1)
          ->orderRandom();
        // Don't put a node into the menu twice.
        if (isset($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->setValue([
            'uri' => 'entity:node/' . $node['nid'],
          ]);
          $menuLinkContent->set('title', $node['title']);
          break;

        }
      case 'external':
        $link->setValue([
          'uri' => 'https://www.example.com/',
        ]);
        break;

      case 'front':
        $link->setValue([
          'uri' => 'internal:/<front>',
        ]);
        break;

      default:
        break;

    }
    $menuLinkContent->save();
    $links[$menuLinkContent->id()] = $menuLinkContent->getTitle();
  }
  return $links;
}