content_workflow_bynder-1.0.0/src/Plugin/migrate/destination/ContentWorkflowBynderEntity.php
src/Plugin/migrate/destination/ContentWorkflowBynderEntity.php
<?php namespace Drupal\content_workflow_bynder\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; /** * Custom migrate entity. * * @\Drupal\migrate\Annotation\MigrateDestination( * id = "cwb_entity", * deriver = "Drupal\content_workflow_bynder\Plugin\Derivative\MigrateEntity" * ) */ class ContentWorkflowBynderEntity extends EntityContentBase { /** * The time service. * * @var \Drupal\Component\Datetime\TimeInterface */ protected $time; /** * The current user. * * @var \Drupal\Core\Session\AccountProxyInterface */ protected $currentUser; /** * {@inheritdoc} */ 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; } /** * {@inheritdoc} */ 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') ); } /** * {@inheritdoc} */ protected static function getEntityTypeId($plugin_id) { // Remove "cwb_entity:". return str_replace("cwb_entity:", "", $plugin_id); } /** * {@inheritdoc} */ protected function getEntity(Row $row, array $old_destination_id_values) { $entity = parent::getEntity($row, $old_destination_id_values); $destination = $row->getDestination(); // Create new revision according to the import options. if ( !empty($destination['cwb_import_options']) && $entity->getEntityType()->isRevisionable() && !$entity->isNew() && $destination['cwb_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; } /** * {@inheritdoc} */ 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; } }