backdrop-contrib / entity_plus

This module wraps in a variety of additional entity-related functionality from various sources. Partial port of D7 Entity API.
https://backdropcms.org/project/entity_plus
GNU General Public License v2.0
3 stars 11 forks source link

Loading all entities of a type twice returns no result on second call #168

Open argiepiano opened 11 months ago

argiepiano commented 11 months ago

This is a known issues in the Drupal Entity API module that has not been resolved, presumably because of backward compatibility and custom modules that may have already adapted their own solutions. See these issues 1572466, 1823926 and 1273756

To reproduce

  1. Create a new entity type that uses EntityPlusController, or use the contrib module Basic Entity Plus
  2. Create some entities of that type
  3. In devel PHP window, run this twice:
dpm(entity_load_multiple('MY_TYPE', FALSE));
dpm(entity_load_multiple('MY_TYPE', FALSE));

The first output is correct (it includes all entities). The second output is an empty array.

The patch in 1273756 could be partially adapted.

Otherwise, a good workaround is to override EntityPlusController::cacheGet() as explained in 1572466, as follows:

/**
 * Implements a controller class for MyEntity.
 */
class MyEntityController extends EntityPlusController {

  /**
   * Overrides EntityPlusController::cacheGet()
   */
  protected function cacheGet($ids, $conditions = array()) {
    // Load any available entities from the internal cache.
    if ($ids === FALSE && !$conditions) {
      return $this->entityCache;
    }

    return parent::cacheGet($ids, $conditions);
  }
}