Same filename and directory in other branches
- 8.9.x core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php
- 9 core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php
Namespace
Drupal\migrate\Plugin\migrate\destination
File
core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php
View source
<?php
namespace Drupal\migrate\Plugin\migrate\destination;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\migrate\Audit\HighestIdInterface;
use Drupal\migrate\Exception\EntityValidationException;
use Drupal\migrate\Plugin\MigrateValidatableEntityInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Row;
use Drupal\user\EntityOwnerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityContentBase extends Entity implements HighestIdInterface, MigrateValidatableEntityInterface {
protected $fieldTypeManager;
protected $accountSwitcher;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, AccountSwitcherInterface $account_switcher = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles);
$this->entityFieldManager = $entity_field_manager;
$this->fieldTypeManager = $field_type_manager;
$this->accountSwitcher = $account_switcher;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
$entity_type = static::getEntityTypeId($plugin_id);
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('entity_type.manager')
->getStorage($entity_type), array_keys($container
->get('entity_type.bundle.info')
->getBundleInfo($entity_type)), $container
->get('entity_field.manager'), $container
->get('plugin.manager.field.field_type'), $container
->get('account_switcher'));
}
public function import(Row $row, array $old_destination_id_values = []) {
$this->rollbackAction = MigrateIdMapInterface::ROLLBACK_DELETE;
$entity = $this
->getEntity($row, $old_destination_id_values);
if (!$entity) {
throw new MigrateException('Unable to get entity');
}
assert($entity instanceof ContentEntityInterface);
if ($this
->isEntityValidationRequired($entity)) {
$this
->validateEntity($entity);
}
$ids = $this
->save($entity, $old_destination_id_values);
if ($this
->isTranslationDestination()) {
$ids[] = $entity
->language()
->getId();
}
return $ids;
}
public function isEntityValidationRequired(FieldableEntityInterface $entity) {
return $entity
->isValidationRequired() || !empty($this->configuration['validate']);
}
public function validateEntity(FieldableEntityInterface $entity) {
$account = $entity instanceof EntityOwnerInterface ? $entity
->getOwner() : NULL;
if ($account) {
$this->accountSwitcher
->switchTo($account);
}
try {
$violations = $entity
->validate();
} finally {
if ($account) {
$this->accountSwitcher
->switchBack();
}
}
if (count($violations) > 0) {
throw new EntityValidationException($violations);
}
}
protected function save(ContentEntityInterface $entity, array $old_destination_id_values = []) {
$entity
->setSyncing(TRUE);
$entity
->save();
return [
$entity
->id(),
];
}
public function isTranslationDestination() {
return !empty($this->configuration['translations']);
}
public function getIds() {
$ids = [];
$id_key = $this
->getKey('id');
$ids[$id_key] = $this
->getDefinitionFromEntity($id_key);
if ($this
->isTranslationDestination()) {
$langcode_key = $this
->getKey('langcode');
if (!$langcode_key) {
throw new MigrateException(sprintf('The "%s" entity type does not support translations.', $this->storage
->getEntityTypeId()));
}
$ids[$langcode_key] = $this
->getDefinitionFromEntity($langcode_key);
}
return $ids;
}
protected function updateEntity(EntityInterface $entity, Row $row) {
$empty_destinations = $row
->getEmptyDestinationProperties();
$rollback_action = MigrateIdMapInterface::ROLLBACK_PRESERVE;
if ($this
->isTranslationDestination()) {
$property = $this->storage
->getEntityType()
->getKey('langcode');
if ($row
->hasDestinationProperty($property)) {
$language = $row
->getDestinationProperty($property);
if (!$entity
->hasTranslation($language)) {
$entity
->addTranslation($language);
$rollback_action = MigrateIdMapInterface::ROLLBACK_DELETE;
}
$entity = $entity
->getTranslation($language);
}
}
if (isset($this->configuration['overwrite_properties'])) {
$empty_destinations = array_intersect($empty_destinations, $this->configuration['overwrite_properties']);
$clone = $row
->cloneWithoutDestination();
foreach ($this->configuration['overwrite_properties'] as $property) {
$clone
->setDestinationProperty($property, $row
->getDestinationProperty($property));
}
$row = $clone;
}
foreach ($row
->getDestination() as $field_name => $values) {
$field = $entity->{$field_name};
if ($field instanceof TypedDataInterface) {
$field
->setValue($values);
}
}
foreach ($empty_destinations as $field_name) {
$entity->{$field_name} = NULL;
}
$this
->setRollbackAction($row
->getIdMap(), $rollback_action);
return $entity;
}
protected function processStubRow(Row $row) {
$bundle_key = $this
->getKey('bundle');
if ($bundle_key && empty($row
->getDestinationProperty($bundle_key))) {
if (empty($this->bundles)) {
throw new MigrateException('Stubbing failed, no bundles available for entity type: ' . $this->storage
->getEntityTypeId());
}
$row
->setDestinationProperty($bundle_key, reset($this->bundles));
}
$bundle = $row
->getDestinationProperty($bundle_key) ?? $this->storage
->getEntityTypeId();
$fields = $this->entityFieldManager
->getFieldDefinitions($this->storage
->getEntityTypeId(), $bundle);
foreach ($fields as $field_name => $field_definition) {
if ($field_definition
->isRequired() && is_null($row
->getDestinationProperty($field_name))) {
if ($default_value = $field_definition
->getDefaultValueLiteral()) {
$values = $default_value;
}
else {
$field_type_class = $this->fieldTypeManager
->getPluginClass($field_definition
->getType());
$values = $field_type_class::generateSampleValue($field_definition);
if (is_null($values)) {
throw new MigrateException('Stubbing failed, unable to generate value for field ' . $field_name);
}
}
$row
->setDestinationProperty($field_name, $values);
}
}
}
public function rollback(array $destination_identifier) {
if ($this
->isTranslationDestination()) {
$entity = $this->storage
->load(reset($destination_identifier));
if ($entity && $entity instanceof TranslatableInterface) {
if ($key = $this
->getKey('langcode')) {
if (isset($destination_identifier[$key])) {
$langcode = $destination_identifier[$key];
if ($entity
->hasTranslation($langcode)) {
$translation = $entity
->getTranslation($langcode);
if (!$translation
->isDefaultTranslation()) {
$entity
->removeTranslation($langcode);
$entity
->setSyncing(TRUE);
$entity
->save();
}
}
}
}
}
}
else {
parent::rollback($destination_identifier);
}
}
public function getHighestId() {
$values = $this->storage
->getQuery()
->accessCheck(FALSE)
->sort($this
->getKey('id'), 'DESC')
->range(0, 1)
->execute();
return (int) current($values);
}
}
Classes
Name |
Description |
EntityContentBase |
Provides destination class for all content entities lacking a specific class. |