Newer
Older
Alex Pott
committed
<?php
/**
* @file
* Contains \Drupal\migrate\Plugin\migrate\destination\Entity.
*/
namespace Drupal\migrate\Plugin\migrate\destination;
use Drupal\Component\Plugin\DependentPluginInterface;
use Drupal\Core\Entity\DependencyTrait;
catch
committed
use Drupal\Core\Entity\EntityStorageInterface;
Alex Pott
committed
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
Dries Buytaert
committed
use Drupal\migrate\Entity\MigrationInterface;
Alex Pott
committed
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
Dries Buytaert
committed
* @MigrateDestination(
* id = "entity",
Dries Buytaert
committed
* deriver = "Drupal\migrate\Plugin\Derivative\MigrateEntity"
Dries Buytaert
committed
* )
Alex Pott
committed
*/
abstract class Entity extends DestinationBase implements ContainerFactoryPluginInterface, DependentPluginInterface {
use DependencyTrait;
Alex Pott
committed
/**
catch
committed
* The entity storage.
Alex Pott
committed
*
catch
committed
* @var \Drupal\Core\Entity\EntityStorageInterface
Alex Pott
committed
*/
catch
committed
protected $storage;
Alex Pott
committed
/**
Dries Buytaert
committed
* The list of the bundles of this entity type.
*
* @var array
*/
protected $bundles;
/**
* Construct a new entity.
Alex Pott
committed
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
Alex Pott
committed
* The plugin implementation definition.
Dries Buytaert
committed
* @param MigrationInterface $migration
* The migration.
catch
committed
* @param EntityStorageInterface $storage
* The storage for this entity type.
Dries Buytaert
committed
* @param array $bundles
* The list of bundles this entity type has.
Alex Pott
committed
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles) {
Dries Buytaert
committed
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
catch
committed
$this->storage = $storage;
Dries Buytaert
committed
$this->bundles = $bundles;
Angie Byron
committed
$this->supportsRollback = TRUE;
Alex Pott
committed
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
Dries Buytaert
committed
$entity_type_id = static::getEntityTypeId($plugin_id);
Alex Pott
committed
return new static(
$configuration,
$plugin_id,
$plugin_definition,
Dries Buytaert
committed
$migration,
catch
committed
$container->get('entity.manager')->getStorage($entity_type_id),
Dries Buytaert
committed
array_keys($container->get('entity.manager')->getBundleInfo($entity_type_id))
Alex Pott
committed
);
}
/**
Dries Buytaert
committed
* Finds the entity type from configuration or plugin id.
*
* @param string $plugin_id
* The plugin id.
*
* @return string
* The entity type.
Alex Pott
committed
*/
Dries Buytaert
committed
protected static function getEntityTypeId($plugin_id) {
// Remove "entity:"
return substr($plugin_id, 7);
Alex Pott
committed
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function fields(MigrationInterface $migration = NULL) {
// TODO: Implement fields() method.
Alex Pott
committed
}
/**
Dries Buytaert
committed
* Creates or loads an entity.
*
* @param \Drupal\migrate\Row $row
* The row object.
* @param array $old_destination_id_values
* The old destination ids.
*
* @return \Drupal\Core\Entity\EntityInterface
* The entity we're importing into.
Alex Pott
committed
*/
Dries Buytaert
committed
protected function getEntity(Row $row, array $old_destination_id_values) {
Alex Pott
committed
$entity_id = $old_destination_id_values ? reset($old_destination_id_values) : $this->getEntityId($row);
catch
committed
if (!empty($entity_id) && ($entity = $this->storage->load($entity_id))) {
Dries Buytaert
committed
$this->updateEntity($entity, $row);
}
else {
// Stubs might need some required fields filled in.
Jess
committed
if ($row->isStub()) {
$this->processStubRow($row);
Dries Buytaert
committed
}
$entity = $this->storage->create($row->getDestination());
Dries Buytaert
committed
$entity->enforceIsNew();
}
return $entity;
}
Alex Pott
committed
/**
* Get the entity id of the row.
*
* @param \Drupal\migrate\Row $row
* The row of data.
* @return string
* The entity id for the row we're importing.
*/
protected function getEntityId(Row $row) {
return $row->getDestinationProperty($this->getKey('id'));
}
Dries Buytaert
committed
/**
* Returns a specific entity key.
*
* @param string $key
* The name of the entity key to return.
*
* @return string|bool
* The entity key, or FALSE if it does not exist.
*
* @see \Drupal\Core\Entity\EntityTypeInterface::getKeys()
*/
protected function getKey($key) {
catch
committed
return $this->storage->getEntityType()->getKey($key);
Alex Pott
committed
}
Angie Byron
committed
/**
* {@inheritdoc}
*/
public function rollback(array $destination_identifier) {
// Delete the specified entity from Drupal if it exists.
$entity = $this->storage->load(reset($destination_identifier));
if ($entity) {
$entity->delete();
}
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
$this->addDependency('module', $this->storage->getEntityType()->getProvider());
return $this->dependencies;
}