EntityHelper.php in Simple XML sitemap 8.2
File
src/EntityHelper.php
View source
<?php
namespace Drupal\simple_sitemap;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Url;
class EntityHelper {
protected $entityTypeManager;
protected $db;
public function __construct(EntityTypeManagerInterface $entityTypeManager, Connection $database) {
$this->entityTypeManager = $entityTypeManager;
$this->db = $database;
}
public function getEntityInstanceBundleName(EntityInterface $entity) {
return $entity
->getEntityTypeId() === 'menu_link_content' ? $entity
->getMenuName() : $entity
->bundle();
}
public function getBundleEntityTypeId(EntityInterface $entity) {
return $entity
->getEntityTypeId() === 'menu' ? 'menu_link_content' : $entity
->getEntityType()
->getBundleOf();
}
public function getSupportedEntityTypes() {
$entity_types = $this->entityTypeManager
->getDefinitions();
foreach ($entity_types as $entity_type_id => $entity_type) {
if (!$entity_type instanceof ContentEntityTypeInterface || !method_exists($entity_type, 'getBundleEntityType') || !$entity_type
->hasLinkTemplate('canonical')) {
unset($entity_types[$entity_type_id]);
}
}
return $entity_types;
}
public function entityTypeIsAtomic($entity_type_id) {
if ($entity_type_id === 'menu_link_content') {
return FALSE;
}
$entity_types = $this->entityTypeManager
->getDefinitions();
if (!isset($entity_types[$entity_type_id])) {
}
return empty($entity_types[$entity_type_id]
->getBundleEntityType()) ? TRUE : FALSE;
}
public function getEntityFromUrlObject(Url $url_object) {
return $url_object
->isRouted() && !empty($route_parameters = $url_object
->getRouteParameters()) && $this->entityTypeManager
->getDefinition($entity_type_id = key($route_parameters), FALSE) ? $this->entityTypeManager
->getStorage($entity_type_id)
->load($route_parameters[$entity_type_id]) : NULL;
}
public function getEntityImageUrls($entity_type_name, $entity_id) {
$query = $this->db
->select('file_managed', 'fm');
$query
->fields('fm', [
'uri',
]);
$query
->join('file_usage', 'fu', 'fu.fid = fm.fid');
$query
->condition('fm.filemime', 'image/%', 'LIKE');
$query
->condition('fu.type', $entity_type_name);
$query
->condition('fu.id', $entity_id);
foreach ($query
->execute() as $row) {
$imageUris[] = file_create_url($row->uri);
}
return !empty($imageUris) ? $imageUris : [];
}
}