You are here

class MultifieldEntityController in Multifield 7

Same name and namespace in other branches
  1. 7.2 MultifieldEntityController.php \MultifieldEntityController

Hierarchy

Expanded class hierarchy of MultifieldEntityController

1 string reference to 'MultifieldEntityController'
multifield_entity_info in ./multifield.module
Implements hook_entity_info().

File

./MultifieldEntityController.php, line 3

View source
class MultifieldEntityController extends DrupalDefaultEntityController {
  public function load($ids = array(), $conditions = array()) {
    if (!empty($conditions)) {
      throw new InvalidArgumentException('MultifieldEntityController does not support entity_load() using conditions.');
    }
    $entities = array();

    // Create a new variable which is either a prepared version of the $ids
    // array for later comparison with the entity cache, or FALSE if no $ids
    // were passed. The $ids array is reduced as items are loaded from cache,
    // and we need to know if it's empty for this reason to avoid querying the
    // database when all requested entities are loaded from cache.
    $passed_ids = !empty($ids) ? array_flip($ids) : FALSE;

    // Try to load entities from the static cache, if the entity type supports
    // static caching.
    if ($this->cache) {
      $entities += $this
        ->cacheGet($ids, $conditions);

      // If any entities were loaded, remove them from the ids still to load.
      if ($passed_ids) {
        $ids = array_keys(array_diff_key($passed_ids, $entities));
      }
    }

    // Load any remaining entities from the database. This is the case if $ids
    // is set to FALSE (so we load all entities), if there are any ids left to
    // load, if loading a revision, or if $conditions was passed without $ids.
    if ($ids === FALSE || $ids) {
      $entities += $this
        ->queryLoad($ids);
    }
    return $entities;
  }
  protected function queryLoad($ids) {
    $multifields = multifield_get_fields();
    foreach (array_keys($multifields) as $field_name) {
      $query = new EntityFieldQuery();
      if ($ids) {
        $query
          ->fieldCondition($field_name, 'id', $ids, 'IN');
      }
      else {
        $query
          ->fieldCondition($field_name, 'id', 0, '>');
      }
      if ($results = $query
        ->execute()) {
        $pseudo_entities = array();
        $field = field_info_field($field_name);
        foreach ($results as $entity_type => $entities) {

          // Simply doing an entity load on the entities with multifield values
          // will cause the cacheSet() from multifield_field_load() to get
          // invoked.
          $entities = entity_load($entity_type, array_keys($entities));
          foreach ($entities as $entity) {
            if ($items = field_get_items($entity_type, $entity, $field_name)) {
              foreach ($items as $item) {
                $pseudo_entities[$item['id']] = _multifield_field_item_to_entity($field['type'], $item);
              }
            }
          }
        }
        $this
          ->cacheSet($pseudo_entities);
      }
    }
    return array_intersect_key($this->entityCache, drupal_map_assoc($ids, $ids));
  }
  public function cacheSet($entities) {
    $this->entityCache += $entities;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalDefaultEntityController::$cache protected property Whether this entity type should use the static cache.
DrupalDefaultEntityController::$entityCache protected property Static cache of entities, keyed by entity ID.
DrupalDefaultEntityController::$entityInfo protected property Array of information about the entity.
DrupalDefaultEntityController::$entityType protected property Entity type for this controller instance.
DrupalDefaultEntityController::$hookLoadArguments protected property Additional arguments to pass to hook_TYPE_load().
DrupalDefaultEntityController::$idKey protected property Name of the entity's ID field in the entity database table.
DrupalDefaultEntityController::$revisionKey protected property Name of entity's revision database table field, if it supports revisions.
DrupalDefaultEntityController::$revisionTable protected property The table that stores revisions, if the entity supports revisions.
DrupalDefaultEntityController::attachLoad protected function Attaches data to entities upon loading. 4
DrupalDefaultEntityController::buildQuery protected function Builds the query to load the entity. 4
DrupalDefaultEntityController::cacheGet protected function Gets entities from the static cache. 1
DrupalDefaultEntityController::cleanIds protected function Ensures integer entity IDs are valid.
DrupalDefaultEntityController::filterId protected function Callback for array_filter that removes non-integer IDs.
DrupalDefaultEntityController::resetCache public function Implements DrupalEntityControllerInterface::resetCache(). Overrides DrupalEntityControllerInterface::resetCache
DrupalDefaultEntityController::__construct public function Constructor: sets basic variables.
MultifieldEntityController::cacheSet public function Stores entities in the static entity cache. Overrides DrupalDefaultEntityController::cacheSet
MultifieldEntityController::load public function Implements DrupalEntityControllerInterface::load(). Overrides DrupalDefaultEntityController::load
MultifieldEntityController::queryLoad protected function