Chris@0: cache = $cache; Chris@0: $this->entityManager = $entity_manager; Chris@0: $this->configFactory = $config_factory; Chris@0: $this->moduleHandler = $module_handler; Chris@0: $this->requestStack = $request_stack; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getRelationUri($entity_type, $bundle, $field_name, $context = []) { Chris@0: // Per the interface documentation of this method, the returned URI may Chris@0: // optionally also serve as the URL of a documentation page about this Chris@0: // field. However, Drupal does not currently implement such a documentation Chris@0: // page. Therefore, we return a URI assembled relative to the site's base Chris@0: // URL, which is sufficient to uniquely identify the site's entity type + Chris@0: // bundle + field for use in hypermedia formats, but we do not take into Chris@0: // account unclean URLs, language prefixing, or anything else that would be Chris@0: // required for Drupal to be able to respond with content at this URL. If a Chris@0: // module is installed that adds such content, but requires this URL to be Chris@0: // different (e.g., include a language prefix), then the module must also Chris@0: // override the RelationLinkManager class/service to return the desired URL. Chris@0: $uri = $this->getLinkDomain() . "/rest/relation/$entity_type/$bundle/$field_name"; Chris@0: $this->moduleHandler->alter('hal_relation_uri', $uri, $context); Chris@0: // @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. This Chris@0: // hook is invoked to maintain backwards compatibility Chris@0: // @see https://www.drupal.org/node/2830467 Chris@0: $this->moduleHandler->alter('rest_relation_uri', $uri, $context); Chris@0: return $uri; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getRelationInternalIds($relation_uri, $context = []) { Chris@0: $relations = $this->getRelations($context); Chris@0: if (isset($relations[$relation_uri])) { Chris@0: return $relations[$relation_uri]; Chris@0: } Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the array of relation links. Chris@0: * Chris@0: * Any field can be handled as a relation simply by changing how it is Chris@0: * normalized. Therefore, there is no prior knowledge that can be used here Chris@0: * to determine which fields to assign relation URIs. Instead, each field, Chris@0: * even primitives, are given a relation URI. It is up to the caller to Chris@0: * determine which URIs to use. Chris@0: * Chris@0: * @param array $context Chris@0: * Context from the normalizer/serializer operation. Chris@0: * Chris@0: * @return array Chris@0: * An array of typed data IDs keyed by corresponding relation URI. The keys Chris@0: * are: Chris@0: * - 'entity_type_id' Chris@0: * - 'bundle' Chris@0: * - 'field_name' Chris@0: * - 'entity_type' (deprecated) Chris@0: * The values for 'entity_type_id', 'bundle' and 'field_name' are strings. Chris@0: * The 'entity_type' key exists for backwards compatibility and its value is Chris@0: * the full entity type object. The 'entity_type' key will be removed before Chris@0: * Drupal 9.0. Chris@0: * Chris@0: * @see https://www.drupal.org/node/2877608 Chris@0: */ Chris@0: protected function getRelations($context = []) { Chris@0: $cid = 'hal:links:relations'; Chris@0: $cache = $this->cache->get($cid); Chris@0: if (!$cache) { Chris@0: $data = $this->writeCache($context); Chris@0: } Chris@0: else { Chris@0: $data = $cache->data; Chris@0: } Chris@0: Chris@0: // @todo https://www.drupal.org/node/2716163 Remove this in Drupal 9.0. Chris@0: foreach ($data as $relation_uri => $ids) { Chris@0: $data[$relation_uri]['entity_type'] = $this->entityManager->getDefinition($ids['entity_type_id']); Chris@0: } Chris@0: return $data; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Writes the cache of relation links. Chris@0: * Chris@0: * @param array $context Chris@0: * Context from the normalizer/serializer operation. Chris@0: * Chris@0: * @return array Chris@0: * An array of typed data IDs keyed by corresponding relation URI. The keys Chris@0: * are: Chris@0: * - 'entity_type_id' Chris@0: * - 'bundle' Chris@0: * - 'field_name' Chris@0: * The values for 'entity_type_id', 'bundle' and 'field_name' are strings. Chris@0: */ Chris@0: protected function writeCache($context = []) { Chris@0: $data = []; Chris@0: Chris@0: foreach ($this->entityManager->getDefinitions() as $entity_type) { Chris@0: if ($entity_type instanceof ContentEntityTypeInterface) { Chris@0: foreach ($this->entityManager->getBundleInfo($entity_type->id()) as $bundle => $bundle_info) { Chris@0: foreach ($this->entityManager->getFieldDefinitions($entity_type->id(), $bundle) as $field_definition) { Chris@0: $relation_uri = $this->getRelationUri($entity_type->id(), $bundle, $field_definition->getName(), $context); Chris@0: $data[$relation_uri] = [ Chris@0: 'entity_type_id' => $entity_type->id(), Chris@0: 'bundle' => $bundle, Chris@0: 'field_name' => $field_definition->getName(), Chris@0: ]; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: // These URIs only change when field info changes, so cache it permanently Chris@0: // and only clear it when the fields cache is cleared. Chris@0: $this->cache->set('hal:links:relations', $data, Cache::PERMANENT, ['entity_field_info']); Chris@0: return $data; Chris@0: } Chris@0: Chris@0: }