EntityConverter.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace Drupal\Core\ParamConverter;
  3. use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
  4. use Drupal\Core\Entity\EntityManagerInterface;
  5. use Drupal\Core\Entity\EntityRepositoryInterface;
  6. use Drupal\Core\Entity\EntityTypeManagerInterface;
  7. use Drupal\Core\Entity\RevisionableInterface;
  8. use Drupal\Core\Language\LanguageInterface;
  9. use Drupal\Core\Plugin\Context\Context;
  10. use Drupal\Core\Plugin\Context\ContextDefinition;
  11. use Drupal\Core\TypedData\TranslatableInterface;
  12. use Symfony\Component\Routing\Route;
  13. /**
  14. * Parameter converter for upcasting entity IDs to full objects.
  15. *
  16. * This is useful in cases where the dynamic elements of the path can't be
  17. * auto-determined; for example, if your path refers to multiple of the same
  18. * type of entity ("example/{node1}/foo/{node2}") or if the path can act on any
  19. * entity type ("example/{entity_type}/{entity}/foo").
  20. *
  21. * In order to use it you should specify some additional options in your route:
  22. * @code
  23. * example.route:
  24. * path: foo/{example}
  25. * options:
  26. * parameters:
  27. * example:
  28. * type: entity:node
  29. * @endcode
  30. *
  31. * If you want to have the entity type itself dynamic in the url you can
  32. * specify it like the following:
  33. * @code
  34. * example.route:
  35. * path: foo/{entity_type}/{example}
  36. * options:
  37. * parameters:
  38. * example:
  39. * type: entity:{entity_type}
  40. * @endcode
  41. *
  42. * If your route needs to support pending revisions, you can specify the
  43. * "load_latest_revision" parameter. This will ensure that the latest revision
  44. * is returned, even if it is not the default one:
  45. * @code
  46. * example.route:
  47. * path: foo/{example}
  48. * options:
  49. * parameters:
  50. * example:
  51. * type: entity:node
  52. * load_latest_revision: TRUE
  53. * @endcode
  54. *
  55. * When dealing with translatable entities, the "load_latest_revision" flag will
  56. * make this converter load the latest revision affecting the translation
  57. * matching the content language for the current request. If none can be found
  58. * it will fall back to the latest revision. For instance, if an entity has an
  59. * English default revision (revision 1) and an Italian pending revision
  60. * (revision 2), "/foo/1" will return the former, while "/it/foo/1" will return
  61. * the latter.
  62. *
  63. * @see entities_revisions_translations
  64. */
  65. class EntityConverter implements ParamConverterInterface {
  66. use DeprecatedServicePropertyTrait;
  67. use DynamicEntityTypeParamConverterTrait;
  68. /**
  69. * {@inheritdoc}
  70. */
  71. protected $deprecatedProperties = [
  72. 'entityManager' => 'entity.manager',
  73. 'languageManager' => 'language_manager',
  74. ];
  75. /**
  76. * Entity type manager which performs the upcasting in the end.
  77. *
  78. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  79. */
  80. protected $entityTypeManager;
  81. /**
  82. * Entity repository.
  83. *
  84. * @var \Drupal\Core\Entity\EntityRepositoryInterface
  85. */
  86. protected $entityRepository;
  87. /**
  88. * Constructs a new EntityConverter.
  89. *
  90. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  91. * The entity type manager.
  92. * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
  93. * The entity repository.
  94. *
  95. * @see https://www.drupal.org/node/2549139
  96. * @see https://www.drupal.org/node/2938929
  97. */
  98. public function __construct(EntityTypeManagerInterface $entity_type_manager, $entity_repository = NULL) {
  99. if ($entity_type_manager instanceof EntityManagerInterface) {
  100. @trigger_error('Passing the entity.manager service to EntityConverter::__construct() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Pass the entity_type.manager service instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
  101. }
  102. $this->entityTypeManager = $entity_type_manager;
  103. if (!($entity_repository instanceof EntityRepositoryInterface)) {
  104. @trigger_error('Calling EntityConverter::__construct() with the $entity_repository argument is supported in drupal:8.7.0 and will be required before drupal:9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
  105. $entity_repository = \Drupal::service('entity.repository');
  106. }
  107. $this->entityRepository = $entity_repository;
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function convert($value, $definition, $name, array $defaults) {
  113. $entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults);
  114. // If the entity type is revisionable and the parameter has the
  115. // "load_latest_revision" flag, load the active variant.
  116. if (!empty($definition['load_latest_revision'])) {
  117. return $this->entityRepository->getActive($entity_type_id, $value);
  118. }
  119. // Do not inject the context repository as it is not an actual dependency:
  120. // it will be removed once both the TODOs below are fixed.
  121. /** @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface $contexts_repository */
  122. $contexts_repository = \Drupal::service('context.repository');
  123. // @todo Consider deprecating the legacy context operation altogether in
  124. // https://www.drupal.org/node/3031124.
  125. $contexts = $contexts_repository->getAvailableContexts();
  126. $contexts[EntityRepositoryInterface::CONTEXT_ID_LEGACY_CONTEXT_OPERATION] =
  127. new Context(new ContextDefinition('string'), 'entity_upcast');
  128. // @todo At the moment we do not need the current user context, which is
  129. // triggering some test failures. We can remove these lines once
  130. // https://www.drupal.org/node/2934192 is fixed.
  131. $context_id = '@user.current_user_context:current_user';
  132. if (isset($contexts[$context_id])) {
  133. $account = $contexts[$context_id]->getContextValue();
  134. unset($account->_skipProtectedUserFieldConstraint);
  135. unset($contexts[$context_id]);
  136. }
  137. $entity = $this->entityRepository->getCanonical($entity_type_id, $value, $contexts);
  138. return $entity;
  139. }
  140. /**
  141. * Returns the latest revision translation of the specified entity.
  142. *
  143. * @param \Drupal\Core\Entity\RevisionableInterface $entity
  144. * The default revision of the entity being converted.
  145. * @param string $langcode
  146. * The language of the revision translation to be loaded.
  147. *
  148. * @return \Drupal\Core\Entity\RevisionableInterface
  149. * The latest translation-affecting revision for the specified entity, or
  150. * just the latest revision, if the specified entity is not translatable or
  151. * does not have a matching translation yet.
  152. *
  153. * @deprecated in drupal:8.7.0 and is removed from drupal:9.0.0.
  154. * Use \Drupal\Core\Entity\EntityRepositoryInterface::getActive() instead.
  155. */
  156. protected function getLatestTranslationAffectedRevision(RevisionableInterface $entity, $langcode) {
  157. @trigger_error('\Drupal\Core\ParamConverter\EntityConverter::getLatestTranslationAffectedRevision() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityRepositoryInterface::getActive() instead.', E_USER_DEPRECATED);
  158. $data_type = 'language';
  159. $context_id_prefix = '@language.current_language_context:';
  160. $contexts = [
  161. $context_id_prefix . LanguageInterface::TYPE_CONTENT => new Context(new ContextDefinition($data_type), $langcode),
  162. $context_id_prefix . LanguageInterface::TYPE_INTERFACE => new Context(new ContextDefinition($data_type), $langcode),
  163. ];
  164. $revision = $this->entityRepository->getActive($entity->getEntityTypeId(), $entity->id(), $contexts);
  165. // The EntityRepositoryInterface::getActive() method performs entity
  166. // translation negotiation, but this used to return an untranslated entity
  167. // object as translation negotiation happened later in ::convert().
  168. if ($revision instanceof TranslatableInterface) {
  169. $revision = $revision->getUntranslated();
  170. }
  171. return $revision;
  172. }
  173. /**
  174. * Loads the specified entity revision.
  175. *
  176. * @param \Drupal\Core\Entity\RevisionableInterface $entity
  177. * The default revision of the entity being converted.
  178. * @param string $revision_id
  179. * The identifier of the revision to be loaded.
  180. *
  181. * @return \Drupal\Core\Entity\RevisionableInterface
  182. * An entity revision object.
  183. *
  184. * @deprecated in drupal:8.7.0 and is removed from drupal:9.0.0.
  185. */
  186. protected function loadRevision(RevisionableInterface $entity, $revision_id) {
  187. @trigger_error('\Drupal\Core\ParamConverter\EntityConverter::loadRevision() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0.', E_USER_DEPRECATED);
  188. // We explicitly perform a loose equality check, since a revision ID may
  189. // be returned as an integer or a string.
  190. if ($entity->getLoadedRevisionId() != $revision_id) {
  191. /** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
  192. $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
  193. return $storage->loadRevision($revision_id);
  194. }
  195. else {
  196. return $entity;
  197. }
  198. }
  199. /**
  200. * {@inheritdoc}
  201. */
  202. public function applies($definition, $name, Route $route) {
  203. if (!empty($definition['type']) && strpos($definition['type'], 'entity:') === 0) {
  204. $entity_type_id = substr($definition['type'], strlen('entity:'));
  205. if (strpos($definition['type'], '{') !== FALSE) {
  206. $entity_type_slug = substr($entity_type_id, 1, -1);
  207. return $name != $entity_type_slug && in_array($entity_type_slug, $route->compile()->getVariables(), TRUE);
  208. }
  209. return $this->entityTypeManager->hasDefinition($entity_type_id);
  210. }
  211. return FALSE;
  212. }
  213. /**
  214. * Returns a language manager instance.
  215. *
  216. * @return \Drupal\Core\Language\LanguageManagerInterface
  217. * The language manager.
  218. *
  219. * @internal
  220. */
  221. protected function languageManager() {
  222. return $this->__get('languageManager');
  223. }
  224. }