You are here

public function MicrositeMenuLinkDiscovery::getMenuLinkDefinitions in Entity Reference Hierarchy 3.x

Gets menu link definitions for the given site or all sites if none given.

Parameters

\Drupal\entity_hierarchy_microsite\Entity\MicrositeInterface|null $microsite: Microsite.

Return value

array Menu link plugin definitions.

Overrides MicrositeMenuLinkDiscoveryInterface::getMenuLinkDefinitions

File

modules/entity_hierarchy_microsite/src/MicrositeMenuLinkDiscovery.php, line 101

Class

MicrositeMenuLinkDiscovery
Defines a class for microsite menu link discovery.

Namespace

Drupal\entity_hierarchy_microsite

Code

public function getMenuLinkDefinitions(MicrositeInterface $microsite = NULL) {
  $definitions = [];
  $microsites = $microsite ? [
    $microsite,
  ] : $this->entityTypeManager
    ->getStorage('entity_hierarchy_microsite')
    ->loadMultiple();

  /** @var \Drupal\entity_hierarchy_microsite\Entity\MicrositeInterface $microsite */
  foreach ($microsites as $microsite) {
    $home = $microsite
      ->getHome();
    if (!$home) {
      continue;
    }
    $key = $this->keyFactory
      ->fromEntity($home);
    $parentUuids = [];
    foreach ($this->candidate
      ->getCandidateFields($home) as $field_name) {

      /** @var \PNX\NestedSet\NestedSetInterface $tree */
      $tree = $this->nestedSetStorageFactory
        ->get($field_name, 'node');
      $homeNode = $tree
        ->getNode($key);
      if (!$homeNode) {

        // No children.
        continue;
      }
      $nodes = $this->mapper
        ->loadEntitiesForTreeNodesWithoutAccessChecks('node', $tree
        ->findDescendants($key));
      $url = $home
        ->toUrl();
      $definitions[$home
        ->uuid()] = [
        'class' => MicrositeMenuItem::class,
        'menu_name' => 'entity-hierarchy-microsite',
        'route_name' => $url
          ->getRouteName(),
        'route_parameters' => $url
          ->getRouteParameters(),
        'options' => $url
          ->getOptions(),
        'title' => $home
          ->label(),
        'description' => '',
        'weight' => $homeNode
          ->getLeft(),
        'id' => 'entity_hierarchy_microsite:' . $home
          ->uuid(),
        'metadata' => [
          'entity_id' => $home
            ->id(),
          'entity_hierarchy_depth' => $homeNode
            ->getDepth(),
        ],
        'form_class' => MenuLinkDefaultForm::class,
        'enabled' => 1,
        'expanded' => 1,
        'provider' => 'entity_hierarchy_microsite',
        'discovered' => 0,
      ];

      /** @var \PNX\NestedSet\Node $treeNode */
      foreach ($nodes as $treeNode) {
        if (!$nodes
          ->contains($treeNode)) {
          continue;
        }

        /** @var \Drupal\node\NodeInterface $item */
        $item = $nodes
          ->offsetGet($treeNode);
        $url = $item
          ->toUrl();
        $revisionKey = sprintf('%s:%s', $treeNode
          ->getId(), $treeNode
          ->getRevisionId());
        $itemUuid = $item
          ->uuid();
        $parentUuids[$revisionKey] = $itemUuid;
        $definitions[$itemUuid] = [
          'class' => MicrositeMenuItem::class,
          'menu_name' => 'entity-hierarchy-microsite',
          'route_name' => $url
            ->getRouteName(),
          'route_parameters' => $url
            ->getRouteParameters(),
          'options' => $url
            ->getOptions(),
          'title' => $item
            ->label(),
          'description' => '',
          'weight' => $treeNode
            ->getLeft(),
          'id' => 'entity_hierarchy_microsite:' . $itemUuid,
          'metadata' => [
            'entity_id' => $item
              ->id(),
            'entity_hierarchy_depth' => $treeNode
              ->getDepth(),
          ],
          'form_class' => MenuLinkDefaultForm::class,
          'enabled' => 1,
          'expanded' => 1,
          'provider' => 'entity_hierarchy_microsite',
          'discovered' => 0,
          'parent' => 'entity_hierarchy_microsite:' . $home
            ->uuid(),
        ];
        $parent = $tree
          ->findParent($treeNode
          ->getNodeKey());
        if ($parent && ($parentRevisionKey = sprintf('%s:%s', $parent
          ->getId(), $parent
          ->getRevisionId())) && array_key_exists($parentRevisionKey, $parentUuids)) {
          $definitions[$itemUuid]['parent'] = 'entity_hierarchy_microsite:' . $parentUuids[$parentRevisionKey];
        }
      }
    }

    /** @var \Drupal\entity_hierarchy_microsite\Entity\MicrositeMenuItemOverrideInterface $override */
    if ($definitions) {
      foreach ($this->entityTypeManager
        ->getStorage('eh_microsite_menu_override')
        ->loadByProperties([
        'target' => array_keys($definitions),
      ]) as $override) {
        $original = $definitions[$override
          ->getTarget()];
        $definitions[$override
          ->getTarget()] = [
          'metadata' => [
            'original' => array_intersect_key($original, [
              'title' => TRUE,
              'weight' => TRUE,
              'enabled' => TRUE,
              'expanded' => TRUE,
              'parent' => TRUE,
            ]),
          ] + $original['metadata'],
          'title' => $override
            ->label(),
          'form_class' => MicrositeMenuItemForm::class,
          'weight' => $override
            ->getWeight(),
          'enabled' => $override
            ->isEnabled(),
          'expanded' => $override
            ->isExpanded(),
          'parent' => $override
            ->getParent(),
        ] + $original;
      }
    }
  }
  $this->moduleHandler
    ->alter('entity_hierarchy_microsite_links', $definitions);
  return $definitions;
}