0de9ce0c16b3a891750046a571b7e6dd5c310ddf
[yaffs-website] / web / modules / contrib / simple_sitemap / src / EntityHelper.php
1 <?php
2
3 namespace Drupal\simple_sitemap;
4
5 use Drupal\Core\Entity\ContentEntityTypeInterface;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Drupal\Core\Database\Connection;
9 use Drupal\Core\Url;
10
11 /**
12  * Class EntityHelper
13  * @package Drupal\simple_sitemap
14  */
15 class EntityHelper {
16
17   /**
18    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
19    */
20   protected $entityTypeManager;
21
22   /**
23    * @var \Drupal\Core\Database\Connection
24    */
25   protected $db;
26
27   /**
28    * EntityHelper constructor.
29    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
30    * @param \Drupal\Core\Database\Connection $database
31    */
32   public function __construct(EntityTypeManagerInterface $entityTypeManager, Connection $database) {
33     $this->entityTypeManager = $entityTypeManager;
34     $this->db = $database;
35   }
36
37   /**
38    * Gets an entity's bundle name.
39    *
40    * @param \Drupal\Core\Entity\EntityInterface $entity
41    * @return string
42    */
43   public function getEntityInstanceBundleName(EntityInterface $entity) {
44     return $entity->getEntityTypeId() === 'menu_link_content'
45       // Menu fix.
46       ? $entity->getMenuName() : $entity->bundle();
47   }
48
49   /**
50    * Gets the entity type id for a bundle.
51    *
52    * @param \Drupal\Core\Entity\EntityInterface $entity
53    * @return null|string
54    */
55   public function getBundleEntityTypeId(EntityInterface $entity) {
56     return $entity->getEntityTypeId() === 'menu'
57       // Menu fix.
58       ? 'menu_link_content' : $entity->getEntityType()->getBundleOf();
59   }
60
61   /**
62    * Returns objects of entity types that can be indexed.
63    *
64    * @return array
65    *   Objects of entity types that can be indexed by the sitemap.
66    */
67   public function getSupportedEntityTypes() {
68     $entity_types = $this->entityTypeManager->getDefinitions();
69
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]);
75       }
76     }
77     return $entity_types;
78   }
79
80   /**
81    * Checks whether an entity type does not provide bundles.
82    *
83    * @param string $entity_type_id
84    * @return bool
85    */
86   public function entityTypeIsAtomic($entity_type_id) {
87
88     // Menu fix.
89     if ($entity_type_id === 'menu_link_content') {
90       return FALSE;
91     }
92
93     $entity_types = $this->entityTypeManager->getDefinitions();
94
95     if (!isset($entity_types[$entity_type_id])) {
96       // todo: Throw exception.
97     }
98
99     return empty($entity_types[$entity_type_id]->getBundleEntityType()) ? TRUE : FALSE;
100   }
101
102   /**
103    * @param $url_object
104    * @return object|null
105    */
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])
112       : NULL;
113   }
114
115   /**
116    * @param $entity_type_name
117    * @param $entity_id
118    * @return array
119    */
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);
127
128     foreach ($query->execute() as $row) {
129       $imageUris[] = file_create_url($row->uri);
130     }
131
132     return !empty($imageUris) ? $imageUris : [];
133   }
134
135 }