3 namespace Drupal\simple_sitemap;
5 use Drupal\Core\Entity\ContentEntityTypeInterface;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Drupal\Core\Entity\Entity;
11 * @package Drupal\simple_sitemap
16 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
18 protected $entityTypeManager;
21 * EntityHelper constructor.
22 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
24 public function __construct(EntityTypeManagerInterface $entityTypeManager) {
25 $this->entityTypeManager = $entityTypeManager;
29 * Gets an entity's bundle name.
31 * @param \Drupal\Core\Entity\Entity $entity
34 public function getEntityInstanceBundleName(Entity $entity) {
35 return $entity->getEntityTypeId() == 'menu_link_content'
37 ? $entity->getMenuName() : $entity->bundle();
41 * Gets the entity type id for a bundle.
43 * @param \Drupal\Core\Entity\Entity $entity
46 public function getBundleEntityTypeId(Entity $entity) {
47 return $entity->getEntityTypeId() == 'menu'
49 ? 'menu_link_content' : $entity->getEntityType()->getBundleOf();
53 * Returns objects of entity types that can be indexed.
56 * Objects of entity types that can be indexed by the sitemap.
58 public function getSitemapEntityTypes() {
59 $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]);
72 * Checks whether an entity type does not provide bundles.
74 * @param string $entity_type_id
77 public function entityTypeIsAtomic($entity_type_id) {
79 if ($entity_type_id == 'menu_link_content') {
83 $sitemap_entity_types = $this->getSitemapEntityTypes();
84 if (isset($sitemap_entity_types[$entity_type_id])) {
85 $entity_type = $sitemap_entity_types[$entity_type_id];
86 if (empty($entity_type->getBundleEntityType())) {
90 // todo: throw exception.