annotate core/lib/Drupal/Core/ParamConverter/EntityConverter.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children af1871eacc83
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\ParamConverter;
Chris@0 4
Chris@0 5 use Drupal\Core\Entity\EntityInterface;
Chris@0 6 use Drupal\Core\Entity\EntityManagerInterface;
Chris@14 7 use Drupal\Core\Entity\RevisionableInterface;
Chris@14 8 use Drupal\Core\Entity\TranslatableRevisionableInterface;
Chris@14 9 use Drupal\Core\Language\LanguageInterface;
Chris@14 10 use Drupal\Core\Language\LanguageManagerInterface;
Chris@0 11 use Drupal\Core\TypedData\TranslatableInterface;
Chris@0 12 use Symfony\Component\Routing\Route;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Parameter converter for upcasting entity IDs to full objects.
Chris@0 16 *
Chris@0 17 * This is useful in cases where the dynamic elements of the path can't be
Chris@0 18 * auto-determined; for example, if your path refers to multiple of the same
Chris@0 19 * type of entity ("example/{node1}/foo/{node2}") or if the path can act on any
Chris@0 20 * entity type ("example/{entity_type}/{entity}/foo").
Chris@0 21 *
Chris@0 22 * In order to use it you should specify some additional options in your route:
Chris@0 23 * @code
Chris@0 24 * example.route:
Chris@0 25 * path: foo/{example}
Chris@0 26 * options:
Chris@0 27 * parameters:
Chris@0 28 * example:
Chris@0 29 * type: entity:node
Chris@0 30 * @endcode
Chris@0 31 *
Chris@0 32 * If you want to have the entity type itself dynamic in the url you can
Chris@0 33 * specify it like the following:
Chris@0 34 * @code
Chris@0 35 * example.route:
Chris@0 36 * path: foo/{entity_type}/{example}
Chris@0 37 * options:
Chris@0 38 * parameters:
Chris@0 39 * example:
Chris@0 40 * type: entity:{entity_type}
Chris@0 41 * @endcode
Chris@14 42 *
Chris@14 43 * If your route needs to support pending revisions, you can specify the
Chris@14 44 * "load_latest_revision" parameter. This will ensure that the latest revision
Chris@14 45 * is returned, even if it is not the default one:
Chris@14 46 * @code
Chris@14 47 * example.route:
Chris@14 48 * path: foo/{example}
Chris@14 49 * options:
Chris@14 50 * parameters:
Chris@14 51 * example:
Chris@14 52 * type: entity:node
Chris@14 53 * load_latest_revision: TRUE
Chris@14 54 * @endcode
Chris@14 55 *
Chris@14 56 * When dealing with translatable entities, the "load_latest_revision" flag will
Chris@14 57 * make this converter load the latest revision affecting the translation
Chris@14 58 * matching the content language for the current request. If none can be found
Chris@14 59 * it will fall back to the latest revision. For instance, if an entity has an
Chris@14 60 * English default revision (revision 1) and an Italian pending revision
Chris@14 61 * (revision 2), "/foo/1" will return the former, while "/it/foo/1" will return
Chris@14 62 * the latter.
Chris@14 63 *
Chris@14 64 * @see entities_revisions_translations
Chris@0 65 */
Chris@0 66 class EntityConverter implements ParamConverterInterface {
Chris@0 67
Chris@0 68 /**
Chris@0 69 * Entity manager which performs the upcasting in the end.
Chris@0 70 *
Chris@0 71 * @var \Drupal\Core\Entity\EntityManagerInterface
Chris@0 72 */
Chris@0 73 protected $entityManager;
Chris@0 74
Chris@0 75 /**
Chris@14 76 * The language manager.
Chris@14 77 *
Chris@14 78 * @var \Drupal\Core\Language\LanguageManagerInterface
Chris@14 79 */
Chris@14 80 protected $languageManager;
Chris@14 81
Chris@14 82 /**
Chris@0 83 * Constructs a new EntityConverter.
Chris@0 84 *
Chris@0 85 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
Chris@0 86 * The entity manager.
Chris@14 87 * @param \Drupal\Core\Language\LanguageManagerInterface|null $language_manager
Chris@14 88 * (optional) The language manager. Defaults to none.
Chris@0 89 */
Chris@14 90 public function __construct(EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager = NULL) {
Chris@0 91 $this->entityManager = $entity_manager;
Chris@14 92 $this->languageManager = $language_manager;
Chris@0 93 }
Chris@0 94
Chris@0 95 /**
Chris@0 96 * {@inheritdoc}
Chris@0 97 */
Chris@0 98 public function convert($value, $definition, $name, array $defaults) {
Chris@0 99 $entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults);
Chris@14 100 $storage = $this->entityManager->getStorage($entity_type_id);
Chris@14 101 $entity_definition = $this->entityManager->getDefinition($entity_type_id);
Chris@14 102
Chris@14 103 $entity = $storage->load($value);
Chris@14 104
Chris@14 105 // If the entity type is revisionable and the parameter has the
Chris@14 106 // "load_latest_revision" flag, load the latest revision.
Chris@14 107 if ($entity instanceof RevisionableInterface && !empty($definition['load_latest_revision']) && $entity_definition->isRevisionable()) {
Chris@14 108 // Retrieve the latest revision ID taking translations into account.
Chris@14 109 $langcode = $this->languageManager()
Chris@14 110 ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
Chris@14 111 ->getId();
Chris@14 112 $entity = $this->getLatestTranslationAffectedRevision($entity, $langcode);
Chris@14 113 }
Chris@14 114
Chris@14 115 // If the entity type is translatable, ensure we return the proper
Chris@14 116 // translation object for the current context.
Chris@14 117 if ($entity instanceof EntityInterface && $entity instanceof TranslatableInterface) {
Chris@14 118 $entity = $this->entityManager->getTranslationFromContext($entity, NULL, ['operation' => 'entity_upcast']);
Chris@14 119 }
Chris@14 120
Chris@14 121 return $entity;
Chris@14 122 }
Chris@14 123
Chris@14 124 /**
Chris@14 125 * Returns the ID of the latest revision translation of the specified entity.
Chris@14 126 *
Chris@14 127 * @param \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\RevisionableInterface $entity
Chris@14 128 * The default revision of the entity being converted.
Chris@14 129 * @param string $langcode
Chris@14 130 * The language of the revision translation to be loaded.
Chris@14 131 *
Chris@14 132 * @return \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\RevisionableInterface
Chris@14 133 * The latest translation-affecting revision for the specified entity, or
Chris@14 134 * just the latest revision, if the specified entity is not translatable or
Chris@14 135 * does not have a matching translation yet.
Chris@14 136 */
Chris@14 137 protected function getLatestTranslationAffectedRevision(RevisionableInterface $entity, $langcode) {
Chris@14 138 $revision = NULL;
Chris@14 139 $storage = $this->entityManager->getStorage($entity->getEntityTypeId());
Chris@14 140
Chris@14 141 if ($entity instanceof TranslatableRevisionableInterface && $entity->isTranslatable()) {
Chris@14 142 /** @var \Drupal\Core\Entity\TranslatableRevisionableStorageInterface $storage */
Chris@14 143 $revision_id = $storage->getLatestTranslationAffectedRevisionId($entity->id(), $langcode);
Chris@14 144
Chris@14 145 // If the latest translation-affecting revision was a default revision, it
Chris@14 146 // is fine to load the latest revision instead, because in this case the
Chris@14 147 // latest revision, regardless of it being default or pending, will always
Chris@14 148 // contain the most up-to-date values for the specified translation. This
Chris@14 149 // provides a BC behavior when the route is defined by a module always
Chris@14 150 // expecting the latest revision to be loaded and to be the default
Chris@14 151 // revision. In this particular case the latest revision is always going
Chris@14 152 // to be the default revision, since pending revisions would not be
Chris@14 153 // supported.
Chris@14 154 /** @var \Drupal\Core\Entity\TranslatableRevisionableInterface $revision */
Chris@14 155 $revision = $revision_id ? $this->loadRevision($entity, $revision_id) : NULL;
Chris@14 156 if (!$revision || ($revision->wasDefaultRevision() && !$revision->isDefaultRevision())) {
Chris@14 157 $revision = NULL;
Chris@0 158 }
Chris@14 159 }
Chris@14 160
Chris@14 161 // Fall back to the latest revisions if no affected revision for the current
Chris@14 162 // content language could be found. This is acceptable as it means the
Chris@14 163 // entity is not translated. This is the correct logic also on monolingual
Chris@14 164 // sites.
Chris@14 165 if (!isset($revision)) {
Chris@14 166 $revision_id = $storage->getLatestRevisionId($entity->id());
Chris@14 167 $revision = $this->loadRevision($entity, $revision_id);
Chris@14 168 }
Chris@14 169
Chris@14 170 return $revision;
Chris@14 171 }
Chris@14 172
Chris@14 173 /**
Chris@14 174 * Loads the specified entity revision.
Chris@14 175 *
Chris@14 176 * @param \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\RevisionableInterface $entity
Chris@14 177 * The default revision of the entity being converted.
Chris@14 178 * @param string $revision_id
Chris@14 179 * The identifier of the revision to be loaded.
Chris@14 180 *
Chris@14 181 * @return \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\RevisionableInterface
Chris@14 182 * An entity revision object.
Chris@14 183 */
Chris@14 184 protected function loadRevision(RevisionableInterface $entity, $revision_id) {
Chris@14 185 // We explicitly perform a loose equality check, since a revision ID may
Chris@14 186 // be returned as an integer or a string.
Chris@14 187 if ($entity->getLoadedRevisionId() != $revision_id) {
Chris@14 188 $storage = $this->entityManager->getStorage($entity->getEntityTypeId());
Chris@14 189 return $storage->loadRevision($revision_id);
Chris@14 190 }
Chris@14 191 else {
Chris@0 192 return $entity;
Chris@0 193 }
Chris@0 194 }
Chris@0 195
Chris@0 196 /**
Chris@0 197 * {@inheritdoc}
Chris@0 198 */
Chris@0 199 public function applies($definition, $name, Route $route) {
Chris@0 200 if (!empty($definition['type']) && strpos($definition['type'], 'entity:') === 0) {
Chris@0 201 $entity_type_id = substr($definition['type'], strlen('entity:'));
Chris@0 202 if (strpos($definition['type'], '{') !== FALSE) {
Chris@0 203 $entity_type_slug = substr($entity_type_id, 1, -1);
Chris@0 204 return $name != $entity_type_slug && in_array($entity_type_slug, $route->compile()->getVariables(), TRUE);
Chris@0 205 }
Chris@0 206 return $this->entityManager->hasDefinition($entity_type_id);
Chris@0 207 }
Chris@0 208 return FALSE;
Chris@0 209 }
Chris@0 210
Chris@0 211 /**
Chris@0 212 * Determines the entity type ID given a route definition and route defaults.
Chris@0 213 *
Chris@0 214 * @param mixed $definition
Chris@0 215 * The parameter definition provided in the route options.
Chris@0 216 * @param string $name
Chris@0 217 * The name of the parameter.
Chris@0 218 * @param array $defaults
Chris@0 219 * The route defaults array.
Chris@0 220 *
Chris@0 221 * @return string
Chris@0 222 * The entity type ID.
Chris@0 223 *
Chris@0 224 * @throws \Drupal\Core\ParamConverter\ParamNotConvertedException
Chris@0 225 * Thrown when the dynamic entity type is not found in the route defaults.
Chris@0 226 */
Chris@0 227 protected function getEntityTypeFromDefaults($definition, $name, array $defaults) {
Chris@0 228 $entity_type_id = substr($definition['type'], strlen('entity:'));
Chris@0 229
Chris@0 230 // If the entity type is dynamic, it will be pulled from the route defaults.
Chris@0 231 if (strpos($entity_type_id, '{') === 0) {
Chris@0 232 $entity_type_slug = substr($entity_type_id, 1, -1);
Chris@0 233 if (!isset($defaults[$entity_type_slug])) {
Chris@0 234 throw new ParamNotConvertedException(sprintf('The "%s" parameter was not converted because the "%s" parameter is missing', $name, $entity_type_slug));
Chris@0 235 }
Chris@0 236 $entity_type_id = $defaults[$entity_type_slug];
Chris@0 237 }
Chris@0 238 return $entity_type_id;
Chris@0 239 }
Chris@0 240
Chris@14 241 /**
Chris@14 242 * Returns a language manager instance.
Chris@14 243 *
Chris@14 244 * @return \Drupal\Core\Language\LanguageManagerInterface
Chris@14 245 * The language manager.
Chris@14 246 *
Chris@14 247 * @internal
Chris@14 248 */
Chris@14 249 protected function languageManager() {
Chris@14 250 if (!isset($this->languageManager)) {
Chris@14 251 $this->languageManager = \Drupal::languageManager();
Chris@14 252 // @todo Turn this into a proper error (E_USER_ERROR) in
Chris@14 253 // https://www.drupal.org/node/2938929.
Chris@14 254 @trigger_error('The language manager parameter has been added to EntityConverter since version 8.5.0 and will be made required in version 9.0.0 when requesting the latest translation-affected revision of an entity.', E_USER_DEPRECATED);
Chris@14 255 }
Chris@14 256 return $this->languageManager;
Chris@14 257 }
Chris@14 258
Chris@0 259 }