EntityStorageException example

use Drupal\Core\Entity\Sql\SqlContentEntityStorage;

/** * Test storage class used to verify that no load operation is triggered. */
class EntityTestNoLoadStorage extends SqlContentEntityStorage {

  /** * {@inheritdoc} */
  public function load($id) {
    throw new EntityStorageException('No load operation is supposed to happen.');
  }

}
$data = $this->storageComparer->getSourceStorage($collection)->read($name);
      $new_config = new Config($name$this->storageComparer->getTargetStorage($collection)$this->eventDispatcher, $this->typedConfigManager);
      if ($data !== FALSE) {
        $new_config->setData($data);
      }

      $method = 'import' . ucfirst($op);
      $entity_storage = $this->configManager->getEntityTypeManager()->getStorage($entity_type);
      // Call to the configuration entity's storage to handle the configuration       // change.       if (!($entity_storage instanceof ImportableEntityStorageInterface)) {
        throw new EntityStorageException(sprintf('The entity storage "%s" for the "%s" entity type does not support imports', get_class($entity_storage)$entity_type));
      }
      $entity_storage->$method($name$new_config$old_config);
      $this->setProcessedConfiguration($collection$op$name);
      return TRUE;
    }
    return FALSE;
  }

  /** * Imports a configuration entity rename. * * @param string $collection * The configuration collection. * @param string $rename_name * The rename configuration name, as provided by * \Drupal\Core\Config\StorageComparer::createRenameName(). * * @return bool * TRUE if the configuration was imported as a configuration entity. FALSE * otherwise. * * @throws \Drupal\Core\Entity\EntityStorageException * Thrown if the data is owned by an entity type, but the entity storage * does not support imports. * * @see \Drupal\Core\Config\ConfigImporter::createRenameName() */
// Track the original ID.     if ($entity->getOriginalId() !== NULL) {
      $id = $entity->getOriginalId();
    }

    // Track if this entity exists already.     $id_exists = $this->has($id$entity);

    // A new entity should not already exist.     if ($id_exists && $entity->isNew()) {
      throw new EntityStorageException("'{$this->entityTypeId}' entity with ID '$id' already exists.");
    }

    // Load the original entity, if any.     if ($id_exists && !isset($entity->original)) {
      $entity->original = $this->loadUnchanged($id);
    }

    // Allow code to run before saving.     $entity->preSave($this);
    $this->invokeHook('presave', $entity);

    
$this->languageManager = $language_manager;
    $this->contextRepository = $context_repository;
  }

  /** * {@inheritdoc} */
  public function loadEntityByUuid($entity_type_id$uuid) {
    $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);

    if (!$uuid_key = $entity_type->getKey('uuid')) {
      throw new EntityStorageException("Entity type $entity_type_id does not support UUIDs.");
    }

    $entities = $this->entityTypeManager->getStorage($entity_type_id)->loadByProperties([$uuid_key => $uuid]);

    return ($entities) ? reset($entities) : NULL;
  }

  /** * {@inheritdoc} */
  public function loadEntityByConfigTarget($entity_type_id$target) {
    
$container->get('entity.memory_cache'),
      $container->get('entity_type.bundle.info')
    );
  }

  /** * {@inheritdoc} */
  protected function doCreate(array $values) {
    $bundle = $this->getBundleFromValues($values);
    if ($this->bundleKey && !$bundle) {
      throw new EntityStorageException('Missing bundle for entity type ' . $this->entityTypeId);
    }
    $entity_class = $this->getEntityClass($bundle);
    $entity = new $entity_class([]$this->entityTypeId, $bundle);
    $this->initFieldValues($entity$values);
    return $entity;
  }

  /** * {@inheritdoc} */
  public function getBundleFromClass(string $class_name): ?string {
    
// If 'progress' is not set, then this will be the first run of the batch.     if (!isset($sandbox['progress'])) {
      // We cannot support updating the schema of an entity type from       // revisionable to non-revisionable or translatable to non-translatable       // because that can lead to unintended data loss.       // @todo Add support for these conversions in case there is no data loss.       // @see https://www.drupal.org/project/drupal/issues/3024727       $convert_rev_to_non_rev = $original->isRevisionable() && !$entity_type->isRevisionable();
      $convert_mul_to_non_mul = $original->isTranslatable() && !$entity_type->isTranslatable();
      if ($has_data && ($convert_rev_to_non_rev || $convert_mul_to_non_mul)) {
        throw new EntityStorageException('Converting an entity type from revisionable to non-revisionable or from translatable to non-translatable is not supported.');
      }

      // Check that the fields required by a revisionable entity type exist.       if ($entity_type->isRevisionable() && !isset($field_storage_definitions[$entity_type->getKey('revision')])) {
        throw new EntityStorageException('Missing revision field.');
      }
      if ($entity_type->isRevisionable() && !isset($field_storage_definitions[$entity_type->getRevisionMetadataKey('revision_default')])) {
        throw new EntityStorageException('Missing revision_default field.');
      }

      // Check that the fields required by a translatable entity type exist.

  public function setEntityType(EntityTypeInterface $entity_type) {
    if ($this->entityType->id() == $entity_type->id()) {
      $this->entityType = $entity_type;
      $this->initTableLayout();
    }
    else {
      throw new EntityStorageException("Unsupported entity type {$entity_type->id()}");
    }
  }

  /** * Updates the internal list of field storage definitions. * * @param \Drupal\Core\Field\FieldStorageDefinitionInterface[] $field_storage_definitions * An array of field storage definitions. * * @internal Only to be used internally by Entity API. */
  
public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
    $this->checkEntityType($entity_type);
    $this->checkEntityType($original);

    // If no schema changes are needed, we don't need to do anything.     if (!$this->requiresEntityStorageSchemaChanges($entity_type$original)) {
      return;
    }

    // If shared table schema changes are needed, we can't proceed.     if (!class_exists($original->getStorageClass()) || $this->hasSharedTableStructureChange($entity_type$original)) {
      throw new EntityStorageException('It is not possible to change the entity type schema outside of a batch context. Use EntityDefinitionUpdateManagerInterface::updateFieldableEntityType() instead.');
    }

    // Drop original indexes and unique keys.     $this->deleteEntitySchemaIndexes($this->loadEntitySchemaData($entity_type));

    // Create new indexes and unique keys.     $entity_schema = $this->getEntitySchema($entity_type, TRUE);
    $this->createEntitySchemaIndexes($entity_schema);

    // Store the updated entity schema.     $this->saveEntitySchemaData($entity_type$entity_schema);
  }
Home | Imprint | This part of the site doesn't use cookies.