FieldableEntityStorageControllerBase.php 11.4 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

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

namespace Drupal\Core\Entity;

10
use Drupal\Core\Field\PrepareCacheInterface;
11 12
use Drupal\field\FieldInterface;
use Drupal\field\FieldInstanceInterface;
13
use Drupal\Core\Field\ConfigFieldItemListInterface;
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
use Symfony\Component\DependencyInjection\Container;

abstract class FieldableEntityStorageControllerBase extends EntityStorageControllerBase implements FieldableEntityStorageControllerInterface {

  /**
   * Loads values of configurable fields for a group of entities.
   *
   * Loads all fields for each entity object in a group of a single entity type.
   * The loaded field values are added directly to the entity objects.
   *
   * This method is a wrapper that handles the field data cache. Subclasses
   * need to implement the doLoadFieldItems() method with the actual storage
   * logic.
   *
   * @param array $entities
   *   An array of entities keyed by entity ID.
   * @param int $age
31 32 33 34
   *   EntityStorageControllerInterface::FIELD_LOAD_CURRENT to load the most
   *   recent revision for all fields, or
   *   EntityStorageControllerInterface::FIELD_LOAD_REVISION to load the version
   *   indicated by each entity.
35 36 37 38 39 40 41 42
   */
  protected function loadFieldItems(array $entities, $age) {
    if (empty($entities)) {
      return;
    }

    // Only the most current revision of non-deleted fields for cacheable entity
    // types can be cached.
43
    $load_current = $age == static::FIELD_LOAD_CURRENT;
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    $info = entity_get_info($this->entityType);
    $use_cache = $load_current && $info['field_cache'];

    // Assume all entities will need to be queried. Entities found in the cache
    // will be removed from the list.
    $queried_entities = $entities;

    // Fetch available entities from cache, if applicable.
    if ($use_cache) {
      // Build the list of cache entries to retrieve.
      $cids = array();
      foreach ($entities as $id => $entity) {
        $cids[] = "field:{$this->entityType}:$id";
      }
      $cache = cache('field')->getMultiple($cids);
      // Put the cached field values back into the entities and remove them from
      // the list of entities to query.
      foreach ($entities as $id => $entity) {
        $cid = "field:{$this->entityType}:$id";
        if (isset($cache[$cid])) {
          unset($queried_entities[$id]);
65 66 67 68 69 70 71
          foreach ($cache[$cid]->data as $langcode => $values) {
            $translation = $entity->getTranslation($langcode);
            // We do not need to worry about field translatability here, the
            // translation object will manage that automatically.
            foreach ($values as $field_name => $items) {
              $translation->$field_name = $items;
            }
72 73 74 75 76 77 78 79 80 81 82
          }
        }
      }
    }

    // Fetch other entities from their storage location.
    if ($queried_entities) {
      // Let the storage controller actually load the values.
      $this->doLoadFieldItems($queried_entities, $age);

      // Build cache data.
83 84
      // @todo: Improve this logic to avoid instantiating field objects once
      // the field logic is improved to not do that anyway.
85 86 87
      if ($use_cache) {
        foreach ($queried_entities as $id => $entity) {
          $data = array();
88 89
          foreach ($entity->getTranslationLanguages() as $langcode => $language) {
            $translation = $entity->getTranslation($langcode);
90 91 92 93 94 95 96 97 98 99 100 101 102 103
            foreach ($translation as $field_name => $items) {
              if ($items instanceof ConfigFieldItemListInterface && !$items->isEmpty()) {
                foreach ($items as $delta => $item) {
                  // If the field item needs to prepare the cache data, call the
                  // corresponding method, otherwise use the values as cache
                  // data.
                  if ($item instanceof PrepareCacheInterface) {
                    $data[$langcode][$field_name][$delta] = $item->getCacheData();
                  }
                  else {
                    $data[$langcode][$field_name][$delta] = $item->getValue();
                  }
                }
              }
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 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
          }
          $cid = "field:{$this->entityType}:$id";
          cache('field')->set($cid, $data);
        }
      }
    }
  }

  /**
   * Saves values of configurable fields for an entity.
   *
   * This method is a wrapper that handles the field data cache. Subclasses
   * need to implement the doSaveFieldItems() method with the actual storage
   * logic.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity.
   * @param bool $update
   *   TRUE if the entity is being updated, FALSE if it is being inserted.
   */
  protected function saveFieldItems(EntityInterface $entity, $update = TRUE) {
    $this->doSaveFieldItems($entity, $update);

    if ($update) {
      $entity_info = $entity->entityInfo();
      if ($entity_info['field_cache']) {
        cache('field')->delete('field:' . $entity->entityType() . ':' . $entity->id());
      }
    }
  }

  /**
   * Deletes values of configurable fields for all revisions of an entity.
   *
   * This method is a wrapper that handles the field data cache. Subclasses
   * need to implement the doDeleteFieldItems() method with the actual storage
   * logic.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity.
   */
  protected function deleteFieldItems(EntityInterface $entity) {
    $this->doDeleteFieldItems($entity);

    $entity_info = $entity->entityInfo();
    if ($entity_info['field_cache']) {
      cache('field')->delete('field:' . $entity->entityType() . ':' . $entity->id());
    }
  }

  /**
   * Deletes values of configurable fields for a single revision of an entity.
   *
   * This method is a wrapper that handles the field data cache. Subclasses
   * need to implement the doDeleteFieldItemsRevision() method with the actual
   * storage logic.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity. It must have a revision ID attribute.
   */
  protected function deleteFieldItemsRevision(EntityInterface $entity) {
166
    $this->doDeleteFieldItemsRevision($entity);
167 168 169 170 171 172 173 174 175 176
  }

  /**
   * Loads values of configurable fields for a group of entities.
   *
   * This is the method that holds the actual storage logic.
   *
   * @param array $entities
   *   An array of entities keyed by entity ID.
   * @param int $age
177 178 179 180
   *   EntityStorageControllerInterface::FIELD_LOAD_CURRENT to load the most
   *   recent revision for all fields, or
   *   EntityStorageControllerInterface::FIELD_LOAD_REVISION to load the version
   *   indicated by each entity.
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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
   */
  abstract protected function doLoadFieldItems($entities, $age);

  /**
   * Saves values of configurable fields for an entity.
   *
   * This is the method that holds the actual storage logic.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity.
   * @param bool $update
   *   TRUE if the entity is being updated, FALSE if it is being inserted.
   */
  abstract protected function doSaveFieldItems(EntityInterface $entity, $update);

  /**
   * Deletes values of configurable fields for all revisions of an entity.
   *
   * This is the method that holds the actual storage logic.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity.
   */
  abstract protected function doDeleteFieldItems(EntityInterface $entity);

  /**
   * Deletes values of configurable fields for a single revision of an entity.
   *
   * This is the method that holds the actual storage logic.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity.
   */
  abstract protected function doDeleteFieldItemsRevision(EntityInterface $entity);

  /**
   * {@inheritdoc}
   */
  public function onFieldCreate(FieldInterface $field) { }

  /**
   * {@inheritdoc}
   */
  public function onFieldUpdate(FieldInterface $field) { }

  /**
   * {@inheritdoc}
   */
  public function onFieldDelete(FieldInterface $field) { }

  /**
   * {@inheritdoc}
   */
  public function onInstanceCreate(FieldInstanceInterface $instance) { }

  /**
   * {@inheritdoc}
   */
  public function onInstanceUpdate(FieldInstanceInterface $instance) { }

  /**
   * {@inheritdoc}
   */
  public function onInstanceDelete(FieldInstanceInterface $instance) { }

  /**
   * {@inheritdoc}
   */
  public function onBundleCreate($bundle) { }

  /**
   * {@inheritdoc}
   */
  public function onBundleRename($bundle, $bundle_new) { }

  /**
   * {@inheritdoc}
   */
  public function onBundleDelete($bundle) { }

  /**
   * {@inheritdoc}
   */
  public function onFieldItemsPurge(EntityInterface $entity, FieldInstanceInterface $instance) {
    if ($values = $this->readFieldItemsToPurge($entity, $instance)) {
      $field = $instance->getField();
      $definition = _field_generate_entity_field_definition($field, $instance);
      $items = \Drupal::typedData()->create($definition, $values, $field->getFieldName(), $entity);
      $items->delete();
    }
    $this->purgeFieldItems($entity, $instance);
  }

  /**
   * Reads values to be purged for a single field of a single entity.
   *
   * This method is called during field data purge, on fields for which
   * onFieldDelete() or onFieldInstanceDelete() has previously run.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity.
   * @param \Drupal\field\FieldInstanceInterface $instance
   *   The field instance.
   *
   * @return array
   *   The field values, in their canonical array format (numerically indexed
   *   array of items, each item being a property/value array).
   */
  abstract protected function readFieldItemsToPurge(EntityInterface $entity, FieldInstanceInterface $instance);

  /**
   * Removes field data from storage during purge.
   *
   * @param EntityInterface $entity
   *   The entity whose values are being purged.
   * @param FieldInstanceInterface $instance
   *   The field whose values are bing purged.
   */
  abstract protected function purgeFieldItems(EntityInterface $entity, FieldInstanceInterface $instance);

  /**
   * {@inheritdoc}
   */
  public function onFieldPurge(FieldInterface $field) { }

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
  /**
   * Checks translation statuses and invoke the related hooks if needed.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity being saved.
   */
  protected function invokeTranslationHooks(ContentEntityInterface $entity) {
    $translations = $entity->getTranslationLanguages(FALSE);
    $original_translations = $entity->original->getTranslationLanguages(FALSE);
    $all_translations = array_keys($translations + $original_translations);

    // Notify modules of translation insertion/deletion.
    foreach ($all_translations as $langcode) {
      if (isset($translations[$langcode]) && !isset($original_translations[$langcode])) {
        $this->invokeHook('translation_insert', $entity->getTranslation($langcode));
      }
      elseif (!isset($translations[$langcode]) && isset($original_translations[$langcode])) {
        $this->invokeHook('translation_delete', $entity->getTranslation($langcode));
      }
    }
  }

  /**
   * Invokes a method on the Field objects within an entity.
   *
   * @param string $method
   *   The method name.
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity object.
   */
  protected function invokeFieldMethod($method, ContentEntityInterface $entity) {
    foreach (array_keys($entity->getTranslationLanguages()) as $langcode) {
      $translation = $entity->getTranslation($langcode);
      foreach ($translation as $field) {
        $field->$method();
      }
    }
  }

345
}