You are here

GatherContentEntity.php in GatherContent 8.5

File

src/Plugin/migrate/destination/GatherContentEntity.php
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;

/**
 * Custom migrate entity.
 *
 * @\Drupal\migrate\Annotation\MigrateDestination(
 *   id = "gc_entity",
 *   deriver = "Drupal\gathercontent\Plugin\Derivative\MigrateEntity"
 * )
 */
class GatherContentEntity 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 "gc_entity:".
    return substr($plugin_id, 10);
  }

  /**
   * {@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['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;
  }

  /**
   * {@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;
  }

}

Classes

Namesort descending Description
GatherContentEntity Custom migrate entity.