123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?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
- * @package Drupal\simple_sitemap
- */
- class EntityHelper {
- /**
- * @var \Drupal\Core\Entity\EntityTypeManagerInterface
- */
- protected $entityTypeManager;
- /**
- * @var \Drupal\Core\Database\Connection
- */
- protected $db;
- /**
- * EntityHelper constructor.
- * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
- * @param \Drupal\Core\Database\Connection $database
- */
- public function __construct(EntityTypeManagerInterface $entityTypeManager, Connection $database) {
- $this->entityTypeManager = $entityTypeManager;
- $this->db = $database;
- }
- /**
- * Gets an entity's bundle name.
- *
- * @param \Drupal\Core\Entity\EntityInterface $entity
- * @return string
- */
- public function getEntityInstanceBundleName(EntityInterface $entity) {
- return $entity->getEntityTypeId() === 'menu_link_content'
- // Menu fix.
- ? $entity->getMenuName() : $entity->bundle();
- }
- /**
- * Gets the entity type id for a bundle.
- *
- * @param \Drupal\Core\Entity\EntityInterface $entity
- * @return null|string
- */
- public function getBundleEntityTypeId(EntityInterface $entity) {
- return $entity->getEntityTypeId() === 'menu'
- // Menu fix.
- ? 'menu_link_content' : $entity->getEntityType()->getBundleOf();
- }
- /**
- * Returns objects of entity types that can be indexed.
- *
- * @return array
- * Objects of entity types that can be indexed by the sitemap.
- */
- public function getSupportedEntityTypes() {
- /** @var \Drupal\Core\Entity\ContentEntityTypeInterface[] $entity_types */
- $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;
- }
- /**
- * Checks whether an entity type does not provide bundles.
- *
- * @param string $entity_type_id
- * @return bool
- */
- public function entityTypeIsAtomic($entity_type_id) {
- // Menu fix.
- if ($entity_type_id === 'menu_link_content') {
- return FALSE;
- }
- $entity_types = $this->entityTypeManager->getDefinitions();
- if (!isset($entity_types[$entity_type_id])) {
- // todo: Throw exception.
- }
- return empty($entity_types[$entity_type_id]->getBundleEntityType()) ? TRUE : FALSE;
- }
- /**
- * @param \Drupal\Core\Url $url_object
- * @return \Drupal\Core\Entity\EntityInterface|null
- * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
- * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
- */
- 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;
- }
- /**
- * @param string $entity_type_id
- * @param string|null $bundle_name
- * @return array|int
- * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
- * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
- */
- public function getEntityInstanceIds($entity_type_id, $bundle_name = NULL) {
- $sitemap_entity_types = $this->getSupportedEntityTypes();
- if (!isset($sitemap_entity_types[$entity_type_id])) {
- return [];
- }
- $entity_query = $this->entityTypeManager->getStorage($entity_type_id)->getQuery();
- if (!$this->entityTypeIsAtomic($entity_type_id) && NULL !== $bundle_name) {
- $keys = $sitemap_entity_types[$entity_type_id]->getKeys();
- // Menu fix.
- $keys['bundle'] = $entity_type_id === 'menu_link_content' ? 'menu_name' : $keys['bundle'];
- $entity_query->condition($keys['bundle'], $bundle_name);
- }
- return $entity_query->execute();
- }
- /**
- * @param string $entity_type_name
- * @param string $entity_id
- * @return array
- */
- 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 : [];
- }
- }
|