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

namespace Drupal\simple_sitemap;

use Drupal\Core\Entity\ContentEntityTypeInterface;
gbyte.co's avatar
gbyte.co committed
6
use Drupal\Core\Entity\EntityInterface;
7
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
8
use Drupal\Core\Entity\EntityTypeManagerInterface;
9 10
use Drupal\Core\Database\Connection;
use Drupal\Core\Url;
11 12

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

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

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

33 34 35 36 37
  /**
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeBundleInfo;

38 39
  /**
   * EntityHelper constructor.
40
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
41
   * @param \Drupal\Core\Database\Connection $database
42
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
43
   */
44 45
  public function __construct(EntityTypeManagerInterface $entity_type_manager, Connection $database, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
    $this->entityTypeManager = $entity_type_manager;
46
    $this->db = $database;
47 48 49 50 51 52 53 54 55 56
    $this->entityTypeBundleInfo = $entity_type_bundle_info;
  }

  /**
   * @param string $entity_type_name
   * @param string $bundle_name
   * @return mixed
   */
  public function getBundleLabel($entity_type_name, $bundle_name) {
    return $this->entityTypeBundleInfo->getBundleInfo($entity_type_name)[$bundle_name]['label'];
57 58 59 60 61
  }

  /**
   * Gets an entity's bundle name.
   *
gbyte.co's avatar
gbyte.co committed
62
   * @param \Drupal\Core\Entity\EntityInterface $entity
63 64
   *   The entity to get the bundle name for.
   *
65
   * @return string
66
   *   The bundle of the entity.
67
   */
gbyte.co's avatar
gbyte.co committed
68
  public function getEntityInstanceBundleName(EntityInterface $entity) {
gbyte.co's avatar
gbyte.co committed
69
    return $entity->getEntityTypeId() === 'menu_link_content'
70 71 72 73 74 75 76
      // Menu fix.
      ? $entity->getMenuName() : $entity->bundle();
  }

  /**
   * Gets the entity type id for a bundle.
   *
gbyte.co's avatar
gbyte.co committed
77
   * @param \Drupal\Core\Entity\EntityInterface $entity
78 79
   *   The entity to get an entity type id for a bundle.
   *
80
   * @return null|string
81
   *   The entity type for a bundle or NULL on failure.
82
   */
gbyte.co's avatar
gbyte.co committed
83
  public function getBundleEntityTypeId(EntityInterface $entity) {
gbyte.co's avatar
gbyte.co committed
84
    return $entity->getEntityTypeId() === 'menu'
85 86 87 88 89 90 91 92 93 94
      // 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.
   */
gbyte.co's avatar
gbyte.co committed
95
  public function getSupportedEntityTypes() {
96

97 98
    /** @var \Drupal\Core\Entity\ContentEntityTypeInterface[] $entity_types */
    $entity_types = $this->entityTypeManager->getDefinitions();
99 100 101 102 103 104 105 106 107 108 109 110 111 112
    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
113 114
   *   The entity type ID.
   *
115
   * @return bool
116
   *   TRUE if the entity type is atomic and FALSE otherwise.
117 118
   */
  public function entityTypeIsAtomic($entity_type_id) {
gbyte.co's avatar
gbyte.co committed
119

120
    // Menu fix.
gbyte.co's avatar
gbyte.co committed
121
    if ($entity_type_id === 'menu_link_content') {
122 123 124
      return FALSE;
    }

gbyte.co's avatar
gbyte.co committed
125 126 127 128
    $entity_types = $this->entityTypeManager->getDefinitions();

    if (!isset($entity_types[$entity_type_id])) {
      // todo: Throw exception.
129
    }
gbyte.co's avatar
gbyte.co committed
130 131

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

134
  /**
135 136
   * Gets the entity from URL object.
   *
137
   * @param \Drupal\Core\Url $url_object
138 139
   *   The URL object.
   *
140
   * @return \Drupal\Core\Entity\EntityInterface|null
141 142
   *   An entity object. NULL if no matching entity is found.
   *
143 144
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
145 146
   */
  public function getEntityFromUrlObject(Url $url_object) {
147 148 149
    return $url_object->isRouted()
    && !empty($route_parameters = $url_object->getRouteParameters())
    && $this->entityTypeManager->getDefinition($entity_type_id = key($route_parameters), FALSE)
150 151 152 153 154
      ? $this->entityTypeManager->getStorage($entity_type_id)
        ->load($route_parameters[$entity_type_id])
      : NULL;
  }

155
  /**
156 157
   * Gets the entity IDs by entity type and bundle.
   *
158
   * @param string $entity_type_id
159
   *   The entity type ID.
160
   * @param string|null $bundle_name
161 162 163 164 165
   *   The bundle name.
   *
   * @return array
   *   An array of entity IDs
   *
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
   * @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();
  }

188
}