3 namespace Drupal\migrate\Plugin\migrate\destination;
5 use Drupal\Component\Plugin\DependentPluginInterface;
6 use Drupal\Core\Entity\DependencyTrait;
7 use Drupal\Core\Entity\EntityStorageInterface;
8 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9 use Drupal\migrate\Plugin\MigrationInterface;
10 use Drupal\migrate\Row;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Provides entity destination plugin.
16 * @MigrateDestination(
18 * deriver = "Drupal\migrate\Plugin\Derivative\MigrateEntity"
21 abstract class Entity extends DestinationBase implements ContainerFactoryPluginInterface, DependentPluginInterface {
28 * @var \Drupal\Core\Entity\EntityStorageInterface
33 * The list of the bundles of this entity type.
40 * Construct a new entity.
42 * @param array $configuration
43 * A configuration array containing information about the plugin instance.
44 * @param string $plugin_id
45 * The plugin_id for the plugin instance.
46 * @param mixed $plugin_definition
47 * The plugin implementation definition.
48 * @param \Drupal\migrate\Plugin\MigrationInterface $migration
50 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
51 * The storage for this entity type.
52 * @param array $bundles
53 * The list of bundles this entity type has.
55 public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles) {
56 parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
57 $this->storage = $storage;
58 $this->bundles = $bundles;
59 $this->supportsRollback = TRUE;
65 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
66 $entity_type_id = static::getEntityTypeId($plugin_id);
72 $container->get('entity.manager')->getStorage($entity_type_id),
73 array_keys($container->get('entity.manager')->getBundleInfo($entity_type_id))
78 * Finds the entity type from configuration or plugin ID.
80 * @param string $plugin_id
86 protected static function getEntityTypeId($plugin_id) {
88 return substr($plugin_id, 7);
92 * Gets the bundle for the row taking into account the default.
94 * @param \Drupal\migrate\Row $row
95 * The current row we're importing.
98 * The bundle for this row.
100 public function getBundle(Row $row) {
101 $default_bundle = isset($this->configuration['default_bundle']) ? $this->configuration['default_bundle'] : '';
102 $bundle_key = $this->getKey('bundle');
103 return $row->getDestinationProperty($bundle_key) ?: $default_bundle;
109 public function fields(MigrationInterface $migration = NULL) {
110 // TODO: Implement fields() method.
114 * Creates or loads an entity.
116 * @param \Drupal\migrate\Row $row
118 * @param array $old_destination_id_values
119 * The old destination IDs.
121 * @return \Drupal\Core\Entity\EntityInterface
122 * The entity we are importing into.
124 protected function getEntity(Row $row, array $old_destination_id_values) {
125 $entity_id = reset($old_destination_id_values) ?: $this->getEntityId($row);
126 if (!empty($entity_id) && ($entity = $this->storage->load($entity_id))) {
127 // Allow updateEntity() to change the entity.
128 $entity = $this->updateEntity($entity, $row) ?: $entity;
131 // Attempt to ensure we always have a bundle.
132 if ($bundle = $this->getBundle($row)) {
133 $row->setDestinationProperty($this->getKey('bundle'), $bundle);
136 // Stubs might need some required fields filled in.
137 if ($row->isStub()) {
138 $this->processStubRow($row);
140 $entity = $this->storage->create($row->getDestination());
141 $entity->enforceIsNew();
147 * Gets the entity ID of the row.
149 * @param \Drupal\migrate\Row $row
153 * The entity ID for the row that we are importing.
155 protected function getEntityId(Row $row) {
156 return $row->getDestinationProperty($this->getKey('id'));
160 * Returns a specific entity key.
163 * The name of the entity key to return.
165 * @return string|bool
166 * The entity key, or FALSE if it does not exist.
168 * @see \Drupal\Core\Entity\EntityTypeInterface::getKeys()
170 protected function getKey($key) {
171 return $this->storage->getEntityType()->getKey($key);
177 public function rollback(array $destination_identifier) {
178 // Delete the specified entity from Drupal if it exists.
179 $entity = $this->storage->load(reset($destination_identifier));
188 public function calculateDependencies() {
189 $this->addDependency('module', $this->storage->getEntityType()->getProvider());
190 return $this->dependencies;