You are here

public function RelationRepository::getAvailable in Relation 8.2

Same name and namespace in other branches
  1. 8 src/Entity/RelationRepository.php \Drupal\relation\Entity\RelationRepository::getAvailable()

Returns the relation types that can have the given entity as an endpoint.

Parameters

string $entity_type: The entity type of the endpoint.

string $bundle: The bundle of the endpoint.

string $endpoint: (optional) the type of endpoint. This is only used for directional relation types. Possible options are 'source', 'target', or 'both'.

Return value

array An array of relation types

Overrides RelationRepositoryInterface::getAvailable

File

src/Entity/RelationRepository.php, line 44

Class

RelationRepository
Provides mechanism for retrieving available relations types.

Namespace

Drupal\relation\Entity

Code

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;
}