Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / simple_sitemap / src / EntityHelper.php
index cb69619991fdf89e41a6be84612d144d2a8e66db..0de9ce0c16b3a891750046a571b7e6dd5c310ddf 100644 (file)
@@ -3,8 +3,10 @@
 namespace Drupal\simple_sitemap;
 
 use Drupal\Core\Entity\ContentEntityTypeInterface;
+use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
-use Drupal\Core\Entity\Entity;
+use Drupal\Core\Database\Connection;
+use Drupal\Core\Url;
 
 /**
  * Class EntityHelper
@@ -17,22 +19,29 @@ class EntityHelper {
    */
   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) {
+  public function __construct(EntityTypeManagerInterface $entityTypeManager, Connection $database) {
     $this->entityTypeManager = $entityTypeManager;
+    $this->db = $database;
   }
 
   /**
    * Gets an entity's bundle name.
    *
-   * @param \Drupal\Core\Entity\Entity $entity
+   * @param \Drupal\Core\Entity\EntityInterface $entity
    * @return string
    */
-  public function getEntityInstanceBundleName(Entity $entity) {
-    return $entity->getEntityTypeId() == 'menu_link_content'
+  public function getEntityInstanceBundleName(EntityInterface $entity) {
+    return $entity->getEntityTypeId() === 'menu_link_content'
       // Menu fix.
       ? $entity->getMenuName() : $entity->bundle();
   }
@@ -40,11 +49,11 @@ class EntityHelper {
   /**
    * Gets the entity type id for a bundle.
    *
-   * @param \Drupal\Core\Entity\Entity $entity
+   * @param \Drupal\Core\Entity\EntityInterface $entity
    * @return null|string
    */
-  public function getBundleEntityTypeId(Entity $entity) {
-    return $entity->getEntityTypeId() == 'menu'
+  public function getBundleEntityTypeId(EntityInterface $entity) {
+    return $entity->getEntityTypeId() === 'menu'
       // Menu fix.
       ? 'menu_link_content' : $entity->getEntityType()->getBundleOf();
   }
@@ -55,7 +64,7 @@ class EntityHelper {
    * @return array
    *   Objects of entity types that can be indexed by the sitemap.
    */
-  public function getSitemapEntityTypes() {
+  public function getSupportedEntityTypes() {
     $entity_types = $this->entityTypeManager->getDefinitions();
 
     foreach ($entity_types as $entity_type_id => $entity_type) {
@@ -75,20 +84,52 @@ class EntityHelper {
    * @return bool
    */
   public function entityTypeIsAtomic($entity_type_id) {
+
     // Menu fix.
-    if ($entity_type_id == 'menu_link_content') {
+    if ($entity_type_id === 'menu_link_content') {
       return FALSE;
     }
 
-    $sitemap_entity_types = $this->getSitemapEntityTypes();
-    if (isset($sitemap_entity_types[$entity_type_id])) {
-      $entity_type = $sitemap_entity_types[$entity_type_id];
-      if (empty($entity_type->getBundleEntityType())) {
-        return TRUE;
-      }
+    $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 $url_object
+   * @return object|null
+   */
+  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 $entity_type_name
+   * @param $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);
     }
-    // todo: throw exception.
-    return FALSE;
+
+    return !empty($imageUris) ? $imageUris : [];
   }
 
 }