abstract class EntityStorageControllerBase

A base entity storage controller class.

Hierarchy

Expanded class hierarchy of EntityStorageControllerBase

1 file declares its use of EntityStorageControllerBase

File

drupal/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php, line 13
Contains \Drupal\Core\Entity\EntityStorageControllerBase.

Namespace

Drupal\Core\Entity
View source
abstract class EntityStorageControllerBase implements EntityStorageControllerInterface, EntityControllerInterface {

  /**
   * Static cache of entities.
   *
   * @var array
   */
  protected $entityCache = array();

  /**
   * Whether this entity type should use the static cache.
   *
   * Set by entity info.
   *
   * @var boolean
   */
  protected $cache;

  /**
   * Entity type for this controller instance.
   *
   * @var string
   */
  protected $entityType;

  /**
   * Array of information about the entity.
   *
   * @var array
   *
   * @see entity_get_info()
   */
  protected $entityInfo;

  /**
   * Additional arguments to pass to hook_TYPE_load().
   *
   * Set before calling Drupal\Core\Entity\DatabaseStorageController::attachLoad().
   *
   * @var array
   */
  protected $hookLoadArguments = array();

  /**
   * Name of the entity's ID field in the entity database table.
   *
   * @var string
   */
  protected $idKey;

  /**
   * Name of entity's UUID database table field, if it supports UUIDs.
   *
   * Has the value FALSE if this entity does not use UUIDs.
   *
   * @var string
   */
  protected $uuidKey;

  /**
   * Constructs an EntityStorageControllerBase instance.
   *
   * @param string $entity_type
   *   The entity type for which the instance is created.
   * @param array $entity_info
   *   An array of entity info for the entity type.
   */
  public function __construct($entity_type, $entity_info) {
    $this->entityType = $entity_type;
    $this->entityInfo = $entity_info;

    // Check if the entity type supports static caching of loaded entities.
    $this->cache = !empty($this->entityInfo['static_cache']);
  }

  /**
   * {@inheritdoc}
   */
  public function loadUnchanged($id) {
    $this
      ->resetCache(array(
      $id,
    ));
    $result = $this
      ->load(array(
      $id,
    ));
    return reset($result);
  }

  /**
   * {@inheritdoc}
   */
  public function resetCache(array $ids = NULL) {
    if ($this->cache && isset($ids)) {
      foreach ($ids as $id) {
        unset($this->entityCache[$id]);
      }
    }
    else {
      $this->entityCache = array();
    }
  }

  /**
   * Gets entities from the static cache.
   *
   * @param $ids
   *   If not empty, return entities that match these IDs.
   *
   * @return
   *   Array of entities from the entity cache.
   */
  protected function cacheGet($ids) {
    $entities = array();

    // Load any available entities from the internal cache.
    if ($this->cache && !empty($this->entityCache)) {
      $entities += array_intersect_key($this->entityCache, array_flip($ids));
    }
    return $entities;
  }

  /**
   * Stores entities in the static entity cache.
   *
   * @param $entities
   *   Entities to store in the cache.
   */
  protected function cacheSet($entities) {
    if ($this->cache) {
      $this->entityCache += $entities;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityControllerInterface::createInstance public static function Instantiates a new instance of this entity controller. 8
EntityStorageControllerBase::$cache protected property Whether this entity type should use the static cache. 1
EntityStorageControllerBase::$entityCache protected property Static cache of entities.
EntityStorageControllerBase::$entityInfo protected property Array of information about the entity.
EntityStorageControllerBase::$entityType protected property Entity type for this controller instance.
EntityStorageControllerBase::$hookLoadArguments protected property Additional arguments to pass to hook_TYPE_load().
EntityStorageControllerBase::$idKey protected property Name of the entity's ID field in the entity database table.
EntityStorageControllerBase::$uuidKey protected property Name of entity's UUID database table field, if it supports UUIDs. 1
EntityStorageControllerBase::cacheGet protected function Gets entities from the static cache.
EntityStorageControllerBase::cacheSet protected function Stores entities in the static entity cache.
EntityStorageControllerBase::loadUnchanged public function Loads an unchanged entity from the database. Overrides EntityStorageControllerInterface::loadUnchanged
EntityStorageControllerBase::resetCache public function Resets the internal, static entity cache. Overrides EntityStorageControllerInterface::resetCache 3
EntityStorageControllerBase::__construct public function Constructs an EntityStorageControllerBase instance. 2
EntityStorageControllerInterface::create public function Constructs a new entity object, without permanently saving it. 2
EntityStorageControllerInterface::delete public function Deletes permanently saved entities. 2
EntityStorageControllerInterface::deleteRevision public function Delete a specific entity revision. 2
EntityStorageControllerInterface::getFieldDefinitions public function Gets an array of entity field definitions. 2
EntityStorageControllerInterface::getQueryServicename public function Gets the name of the service for the query for this entity storage. 1
EntityStorageControllerInterface::load public function Loads one or more entities. 2
EntityStorageControllerInterface::loadByProperties public function Load entities by their property values. 2
EntityStorageControllerInterface::loadRevision public function Load a specific entity revision. 2
EntityStorageControllerInterface::save public function Saves the entity permanently. 2