You are here

public function Tracker::getHostData in Menu Entity Index 8

Gets host information for a target entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The target entity to get the host information for.

Return value

array Host information for target entity. For each menu link that references that entity, an array value will be returned. That value is an array with the following keys:

  • menu_name: Menu name of menu link referencing the entity.
  • level: Menu level of menu link referencing the entity.
  • label: Label of menu link referencing the entity.
  • link: URL object for edit page of menu link referencing the entity. If the current user does not have access to view the menu link, the key contains an empty string instead.
  • language: Name of language of menu link referencing the entity.

Overrides TrackerInterface::getHostData

File

src/Tracker.php, line 637

Class

Tracker
Tracks menu links and their referenced entities.

Namespace

Drupal\menu_entity_index

Code

public function getHostData(EntityInterface $entity) {
  $data = [];
  $type = $entity
    ->getEntityTypeId();
  if (in_array($entity
    ->getEntityTypeId(), $this
    ->getTrackedEntityTypes())) {
    $id = $entity
      ->id();
    $result = $this->database
      ->select('menu_entity_index')
      ->fields('menu_entity_index', [
      'entity_type',
      'entity_id',
      'menu_name',
      'level',
      'langcode',
    ])
      ->condition('target_type', $type)
      ->condition('target_id', $id)
      ->orderBy('menu_name', 'ASC')
      ->orderBy('level', 'ASC')
      ->execute();
    $menus = [];
    foreach ($result as $row) {
      if (!isset($menus[$row->menu_name])) {
        $entity = $this->entityTypeManager
          ->getStorage('menu')
          ->load($row->menu_name);
        $menus[$row->menu_name] = $entity
          ->label();
      }
      $entity = $this->entityTypeManager
        ->getStorage($row->entity_type)
        ->load($row->entity_id);
      if ($entity) {
        $data[] = [
          'menu_name' => $menus[$row->menu_name],
          'level' => $row->level,
          'label' => $entity
            ->getTitle(),
          'link' => $entity
            ->access('view') ? $entity
            ->toUrl() : '',
          'language' => $entity
            ->language()
            ->getName(),
        ];
      }
    }
  }
  return $data;
}