class RelationLinkManager

Hierarchy

Expanded class hierarchy of RelationLinkManager

1 file declares its use of RelationLinkManager
NormalizerTestBase.php in drupal/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php
Contains \Drupal\hal\Tests\NormalizerTestBase.
1 string reference to 'RelationLinkManager'
rest.services.yml in drupal/core/modules/rest/rest.services.yml
drupal/core/modules/rest/rest.services.yml
1 service uses RelationLinkManager

File

drupal/core/modules/rest/lib/Drupal/rest/LinkManager/RelationLinkManager.php, line 12
Contains \Drupal\rest\LinkManager\RelationLinkManager.

Namespace

Drupal\rest\LinkManager
View source
class RelationLinkManager implements RelationLinkManagerInterface {

  /**
   * @var \Drupal\Core\Cache\CacheBackendInterface;
   */
  protected $cache;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache
   *   The cache of relation URIs and their associated Typed Data IDs.
   */
  public function __construct(CacheBackendInterface $cache) {
    $this->cache = $cache;
  }

  /**
   * Implements \Drupal\rest\LinkManager\RelationLinkManagerInterface::getRelationUri().
   */
  public function getRelationUri($entity_type, $bundle, $field_name) {

    // @todo Make the base path configurable.
    return url("rest/relation/{$entity_type}/{$bundle}/{$field_name}", array(
      'absolute' => TRUE,
    ));
  }

  /**
   * Implements \Drupal\rest\LinkManager\RelationLinkManagerInterface::getRelationInternalIds().
   */
  public function getRelationInternalIds($relation_uri) {
    $relations = $this
      ->getRelations();
    if (isset($relations[$relation_uri])) {
      return $relations[$relation_uri];
    }
    return FALSE;
  }

  /**
   * Get the array of relation links.
   *
   * Any field can be handled as a relation simply by changing how it is
   * normalized. Therefore, there is no prior knowledge that can be used here
   * to determine which fields to assign relation URIs. Instead, each field,
   * even primitives, are given a relation URI. It is up to the caller to
   * determine which URIs to use.
   *
   * @return array
   *   An array of typed data ids (entity_type, bundle, and field name) keyed
   *   by corresponding relation URI.
   */
  public function getRelations() {
    $cid = 'rest:links:relations';
    $cache = $this->cache
      ->get($cid);
    if (!$cache) {
      $this
        ->writeCache();
      $cache = $this->cache
        ->get($cid);
    }
    return $cache->data;
  }

  /**
   * Writes the cache of relation links.
   */
  protected function writeCache() {
    $data = array();
    foreach (field_info_fields() as $field_info) {
      foreach ($field_info['bundles'] as $entity_type => $bundles) {
        foreach ($bundles as $bundle) {
          $relation_uri = $this
            ->getRelationUri($entity_type, $bundle, $field_info['field_name']);
          $data[$relation_uri] = array(
            'entity_type' => $entity_type,
            'bundle' => $bundle,
            'field_name' => $field_info['field_name'],
          );
        }
      }
    }

    // These URIs only change when field info changes, so cache it permanently
    // and only clear it when field_info is cleared.
    $this->cache
      ->set('rest:links:relations', $data, CacheBackendInterface::CACHE_PERMANENT, array(
      'field_info' => TRUE,
    ));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RelationLinkManager::$cache protected property
RelationLinkManager::getRelationInternalIds public function Implements \Drupal\rest\LinkManager\RelationLinkManagerInterface::getRelationInternalIds().
RelationLinkManager::getRelations public function Get the array of relation links.
RelationLinkManager::getRelationUri public function Implements \Drupal\rest\LinkManager\RelationLinkManagerInterface::getRelationUri(). Overrides RelationLinkManagerInterface::getRelationUri
RelationLinkManager::writeCache protected function Writes the cache of relation links.
RelationLinkManager::__construct public function Constructor.