EntityHelper.php in Simple XML sitemap 4.x
File
src/Entity/EntityHelper.php
View source
<?php
namespace Drupal\simple_sitemap\Entity;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Url;
class EntityHelper {
protected $entityTypeManager;
protected $entityTypeBundleInfo;
protected $configFactory;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, ConfigFactoryInterface $configFactory) {
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->configFactory = $configFactory;
}
public function getBundleInfo(string $entity_type_id) : array {
return $this->entityTypeBundleInfo
->getBundleInfo($entity_type_id);
}
public function getBundleLabel(string $entity_type_id, string $bundle_name) {
return $this
->getBundleInfo($entity_type_id)[$bundle_name]['label'] ?? $bundle_name;
}
public function getEntityInstanceBundleName(EntityInterface $entity) : string {
return $entity
->getEntityTypeId() === 'menu_link_content' ? $entity
->getMenuName() : $entity
->bundle();
}
public function getBundleEntityTypeId(EntityInterface $entity) : ?string {
return $entity
->getEntityTypeId() === 'menu' ? 'menu_link_content' : $entity
->getEntityType()
->getBundleOf();
}
public function getSupportedEntityTypes() : array {
return array_filter($this->entityTypeManager
->getDefinitions(), [
$this,
'supports',
]);
}
public function supports(EntityTypeInterface $entity_type) : bool {
if (!$entity_type instanceof ContentEntityTypeInterface || !method_exists($entity_type, 'getBundleEntityType') || !$entity_type
->hasLinkTemplate('canonical')) {
return FALSE;
}
return TRUE;
}
public function entityTypeIsAtomic($entity_type_id) : bool {
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());
}
public function getEntityFromUrlObject(Url $url_object) : ?EntityInterface {
if ($url_object
->isRouted()) {
if ($url_object
->getRouteName() === '<front>' && !empty($uri = $this->configFactory
->get('system.site')
->get('page.front'))) {
$url_object = Url::fromUri('internal:' . $uri);
}
if (!empty($route_parameters = $url_object
->getRouteParameters()) && $this->entityTypeManager
->getDefinition($entity_type_id = key($route_parameters), FALSE)) {
return $this->entityTypeManager
->getStorage($entity_type_id)
->load($route_parameters[$entity_type_id]);
}
}
return NULL;
}
public function getEntityInstanceIds(string $entity_type_id, ?string $bundle_name = NULL) : array {
$sitemap_entity_types = $this
->getSupportedEntityTypes();
if (!isset($sitemap_entity_types[$entity_type_id])) {
return [];
}
$entity_query = $this->entityTypeManager
->getStorage($entity_type_id)
->getQuery();
if ($bundle_name !== NULL && !$this
->entityTypeIsAtomic($entity_type_id)) {
$keys = $sitemap_entity_types[$entity_type_id]
->getKeys();
$keys['bundle'] = $entity_type_id === 'menu_link_content' ? 'menu_name' : $keys['bundle'];
$entity_query
->condition($keys['bundle'], $bundle_name);
}
return $entity_query
->execute();
}
}