EntityRepository.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
  4. use Drupal\Core\Language\LanguageInterface;
  5. use Drupal\Core\Language\LanguageManagerInterface;
  6. use Drupal\Core\TypedData\TranslatableInterface as TranslatableDataInterface;
  7. /**
  8. * Provides several mechanisms for retrieving entities.
  9. */
  10. class EntityRepository implements EntityRepositoryInterface {
  11. /**
  12. * The entity type manager.
  13. *
  14. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  15. */
  16. protected $entityTypeManager;
  17. /**
  18. * The language manager.
  19. *
  20. * @var \Drupal\Core\Language\LanguageManagerInterface
  21. */
  22. protected $languageManager;
  23. /**
  24. * Constructs a new EntityRepository.
  25. *
  26. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  27. * The entity type manager.
  28. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
  29. * The language manager.
  30. */
  31. public function __construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager) {
  32. $this->entityTypeManager = $entity_type_manager;
  33. $this->languageManager = $language_manager;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function loadEntityByUuid($entity_type_id, $uuid) {
  39. $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
  40. if (!$uuid_key = $entity_type->getKey('uuid')) {
  41. throw new EntityStorageException("Entity type $entity_type_id does not support UUIDs.");
  42. }
  43. $entities = $this->entityTypeManager->getStorage($entity_type_id)->loadByProperties([$uuid_key => $uuid]);
  44. return ($entities) ? reset($entities) : NULL;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function loadEntityByConfigTarget($entity_type_id, $target) {
  50. $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
  51. // For configuration entities, the config target is given by the entity ID.
  52. // @todo Consider adding a method to allow entity types to indicate the
  53. // target identifier key rather than hard-coding this check. Issue:
  54. // https://www.drupal.org/node/2412983.
  55. if ($entity_type instanceof ConfigEntityTypeInterface) {
  56. $entity = $this->entityTypeManager->getStorage($entity_type_id)->load($target);
  57. }
  58. // For content entities, the config target is given by the UUID.
  59. else {
  60. $entity = $this->loadEntityByUuid($entity_type_id, $target);
  61. }
  62. return $entity;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function getTranslationFromContext(EntityInterface $entity, $langcode = NULL, $context = []) {
  68. $translation = $entity;
  69. if ($entity instanceof TranslatableDataInterface && count($entity->getTranslationLanguages()) > 1) {
  70. if (empty($langcode)) {
  71. $langcode = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
  72. $entity->addCacheContexts(['languages:' . LanguageInterface::TYPE_CONTENT]);
  73. }
  74. // Retrieve language fallback candidates to perform the entity language
  75. // negotiation, unless the current translation is already the desired one.
  76. if ($entity->language()->getId() != $langcode) {
  77. $context['data'] = $entity;
  78. $context += ['operation' => 'entity_view', 'langcode' => $langcode];
  79. $candidates = $this->languageManager->getFallbackCandidates($context);
  80. // Ensure the default language has the proper language code.
  81. $default_language = $entity->getUntranslated()->language();
  82. $candidates[$default_language->getId()] = LanguageInterface::LANGCODE_DEFAULT;
  83. // Return the most fitting entity translation.
  84. foreach ($candidates as $candidate) {
  85. if ($entity->hasTranslation($candidate)) {
  86. $translation = $entity->getTranslation($candidate);
  87. break;
  88. }
  89. }
  90. }
  91. }
  92. return $translation;
  93. }
  94. }