function NavigationContentLinks::addCreateEntityLinks

Same name in other branches
  1. 11.x core/modules/navigation/src/NavigationContentLinks.php \Drupal\navigation\NavigationContentLinks::addCreateEntityLinks()

Add create links for an entity type.

This function preserves the order of entity types as it is called.

Parameters

string $entity_type: The entity type to add links for, such as node_type.

string $add_route_id: The ID of the route for the entity type add form.

array $links: The existing array of links to add to.

array $bundle_allow_list: A list of allowed bundles to include. Can be used to limit the list of bundles that are included for noisy entity types like media.

1 call to NavigationContentLinks::addCreateEntityLinks()
NavigationContentLinks::addMenuLinks in core/modules/navigation/src/NavigationContentLinks.php
Add links to the Content menu, based on enabled modules.

File

core/modules/navigation/src/NavigationContentLinks.php, line 142

Class

NavigationContentLinks
Build the menu links for the Content menu.

Namespace

Drupal\navigation

Code

private function addCreateEntityLinks(string $entity_type, string $add_route_id, array &$links, array $bundle_allow_list = []) : void {
    // Ensure subsequent calls always get added to the bottom, and not in
    // alphabetical order.
    static $weight = 0;
    // The module providing the entity type is either not installed, or in the
    // process of being uninstalled.
    if (!$this->entityTypeManager
        ->hasDefinition($entity_type)) {
        return;
    }
    // Sort all types within an entity type alphabetically.
    $definition = $this->entityTypeManager
        ->getDefinition($entity_type);
    $types = $this->entityTypeManager
        ->getStorage($entity_type)
        ->loadMultiple();
    if (method_exists($definition->getClass(), 'sort')) {
        uasort($types, [
            $definition->getClass(),
            'sort',
        ]);
    }
    $add_content_links = [];
    foreach ($types as $type) {
        // Skip if the bundle is not in the allow list.
        if (!empty($bundle_allow_list) && !in_array($type->id(), $bundle_allow_list)) {
            continue;
        }
        $add_content_links['navigation.content.' . $type->getEntityTypeId() . '.' . $type->id()] = [
            'title' => $type->label(),
            'route_name' => $add_route_id,
            'route_parameters' => [
                $entity_type => $type->id(),
            ],
            'parent' => 'navigation.create',
            'weight' => $weight,
        ];
    }
    foreach ($add_content_links as $link_name => $link) {
        $this->addLink($link_name, $link, $links);
    }
    $weight++;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.