You are here

class RelationRepository in Relation 8

Same name and namespace in other branches
  1. 8.2 src/Entity/RelationRepository.php \Drupal\relation\Entity\RelationRepository

Provides mechanism for retrieving available relations types.

Hierarchy

Expanded class hierarchy of RelationRepository

1 string reference to 'RelationRepository'
relation.services.yml in ./relation.services.yml
relation.services.yml
1 service uses RelationRepository
entity.repository.relation in ./relation.services.yml
Drupal\relation\Entity\RelationRepository

File

src/Entity/RelationRepository.php, line 12

Namespace

Drupal\relation\Entity
View source
class RelationRepository implements RelationRepositoryInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity query.
   *
   * @var \Drupal\Core\Entity\Query\QueryFactory
   */
  protected $entityQuery;

  /**
   * Constructs a new RelationRepository.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\Query\QueryFactory $entity_query
   *   The entity query.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, QueryFactory $entity_query) {
    $this->entityTypeManager = $entity_type_manager;
    $this->entityQuery = $entity_query;
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailable($entity_type, $bundle, $endpoint = 'source') {
    $bundle_key = $entity_type . ':' . $bundle;
    $all_bundle_key = $entity_type . ':*';
    $available_types = array();
    $relation_types = $this->entityTypeManager
      ->getStorage('relation_type')
      ->loadMultiple();
    foreach ($relation_types as $relation_type) {
      $available = FALSE;
      if ($endpoint == 'source' || $endpoint == 'both') {
        if (in_array($bundle_key, $relation_type->source_bundles) || in_array($all_bundle_key, $relation_type->source_bundles)) {
          $available = TRUE;
        }
      }
      if ($endpoint == 'target' || $endpoint == 'both') {
        if (in_array($bundle_key, $relation_type->target_bundles) || in_array($all_bundle_key, $relation_type->target_bundles)) {
          $available = TRUE;
        }
      }
      if ($available) {
        $available_types[] = $relation_type;
      }
    }
    return $available_types;
  }

  /**
   * {@inheritdoc}
   */
  public function relationExists(array $endpoints, $relation_type = NULL, $enforce_direction = FALSE) {
    $query = $this->entityQuery
      ->get('relation');
    foreach ($endpoints as $r_index => $endpoint) {
      relation_query_add_related($query, $endpoint['entity_type'], $endpoint['entity_id'], $enforce_direction ? $r_index : NULL);
    }
    if ($relation_type) {
      $query
        ->condition('relation_type', $relation_type);
    }
    $query
      ->condition('arity', count($endpoints));

    // If direction of the relation is not forced make sure the each endpoint
    // is counted just once.
    if (!$enforce_direction) {
      $query
        ->addTag('enforce_distinct_endpoints');
    }
    return $query
      ->execute();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RelationRepository::$entityQuery protected property The entity query.
RelationRepository::$entityTypeManager protected property The entity type manager.
RelationRepository::getAvailable public function Returns the relation types that can have the given entity as an endpoint. Overrides RelationRepositoryInterface::getAvailable
RelationRepository::relationExists public function Checks if a relation exists. Overrides RelationRepositoryInterface::relationExists
RelationRepository::__construct public function Constructs a new RelationRepository.