annotate core/modules/hal/src/LinkManager/RelationLinkManager.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\hal\LinkManager;
Chris@0 4
Chris@0 5 use Drupal\Core\Cache\Cache;
Chris@0 6 use Drupal\Core\Cache\CacheBackendInterface;
Chris@0 7 use Drupal\Core\Config\ConfigFactoryInterface;
Chris@0 8 use Drupal\Core\Entity\ContentEntityTypeInterface;
Chris@0 9 use Drupal\Core\Entity\EntityManagerInterface;
Chris@0 10 use Drupal\Core\Extension\ModuleHandlerInterface;
Chris@0 11 use Symfony\Component\HttpFoundation\RequestStack;
Chris@0 12
Chris@0 13 class RelationLinkManager extends LinkManagerBase implements RelationLinkManagerInterface {
Chris@0 14
Chris@0 15 /**
Chris@0 16 * @var \Drupal\Core\Cache\CacheBackendInterface;
Chris@0 17 */
Chris@0 18 protected $cache;
Chris@0 19
Chris@0 20 /**
Chris@0 21 * Entity manager.
Chris@0 22 *
Chris@0 23 * @var \Drupal\Core\Entity\EntityManagerInterface
Chris@0 24 */
Chris@0 25 protected $entityManager;
Chris@0 26
Chris@0 27 /**
Chris@0 28 * Module handler service.
Chris@0 29 *
Chris@0 30 * @var \Drupal\Core\Extension\ModuleHandlerInterface
Chris@0 31 */
Chris@0 32 protected $moduleHandler;
Chris@0 33
Chris@0 34 /**
Chris@0 35 * Constructor.
Chris@0 36 *
Chris@0 37 * @param \Drupal\Core\Cache\CacheBackendInterface $cache
Chris@0 38 * The cache of relation URIs and their associated Typed Data IDs.
Chris@0 39 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
Chris@0 40 * The entity manager.
Chris@0 41 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
Chris@0 42 * The module handler service.
Chris@0 43 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Chris@0 44 * The config factory service.
Chris@0 45 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
Chris@0 46 * The request stack.
Chris@0 47 */
Chris@0 48 public function __construct(CacheBackendInterface $cache, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory, RequestStack $request_stack) {
Chris@0 49 $this->cache = $cache;
Chris@0 50 $this->entityManager = $entity_manager;
Chris@0 51 $this->configFactory = $config_factory;
Chris@0 52 $this->moduleHandler = $module_handler;
Chris@0 53 $this->requestStack = $request_stack;
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * {@inheritdoc}
Chris@0 58 */
Chris@0 59 public function getRelationUri($entity_type, $bundle, $field_name, $context = []) {
Chris@0 60 // Per the interface documentation of this method, the returned URI may
Chris@0 61 // optionally also serve as the URL of a documentation page about this
Chris@0 62 // field. However, Drupal does not currently implement such a documentation
Chris@0 63 // page. Therefore, we return a URI assembled relative to the site's base
Chris@0 64 // URL, which is sufficient to uniquely identify the site's entity type +
Chris@0 65 // bundle + field for use in hypermedia formats, but we do not take into
Chris@0 66 // account unclean URLs, language prefixing, or anything else that would be
Chris@0 67 // required for Drupal to be able to respond with content at this URL. If a
Chris@0 68 // module is installed that adds such content, but requires this URL to be
Chris@0 69 // different (e.g., include a language prefix), then the module must also
Chris@0 70 // override the RelationLinkManager class/service to return the desired URL.
Chris@0 71 $uri = $this->getLinkDomain($context) . "/rest/relation/$entity_type/$bundle/$field_name";
Chris@0 72 $this->moduleHandler->alter('hal_relation_uri', $uri, $context);
Chris@0 73 // @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. This
Chris@0 74 // hook is invoked to maintain backwards compatibility
Chris@0 75 // @see https://www.drupal.org/node/2830467
Chris@0 76 $this->moduleHandler->alter('rest_relation_uri', $uri, $context);
Chris@0 77 return $uri;
Chris@0 78 }
Chris@0 79
Chris@0 80 /**
Chris@0 81 * {@inheritdoc}
Chris@0 82 */
Chris@0 83 public function getRelationInternalIds($relation_uri, $context = []) {
Chris@0 84 $relations = $this->getRelations($context);
Chris@0 85 if (isset($relations[$relation_uri])) {
Chris@0 86 return $relations[$relation_uri];
Chris@0 87 }
Chris@0 88 return FALSE;
Chris@0 89 }
Chris@0 90
Chris@0 91 /**
Chris@0 92 * Get the array of relation links.
Chris@0 93 *
Chris@0 94 * Any field can be handled as a relation simply by changing how it is
Chris@0 95 * normalized. Therefore, there is no prior knowledge that can be used here
Chris@0 96 * to determine which fields to assign relation URIs. Instead, each field,
Chris@0 97 * even primitives, are given a relation URI. It is up to the caller to
Chris@0 98 * determine which URIs to use.
Chris@0 99 *
Chris@0 100 * @param array $context
Chris@0 101 * Context from the normalizer/serializer operation.
Chris@0 102 *
Chris@0 103 * @return array
Chris@0 104 * An array of typed data IDs keyed by corresponding relation URI. The keys
Chris@0 105 * are:
Chris@0 106 * - 'entity_type_id'
Chris@0 107 * - 'bundle'
Chris@0 108 * - 'field_name'
Chris@0 109 * - 'entity_type' (deprecated)
Chris@0 110 * The values for 'entity_type_id', 'bundle' and 'field_name' are strings.
Chris@0 111 * The 'entity_type' key exists for backwards compatibility and its value is
Chris@0 112 * the full entity type object. The 'entity_type' key will be removed before
Chris@0 113 * Drupal 9.0.
Chris@0 114 *
Chris@0 115 * @see https://www.drupal.org/node/2877608
Chris@0 116 */
Chris@0 117 protected function getRelations($context = []) {
Chris@0 118 $cid = 'hal:links:relations';
Chris@0 119 $cache = $this->cache->get($cid);
Chris@0 120 if (!$cache) {
Chris@0 121 $data = $this->writeCache($context);
Chris@0 122 }
Chris@0 123 else {
Chris@0 124 $data = $cache->data;
Chris@0 125 }
Chris@0 126
Chris@0 127 // @todo https://www.drupal.org/node/2716163 Remove this in Drupal 9.0.
Chris@0 128 foreach ($data as $relation_uri => $ids) {
Chris@0 129 $data[$relation_uri]['entity_type'] = $this->entityManager->getDefinition($ids['entity_type_id']);
Chris@0 130 }
Chris@0 131 return $data;
Chris@0 132 }
Chris@0 133
Chris@0 134 /**
Chris@0 135 * Writes the cache of relation links.
Chris@0 136 *
Chris@0 137 * @param array $context
Chris@0 138 * Context from the normalizer/serializer operation.
Chris@0 139 *
Chris@0 140 * @return array
Chris@0 141 * An array of typed data IDs keyed by corresponding relation URI. The keys
Chris@0 142 * are:
Chris@0 143 * - 'entity_type_id'
Chris@0 144 * - 'bundle'
Chris@0 145 * - 'field_name'
Chris@0 146 * The values for 'entity_type_id', 'bundle' and 'field_name' are strings.
Chris@0 147 */
Chris@0 148 protected function writeCache($context = []) {
Chris@0 149 $data = [];
Chris@0 150
Chris@0 151 foreach ($this->entityManager->getDefinitions() as $entity_type) {
Chris@0 152 if ($entity_type instanceof ContentEntityTypeInterface) {
Chris@0 153 foreach ($this->entityManager->getBundleInfo($entity_type->id()) as $bundle => $bundle_info) {
Chris@0 154 foreach ($this->entityManager->getFieldDefinitions($entity_type->id(), $bundle) as $field_definition) {
Chris@0 155 $relation_uri = $this->getRelationUri($entity_type->id(), $bundle, $field_definition->getName(), $context);
Chris@0 156 $data[$relation_uri] = [
Chris@0 157 'entity_type_id' => $entity_type->id(),
Chris@0 158 'bundle' => $bundle,
Chris@0 159 'field_name' => $field_definition->getName(),
Chris@0 160 ];
Chris@0 161 }
Chris@0 162 }
Chris@0 163 }
Chris@0 164 }
Chris@0 165 // These URIs only change when field info changes, so cache it permanently
Chris@0 166 // and only clear it when the fields cache is cleared.
Chris@0 167 $this->cache->set('hal:links:relations', $data, Cache::PERMANENT, ['entity_field_info']);
Chris@0 168 return $data;
Chris@0 169 }
Chris@0 170
Chris@0 171 }