You are here

protected function Tracker::addEntityTargets in Menu Entity Index 8

Inserts a database entry for each target entity of the given host entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The host entity for which to add tracking records.

array $targets: An array of target entities.

Return value

int|bool The last insert ID of the query or FALSE if no targets were provided.

1 call to Tracker::addEntityTargets()
Tracker::updateEntity in src/Tracker.php
Updates database tracking for new or updated entities.

File

src/Tracker.php, line 546

Class

Tracker
Tracks menu links and their referenced entities.

Namespace

Drupal\menu_entity_index

Code

protected function addEntityTargets(EntityInterface $entity, array $targets = []) {
  if (empty($targets)) {
    return FALSE;
  }
  $parent_link = NULL;
  $parent_entity = NULL;
  $parent_id = $entity
    ->getParentId();
  if (!empty($parent_id) && $this->menuLinkManager
    ->hasDefinition($parent_id)) {
    $parent_link = $this->menuLinkManager
      ->createInstance($parent_id);
    if ($parent_link
      ->getBaseId() == 'menu_link_content') {
      $parent_entity = $this->entityRepository
        ->loadEntityByUuid($parent_link
        ->getBaseId(), $parent_link
        ->getDerivativeId());
    }
    elseif ($parent_link
      ->getBaseId() == 'views_view') {
      $parent_entity = $parent_link
        ->loadView();
    }
  }
  $menu_link = $this->menuLinkManager
    ->createInstance($entity
    ->getPluginId());
  $host_values = [
    'menu_name' => $entity
      ->getMenuName(),
    'level' => $menu_link ? $this
      ->getMenuLinkLevel($menu_link) : 0,
    'entity_type' => $entity
      ->getEntityTypeId(),
    'entity_subtype' => $entity
      ->bundle(),
    'entity_id' => $entity
      ->id(),
    'entity_uuid' => $entity
      ->uuid(),
    'parent_type' => $parent_link ? $parent_link
      ->getBaseId() : '',
    'parent_id' => $parent_entity ? $parent_entity
      ->id() : NULL,
    'parent_uuid' => $parent_link ? (string) $parent_link
      ->getDerivativeId() : '',
    'langcode' => $entity
      ->getEntityType()
      ->hasKey('langcode') ? $entity
      ->language()
      ->getId() : '',
  ];
  $query = $this->database
    ->insert('menu_entity_index')
    ->fields([
    'menu_name',
    'level',
    'entity_type',
    'entity_subtype',
    'entity_id',
    'entity_uuid',
    'parent_type',
    'parent_id',
    'parent_uuid',
    'langcode',
    'target_type',
    'target_subtype',
    'target_id',
    'target_uuid',
    'target_langcode',
  ]);
  foreach ($targets as $target_entity) {
    $values = [
      'target_type' => $target_entity
        ->getEntityTypeId(),
      'target_subtype' => $target_entity
        ->bundle(),
      'target_id' => $target_entity
        ->id(),
      'target_uuid' => $target_entity
        ->uuid(),
      'target_langcode' => $target_entity
        ->getEntityType()
        ->hasKey('langcode') ? $target_entity
        ->language()
        ->getId() : '',
    ];
    $query
      ->values($values + $host_values);
  }
  return $query
    ->execute();
}