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 a generic destination to import entities.
16 * Available configuration keys:
17 * - translations: (optional) Boolean, if TRUE, the destination will be
18 * associated with the langcode provided by the source plugin. Defaults to
32 * revision_timestamp: timestamp
37 * This will save the processed, migrated row as a node.
48 * revision_timestamp: timestamp
54 * This will save the processed, migrated row as a node with the relevant
55 * langcode because the translations configuration is set to "true".
57 * @MigrateDestination(
59 * deriver = "Drupal\migrate\Plugin\Derivative\MigrateEntity"
62 abstract class Entity extends DestinationBase implements ContainerFactoryPluginInterface, DependentPluginInterface {
69 * @var \Drupal\Core\Entity\EntityStorageInterface
74 * The list of the bundles of this entity type.
81 * Construct a new entity.
83 * @param array $configuration
84 * A configuration array containing information about the plugin instance.
85 * @param string $plugin_id
86 * The plugin_id for the plugin instance.
87 * @param mixed $plugin_definition
88 * The plugin implementation definition.
89 * @param \Drupal\migrate\Plugin\MigrationInterface $migration
91 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
92 * The storage for this entity type.
93 * @param array $bundles
94 * The list of bundles this entity type has.
96 public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles) {
97 $plugin_definition += [
98 'label' => $storage->getEntityType()->getPluralLabel(),
101 parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
102 $this->storage = $storage;
103 $this->bundles = $bundles;
104 $this->supportsRollback = TRUE;
110 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
111 $entity_type_id = static::getEntityTypeId($plugin_id);
117 $container->get('entity.manager')->getStorage($entity_type_id),
118 array_keys($container->get('entity.manager')->getBundleInfo($entity_type_id))
123 * Finds the entity type from configuration or plugin ID.
125 * @param string $plugin_id
131 protected static function getEntityTypeId($plugin_id) {
133 return substr($plugin_id, 7);
137 * Gets the bundle for the row taking into account the default.
139 * @param \Drupal\migrate\Row $row
140 * The current row we're importing.
143 * The bundle for this row.
145 public function getBundle(Row $row) {
146 $default_bundle = isset($this->configuration['default_bundle']) ? $this->configuration['default_bundle'] : '';
147 $bundle_key = $this->getKey('bundle');
148 return $row->getDestinationProperty($bundle_key) ?: $default_bundle;
154 public function fields(MigrationInterface $migration = NULL) {
155 // TODO: Implement fields() method.
159 * Creates or loads an entity.
161 * @param \Drupal\migrate\Row $row
163 * @param array $old_destination_id_values
164 * The old destination IDs.
166 * @return \Drupal\Core\Entity\EntityInterface
167 * The entity we are importing into.
169 protected function getEntity(Row $row, array $old_destination_id_values) {
170 $entity_id = reset($old_destination_id_values) ?: $this->getEntityId($row);
171 if (!empty($entity_id) && ($entity = $this->storage->load($entity_id))) {
172 // Allow updateEntity() to change the entity.
173 $entity = $this->updateEntity($entity, $row) ?: $entity;
176 // Attempt to ensure we always have a bundle.
177 if ($bundle = $this->getBundle($row)) {
178 $row->setDestinationProperty($this->getKey('bundle'), $bundle);
181 // Stubs might need some required fields filled in.
182 if ($row->isStub()) {
183 $this->processStubRow($row);
185 $entity = $this->storage->create($row->getDestination());
186 $entity->enforceIsNew();
192 * Gets the entity ID of the row.
194 * @param \Drupal\migrate\Row $row
198 * The entity ID for the row that we are importing.
200 protected function getEntityId(Row $row) {
201 return $row->getDestinationProperty($this->getKey('id'));
205 * Returns a specific entity key.
208 * The name of the entity key to return.
210 * @return string|bool
211 * The entity key, or FALSE if it does not exist.
213 * @see \Drupal\Core\Entity\EntityTypeInterface::getKeys()
215 protected function getKey($key) {
216 return $this->storage->getEntityType()->getKey($key);
222 public function rollback(array $destination_identifier) {
223 // Delete the specified entity from Drupal if it exists.
224 $entity = $this->storage->load(reset($destination_identifier));
233 public function calculateDependencies() {
234 $this->addDependency('module', $this->storage->getEntityType()->getProvider());
235 return $this->dependencies;