View source
<?php
namespace Drupal\gathercontent\Plugin\migrate\destination;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
class GatherContentEntity extends EntityContentBase {
protected $time;
protected $currentUser;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityFieldManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, TimeInterface $time, AccountProxyInterface $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $entity_manager, $field_type_manager);
$this->time = $time;
$this->currentUser = $current_user;
}
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('datetime.time'), $container
->get('current_user'));
}
protected static function getEntityTypeId($plugin_id) {
return substr($plugin_id, 10);
}
protected function getEntity(Row $row, array $old_destination_id_values) {
$entity = parent::getEntity($row, $old_destination_id_values);
$destination = $row
->getDestination();
if (!empty($destination['gc_import_options']) && $entity
->getEntityType()
->isRevisionable() && !$entity
->isNew() && $destination['gc_import_options']['new_revision']) {
$entity
->setNewRevision(TRUE);
$entity
->setRevisionLogMessage('Created revision for entity ID: ' . $entity
->id());
$entity
->setRevisionCreationTime($this->time
->getRequestTime());
$entity
->setRevisionUserId($this->currentUser
->id());
}
return $entity;
}
public function fields(MigrationInterface $migration = NULL) {
$fields = parent::fields();
$fields += [
'delta' => $this
->t('The delta of this body and version in the source node'),
];
return $fields;
}
}