EntityHelper.php 5.04 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\simple_sitemap;

use Drupal\Core\Entity\ContentEntityTypeInterface;
Pawel G's avatar
Pawel G committed
6
use Drupal\Core\Entity\EntityInterface;
7
use Drupal\Core\Entity\EntityTypeManagerInterface;
8 9
use Drupal\Core\Database\Connection;
use Drupal\Core\Url;
10 11

/**
12 13
 * Helper class for working with entities.
 *
14 15 16 17 18
 * @package Drupal\simple_sitemap
 */
class EntityHelper {

  /**
19 20
   * The entity type manager.
   *
21 22
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
23
  protected $entityTypeManager;
24

25
  /**
26 27
   * The current active database's master connection.
   *
28 29 30 31
   * @var \Drupal\Core\Database\Connection
   */
  protected $db;

32 33
  /**
   * EntityHelper constructor.
34
   *
35
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
36
   *   The entity type manager.
37
   * @param \Drupal\Core\Database\Connection $database
38
   *   The current active database's master connection.
39
   */
40
  public function __construct(EntityTypeManagerInterface $entityTypeManager, Connection $database) {
41
    $this->entityTypeManager = $entityTypeManager;
42
    $this->db = $database;
43 44 45 46 47
  }

  /**
   * Gets an entity's bundle name.
   *
Pawel G's avatar
Pawel G committed
48
   * @param \Drupal\Core\Entity\EntityInterface $entity
49 50
   *   The entity to get the bundle name for.
   *
51
   * @return string
52
   *   The bundle of the entity.
53
   */
Pawel G's avatar
Pawel G committed
54
  public function getEntityInstanceBundleName(EntityInterface $entity) {
Pawel G's avatar
Pawel G committed
55
    return $entity->getEntityTypeId() === 'menu_link_content'
56 57 58 59 60 61 62
      // Menu fix.
      ? $entity->getMenuName() : $entity->bundle();
  }

  /**
   * Gets the entity type id for a bundle.
   *
Pawel G's avatar
Pawel G committed
63
   * @param \Drupal\Core\Entity\EntityInterface $entity
64 65
   *   The entity to get an entity type id for a bundle.
   *
66
   * @return null|string
67
   *   The entity type for a bundle or NULL on failure.
68
   */
Pawel G's avatar
Pawel G committed
69
  public function getBundleEntityTypeId(EntityInterface $entity) {
Pawel G's avatar
Pawel G committed
70
    return $entity->getEntityTypeId() === 'menu'
71 72 73 74 75 76 77 78 79 80
      // 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.
   */
Pawel G's avatar
Pawel G committed
81
  public function getSupportedEntityTypes() {
82

83 84
    /** @var \Drupal\Core\Entity\ContentEntityTypeInterface[] $entity_types */
    $entity_types = $this->entityTypeManager->getDefinitions();
85 86 87 88 89 90 91 92 93 94 95 96 97 98
    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
99 100
   *   The entity type ID.
   *
101
   * @return bool
102
   *   TRUE if the entity type is atomic and FALSE otherwise.
103 104
   */
  public function entityTypeIsAtomic($entity_type_id) {
Pawel G's avatar
Pawel G committed
105

106
    // Menu fix.
Pawel G's avatar
Pawel G committed
107
    if ($entity_type_id === 'menu_link_content') {
108 109 110
      return FALSE;
    }

Pawel G's avatar
Pawel G committed
111 112 113 114
    $entity_types = $this->entityTypeManager->getDefinitions();

    if (!isset($entity_types[$entity_type_id])) {
      // todo: Throw exception.
115
    }
Pawel G's avatar
Pawel G committed
116 117

    return empty($entity_types[$entity_type_id]->getBundleEntityType()) ? TRUE : FALSE;
118 119
  }

120
  /**
121 122
   * Gets the entity from URL object.
   *
Pawel G's avatar
Pawel G committed
123
   * @param \Drupal\Core\Url $url_object
124 125
   *   The URL object.
   *
Pawel G's avatar
Pawel G committed
126
   * @return \Drupal\Core\Entity\EntityInterface|null
127 128
   *   An entity object. NULL if no matching entity is found.
   *
Pawel G's avatar
Pawel G committed
129 130
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
131 132
   */
  public function getEntityFromUrlObject(Url $url_object) {
133 134 135
    return $url_object->isRouted()
    && !empty($route_parameters = $url_object->getRouteParameters())
    && $this->entityTypeManager->getDefinition($entity_type_id = key($route_parameters), FALSE)
136 137 138 139 140
      ? $this->entityTypeManager->getStorage($entity_type_id)
        ->load($route_parameters[$entity_type_id])
      : NULL;
  }

141
  /**
142 143
   * Gets the entity IDs by entity type and bundle.
   *
144
   * @param string $entity_type_id
145
   *   The entity type ID.
146
   * @param string|null $bundle_name
147 148 149 150 151
   *   The bundle name.
   *
   * @return array
   *   An array of entity IDs
   *
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
   * @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();
  }

174
}