Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
* Contains \Drupal\migrate\Plugin\migrate\destination\EntityContentBase.
Dries Buytaert
committed
*/
namespace Drupal\migrate\Plugin\migrate\destination;
use Drupal\Component\Utility\Random;
use Drupal\Component\Utility\Unicode;
Dries Buytaert
committed
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
Alex Pott
committed
use Drupal\Core\Entity\EntityManagerInterface;
catch
committed
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
Dries Buytaert
committed
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\link\LinkItemInterface;
Dries Buytaert
committed
use Drupal\migrate\Entity\MigrationInterface;
use Drupal\migrate\MigrateException;
Angie Byron
committed
use Drupal\migrate\Plugin\MigrateIdMapInterface;
Dries Buytaert
committed
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* The destination class for all content entities lacking a specific class.
*/
class EntityContentBase extends Entity {
/**
Alex Pott
committed
* Entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
Dries Buytaert
committed
*/
Alex Pott
committed
protected $entityManager;
Dries Buytaert
committed
/**
* Field type plugin manager.
*
* @var \Drupal\Core\Field\FieldTypePluginManagerInterface
*/
protected $fieldTypeManager;
Dries Buytaert
committed
/**
* Constructs a content entity.
*
* @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
Dries Buytaert
committed
* The plugin implementation definition.
* @param \Drupal\migrate\Entity\MigrationInterface $migration
* The migration entity.
catch
committed
* @param \Drupal\Core\Entity\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
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service.
* @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
* The field type plugin manager service.
Dries Buytaert
committed
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager) {
catch
committed
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles);
Alex Pott
committed
$this->entityManager = $entity_manager;
$this->fieldTypeManager = $field_type_manager;
Dries Buytaert
committed
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
Dries Buytaert
committed
$entity_type = static::getEntityTypeId($plugin_id);
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$migration,
catch
committed
$container->get('entity.manager')->getStorage($entity_type),
Dries Buytaert
committed
array_keys($container->get('entity.manager')->getBundleInfo($entity_type)),
$container->get('entity.manager'),
$container->get('plugin.manager.field.field_type')
Dries Buytaert
committed
);
}
/**
* {@inheritdoc}
*/
public function import(Row $row, array $old_destination_id_values = array()) {
Angie Byron
committed
$this->rollbackAction = MigrateIdMapInterface::ROLLBACK_DELETE;
Dries Buytaert
committed
$entity = $this->getEntity($row, $old_destination_id_values);
Angie Byron
committed
if (!$entity) {
throw new MigrateException('Unable to get entity');
}
Dries Buytaert
committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
return $this->save($entity, $old_destination_id_values);
}
/**
* Save the entity.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The content entity.
* @param array $old_destination_id_values
* An array of destination id values.
*
* @return array
* An array containing the entity id.
*/
protected function save(ContentEntityInterface $entity, array $old_destination_id_values = array()) {
$entity->save();
return array($entity->id());
}
/**
* {@inheritdoc}
*/
public function getIds() {
$id_key = $this->getKey('id');
$ids[$id_key]['type'] = 'integer';
return $ids;
}
/**
* Update an entity with the new values from row.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to update.
* @param \Drupal\migrate\Row $row
* The row object to update from.
*/
protected function updateEntity(EntityInterface $entity, Row $row) {
// If the migration has specified a list of properties to be overwritten,
// clone the row with an empty set of destination values, and re-add only
// the specified properties.
if (isset($this->configuration['overwrite_properties'])) {
$clone = $row->cloneWithoutDestination();
foreach ($this->configuration['overwrite_properties'] as $property) {
$clone->setDestinationProperty($property, $row->getDestinationProperty($property));
}
$row = $clone;
}
Dries Buytaert
committed
foreach ($row->getDestination() as $field_name => $values) {
$field = $entity->$field_name;
if ($field instanceof TypedDataInterface) {
$field->setValue($values);
}
}
Angie Byron
committed
$this->setRollbackAction($row->getIdMap());
Dries Buytaert
committed
}
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* Do as much population of the stub row as we can.
*
* @param \Drupal\migrate\Row $row
* The row of data.
*/
protected function processStubRow(Row $row) {
$bundle_key = $this->getKey('bundle');
if ($bundle_key && empty($row->getDestinationProperty($bundle_key))) {
$row->setDestinationProperty($bundle_key, reset($this->bundles));
}
// Populate any required fields not already populated.
$fields = $this->entityManager
->getFieldDefinitions($this->storage->getEntityTypeId(), $bundle_key);
foreach ($fields as $field_name => $field_definition) {
if ($field_definition->isRequired() && is_null($row->getDestinationProperty($field_name))) {
// Use the configured default value for this specific field, if any.
if ($default_value = $field_definition->getDefaultValueLiteral()) {
$values[] = $default_value;
}
else {
// Otherwise, ask the field type to generate a sample value.
$field_type = $field_definition->getType();
/** @var \Drupal\Core\Field\FieldItemInterface $field_type_class */
$field_type_class = $this->fieldTypeManager
->getPluginClass($field_definition->getType());
$values = $field_type_class::generateSampleValue($field_definition);
if (is_null($values)) {
// Handle failure to generate a sample value.
throw new MigrateException('Stubbing failed, unable to generate value for field ' . $field_name);
break;
}
}
$row->setDestinationProperty($field_name, $values);
}
}
}