EntityHelper.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace Drupal\simple_sitemap;
  3. use Drupal\Core\Entity\ContentEntityTypeInterface;
  4. use Drupal\Core\Entity\EntityInterface;
  5. use Drupal\Core\Entity\EntityTypeManagerInterface;
  6. use Drupal\Core\Database\Connection;
  7. use Drupal\Core\Url;
  8. /**
  9. * Class EntityHelper
  10. * @package Drupal\simple_sitemap
  11. */
  12. class EntityHelper {
  13. /**
  14. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  15. */
  16. protected $entityTypeManager;
  17. /**
  18. * @var \Drupal\Core\Database\Connection
  19. */
  20. protected $db;
  21. /**
  22. * EntityHelper constructor.
  23. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
  24. * @param \Drupal\Core\Database\Connection $database
  25. */
  26. public function __construct(EntityTypeManagerInterface $entityTypeManager, Connection $database) {
  27. $this->entityTypeManager = $entityTypeManager;
  28. $this->db = $database;
  29. }
  30. /**
  31. * Gets an entity's bundle name.
  32. *
  33. * @param \Drupal\Core\Entity\EntityInterface $entity
  34. * @return string
  35. */
  36. public function getEntityInstanceBundleName(EntityInterface $entity) {
  37. return $entity->getEntityTypeId() === 'menu_link_content'
  38. // Menu fix.
  39. ? $entity->getMenuName() : $entity->bundle();
  40. }
  41. /**
  42. * Gets the entity type id for a bundle.
  43. *
  44. * @param \Drupal\Core\Entity\EntityInterface $entity
  45. * @return null|string
  46. */
  47. public function getBundleEntityTypeId(EntityInterface $entity) {
  48. return $entity->getEntityTypeId() === 'menu'
  49. // Menu fix.
  50. ? 'menu_link_content' : $entity->getEntityType()->getBundleOf();
  51. }
  52. /**
  53. * Returns objects of entity types that can be indexed.
  54. *
  55. * @return array
  56. * Objects of entity types that can be indexed by the sitemap.
  57. */
  58. public function getSupportedEntityTypes() {
  59. /** @var \Drupal\Core\Entity\ContentEntityTypeInterface[] $entity_types */
  60. $entity_types = $this->entityTypeManager->getDefinitions();
  61. foreach ($entity_types as $entity_type_id => $entity_type) {
  62. if (!$entity_type instanceof ContentEntityTypeInterface
  63. || !method_exists($entity_type, 'getBundleEntityType')
  64. || !$entity_type->hasLinkTemplate('canonical')) {
  65. unset($entity_types[$entity_type_id]);
  66. }
  67. }
  68. return $entity_types;
  69. }
  70. /**
  71. * Checks whether an entity type does not provide bundles.
  72. *
  73. * @param string $entity_type_id
  74. * @return bool
  75. */
  76. public function entityTypeIsAtomic($entity_type_id) {
  77. // Menu fix.
  78. if ($entity_type_id === 'menu_link_content') {
  79. return FALSE;
  80. }
  81. $entity_types = $this->entityTypeManager->getDefinitions();
  82. if (!isset($entity_types[$entity_type_id])) {
  83. // todo: Throw exception.
  84. }
  85. return empty($entity_types[$entity_type_id]->getBundleEntityType()) ? TRUE : FALSE;
  86. }
  87. /**
  88. * @param \Drupal\Core\Url $url_object
  89. * @return \Drupal\Core\Entity\EntityInterface|null
  90. * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
  91. * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
  92. */
  93. public function getEntityFromUrlObject(Url $url_object) {
  94. return $url_object->isRouted()
  95. && !empty($route_parameters = $url_object->getRouteParameters())
  96. && $this->entityTypeManager->getDefinition($entity_type_id = key($route_parameters), FALSE)
  97. ? $this->entityTypeManager->getStorage($entity_type_id)
  98. ->load($route_parameters[$entity_type_id])
  99. : NULL;
  100. }
  101. /**
  102. * @param string $entity_type_id
  103. * @param string|null $bundle_name
  104. * @return array|int
  105. * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
  106. * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
  107. */
  108. public function getEntityInstanceIds($entity_type_id, $bundle_name = NULL) {
  109. $sitemap_entity_types = $this->getSupportedEntityTypes();
  110. if (!isset($sitemap_entity_types[$entity_type_id])) {
  111. return [];
  112. }
  113. $entity_query = $this->entityTypeManager->getStorage($entity_type_id)->getQuery();
  114. if (!$this->entityTypeIsAtomic($entity_type_id) && NULL !== $bundle_name) {
  115. $keys = $sitemap_entity_types[$entity_type_id]->getKeys();
  116. // Menu fix.
  117. $keys['bundle'] = $entity_type_id === 'menu_link_content' ? 'menu_name' : $keys['bundle'];
  118. $entity_query->condition($keys['bundle'], $bundle_name);
  119. }
  120. return $entity_query->execute();
  121. }
  122. /**
  123. * @param string $entity_type_name
  124. * @param string $entity_id
  125. * @return array
  126. */
  127. public function getEntityImageUrls($entity_type_name, $entity_id) {
  128. $query = $this->db->select('file_managed', 'fm');
  129. $query->fields('fm', ['uri']);
  130. $query->join('file_usage', 'fu', 'fu.fid = fm.fid');
  131. $query->condition('fm.filemime', 'image/%', 'LIKE');
  132. $query->condition('fu.type', $entity_type_name);
  133. $query->condition('fu.id', $entity_id);
  134. foreach ($query->execute() as $row) {
  135. $imageUris[] = file_create_url($row->uri);
  136. }
  137. return !empty($imageUris) ? $imageUris : [];
  138. }
  139. }