You are here

protected function EntityReferenceHierarchy::loadSiblingEntityWeights in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldType/EntityReferenceHierarchy.php \Drupal\entity_hierarchy\Plugin\Field\FieldType\EntityReferenceHierarchy::loadSiblingEntityWeights()

Loads other children of the given parent.

Parameters

\PNX\NestedSet\Node[] $siblings: Target siblings.

Return value

\SplObjectStorage Map of weights keyed by node.

1 call to EntityReferenceHierarchy::loadSiblingEntityWeights()
EntityReferenceHierarchy::getSiblingEntityWeights in src/Plugin/Field/FieldType/EntityReferenceHierarchy.php
Gets siblings.

File

src/Plugin/Field/FieldType/EntityReferenceHierarchy.php, line 233

Class

EntityReferenceHierarchy
Plugin implementation of the 'entity_reference_hierarchy' field type.

Namespace

Drupal\entity_hierarchy\Plugin\Field\FieldType

Code

protected function loadSiblingEntityWeights(array $siblings) {
  $fieldDefinition = $this
    ->getFieldDefinition();
  $entityType = $this
    ->entityTypeDefinition();
  $entityTypeId = $fieldDefinition
    ->getTargetEntityTypeId();
  $entityStorage = $this
    ->entityTypeStorage($entityTypeId);
  $siblingEntities = new \SplObjectStorage();
  $key = $entityType
    ->hasKey('revision') ? $entityType
    ->getKey('revision') : $entityType
    ->getKey('id');
  $parentField = $fieldDefinition
    ->getName();
  $query = $entityStorage
    ->getAggregateQuery();
  $ids = array_map(function (Node $item) {
    return $item
      ->getRevisionId();
  }, $siblings);
  $entities = $query
    ->groupBy($key)
    ->sort($key, 'ASC')
    ->groupBy($parentField . '.weight')
    ->condition($key, $ids, 'IN')
    ->execute();
  $weightSeparator = $fieldDefinition instanceof BaseFieldDefinition ? '__' : '_';
  $entities = array_combine(array_column($entities, $key), array_column($entities, $parentField . $weightSeparator . 'weight'));
  foreach ($siblings as $node) {
    if (!isset($entities[$node
      ->getRevisionId()])) {
      continue;
    }
    $siblingEntities[$node] = (int) $entities[$node
      ->getRevisionId()];
  }
  return $siblingEntities;
}