3 namespace Drupal\simple_sitemap;
5 use Drupal\Core\Entity\ContentEntityTypeInterface;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Drupal\Core\Database\Connection;
13 * @package Drupal\simple_sitemap
18 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
20 protected $entityTypeManager;
23 * @var \Drupal\Core\Database\Connection
28 * EntityHelper constructor.
29 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
30 * @param \Drupal\Core\Database\Connection $database
32 public function __construct(EntityTypeManagerInterface $entityTypeManager, Connection $database) {
33 $this->entityTypeManager = $entityTypeManager;
34 $this->db = $database;
38 * Gets an entity's bundle name.
40 * @param \Drupal\Core\Entity\EntityInterface $entity
43 public function getEntityInstanceBundleName(EntityInterface $entity) {
44 return $entity->getEntityTypeId() === 'menu_link_content'
46 ? $entity->getMenuName() : $entity->bundle();
50 * Gets the entity type id for a bundle.
52 * @param \Drupal\Core\Entity\EntityInterface $entity
55 public function getBundleEntityTypeId(EntityInterface $entity) {
56 return $entity->getEntityTypeId() === 'menu'
58 ? 'menu_link_content' : $entity->getEntityType()->getBundleOf();
62 * Returns objects of entity types that can be indexed.
65 * Objects of entity types that can be indexed by the sitemap.
67 public function getSupportedEntityTypes() {
68 $entity_types = $this->entityTypeManager->getDefinitions();
70 foreach ($entity_types as $entity_type_id => $entity_type) {
71 if (!$entity_type instanceof ContentEntityTypeInterface
72 || !method_exists($entity_type, 'getBundleEntityType')
73 || !$entity_type->hasLinkTemplate('canonical')) {
74 unset($entity_types[$entity_type_id]);
81 * Checks whether an entity type does not provide bundles.
83 * @param string $entity_type_id
86 public function entityTypeIsAtomic($entity_type_id) {
89 if ($entity_type_id === 'menu_link_content') {
93 $entity_types = $this->entityTypeManager->getDefinitions();
95 if (!isset($entity_types[$entity_type_id])) {
96 // todo: Throw exception.
99 return empty($entity_types[$entity_type_id]->getBundleEntityType()) ? TRUE : FALSE;
104 * @return object|null
106 public function getEntityFromUrlObject(Url $url_object) {
107 return $url_object->isRouted()
108 && !empty($route_parameters = $url_object->getRouteParameters())
109 && $this->entityTypeManager->getDefinition($entity_type_id = key($route_parameters), FALSE)
110 ? $this->entityTypeManager->getStorage($entity_type_id)
111 ->load($route_parameters[$entity_type_id])
116 * @param $entity_type_name
120 public function getEntityImageUrls($entity_type_name, $entity_id) {
121 $query = $this->db->select('file_managed', 'fm');
122 $query->fields('fm', ['uri']);
123 $query->join('file_usage', 'fu', 'fu.fid = fm.fid');
124 $query->condition('fm.filemime', 'image/%', 'LIKE');
125 $query->condition('fu.type', $entity_type_name);
126 $query->condition('fu.id', $entity_id);
128 foreach ($query->execute() as $row) {
129 $imageUris[] = file_create_url($row->uri);
132 return !empty($imageUris) ? $imageUris : [];