EntityStorageControllerBase.php 7.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
<?php

/**
 * @file
 * Contains \Drupal\Core\Entity\EntityStorageControllerBase.
 */

namespace Drupal\Core\Entity;

/**
 * A base entity storage controller class.
 */
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;
    }
  }

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
  /**
   * {@inheritdoc}
   */
  public function invokeFieldMethod($method, EntityInterface $entity) {
    foreach (array_keys($entity->getTranslationLanguages()) as $langcode) {
      // @todo getTranslation() only works on NG entities. Remove the condition
      // and the second code branch when all core entity types are converted.
      if ($translation = $entity->getTranslation($langcode)) {
        foreach ($translation as $field_name => $field) {
          $field->$method();
        }
      }
      else {
        // For BC entities, iterate through fields and instantiate NG items
        // objects manually.
        $definitions = \Drupal::entityManager()->getFieldDefinitions($entity->entityType(), $entity->bundle());
        foreach ($definitions as $field_name => $definition) {
          if (!empty($definition['configurable'])) {
            // Create the items object.
            $itemsBC = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array();
            // @todo Exception : this calls setValue(), tries to set the
            // 'formatted' property. For now, this is worked around by
            // commenting out the Exception in TextProcessed::setValue().
            $items = \Drupal::typedData()->create($definition, $itemsBC, $field_name, $entity);
            $items->$method();

            // Put back the items values in the entity.
            $itemsBC = $items->getValue(TRUE);
            if ($itemsBC !== array() || isset($entity->{$field_name}[$langcode])) {
              $entity->{$field_name}[$langcode] = $itemsBC;
            }
          }
        }
      }
    }
  }

   /**
   * {@inheritdoc}
   */
  public function invokeFieldItemPrepareCache(EntityInterface $entity) {
    foreach (array_keys($entity->getTranslationLanguages()) as $langcode) {
      // @todo getTranslation() only works on NG entities. Remove the condition
      // and the second code branch when all core entity types are converted.
      if ($translation = $entity->getTranslation($langcode)) {
        foreach ($translation->getPropertyDefinitions() as $property => $definition) {
          $type_definition = \Drupal::typedData()->getDefinition($definition['type']);
          // Only create the item objects if needed.
          if (is_subclass_of($type_definition['class'], '\Drupal\Core\Entity\Field\PrepareCacheInterface')
            // Prevent legacy field types from skewing performance too much by
            // checking the existence of the legacy function directly, instead
            // of making LegacyConfigFieldItem implement PrepareCacheInterface.
            // @todo Remove once all core field types have been converted (see
            // http://drupal.org/node/2014671).
            || (is_subclass_of($type_definition['class'], '\Drupal\field\Plugin\field\field_type\LegacyConfigFieldItem') && function_exists($type_definition['module'] . '_field_load'))) {

            // Call the prepareCache() method directly on each item
            // individually.
            foreach ($translation->get($property) as $item) {
              $item->prepareCache();
            }
          }
        }
      }
      else {
        // For BC entities, iterate through the fields and instantiate NG items
        // objects manually.
        $definitions = \Drupal::entityManager()->getFieldDefinitions($entity->entityType(), $entity->bundle());
        foreach ($definitions as $field_name => $definition) {
          if (!empty($definition['configurable'])) {
            $type_definition = \Drupal::typedData()->getDefinition($definition['type']);
            // Only create the item objects if needed.
            if (is_subclass_of($type_definition['class'], '\Drupal\Core\Entity\Field\PrepareCacheInterface')
              // @todo Remove once all core field types have been converted
              // (see http://drupal.org/node/2014671).
              || (is_subclass_of($type_definition['class'], '\Drupal\field\Plugin\field\field_type\LegacyConfigFieldItem') && function_exists($type_definition['module'] . '_field_load'))) {

              // Create the items object.
              $items = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array();
              $itemsNG = \Drupal::typedData()->create($definition, $items, $field_name, $entity);

              foreach ($itemsNG as $item) {
                $item->prepareCache();
              }

              // Put back the items values in the entity.
              $items = $itemsNG->getValue(TRUE);
              if ($items !== array() || isset($entity->{$field_name}[$langcode])) {
                $entity->{$field_name}[$langcode] = $items;
              }
            }
          }
        }
      }
    }
  }

237
}