View source
<?php
namespace Drupal\migrate_drupal\Plugin\migrate\source;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\EntityFieldDefinitionTrait;
use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
use Drupal\migrate\Plugin\MigrateSourceInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContentEntity extends SourcePluginBase implements ContainerFactoryPluginInterface {
use EntityFieldDefinitionTrait;
protected $entityTypeManager;
protected $entityFieldManager;
protected $entityTypeBundleInfo;
protected $entityType;
protected $defaultConfiguration = [
'bundle' => NULL,
'include_translations' => TRUE,
'add_revision_id' => TRUE,
];
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
if (empty($plugin_definition['entity_type'])) {
throw new InvalidPluginDefinitionException($plugin_id, 'Missing required "entity_type" definition.');
}
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->entityType = $this->entityTypeManager
->getDefinition($plugin_definition['entity_type']);
if (!$this->entityType instanceof ContentEntityTypeInterface) {
throw new InvalidPluginDefinitionException($plugin_id, sprintf('The entity type (%s) is not supported. The "content_entity" source plugin only supports content entities.', $plugin_definition['entity_type']));
}
if (!empty($configuration['bundle'])) {
if (!$this->entityType
->hasKey('bundle')) {
throw new \InvalidArgumentException(sprintf('A bundle was provided but the entity type (%s) is not bundleable.', $plugin_definition['entity_type']));
}
$bundle_info = array_keys($this->entityTypeBundleInfo
->getBundleInfo($this->entityType
->id()));
if (!in_array($configuration['bundle'], $bundle_info, TRUE)) {
throw new \InvalidArgumentException(sprintf('The provided bundle (%s) is not valid for the (%s) entity type.', $configuration['bundle'], $plugin_definition['entity_type']));
}
}
parent::__construct($configuration + $this->defaultConfiguration, $plugin_id, $plugin_definition, $migration);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('entity_type.manager'), $container
->get('entity_field.manager'), $container
->get('entity_type.bundle.info'));
}
public function __toString() {
return (string) $this->entityType
->getPluralLabel();
}
protected function initializeIterator() {
$ids = $this
->query()
->execute();
return $this
->yieldEntities($ids);
}
protected function yieldEntities(array $ids) {
$storage = $this->entityTypeManager
->getStorage($this->entityType
->id());
foreach ($ids as $id) {
$entity = $storage
->load($id);
(yield $this
->toArray($entity));
if ($this->configuration['include_translations']) {
foreach ($entity
->getTranslationLanguages(FALSE) as $language) {
(yield $this
->toArray($entity
->getTranslation($language
->getId())));
}
}
}
}
protected function toArray(ContentEntityInterface $entity) {
$return = $entity
->toArray();
foreach (array_keys($this
->getIds()) as $id) {
$value = $entity
->get($id);
$return[$id] = $value
->first()
->getString();
}
return $return;
}
public function query() {
$query = $this->entityTypeManager
->getStorage($this->entityType
->id())
->getQuery()
->accessCheck(FALSE);
if (!empty($this->configuration['bundle'])) {
$query
->condition($this->entityType
->getKey('bundle'), $this->configuration['bundle']);
}
if ($this->entityType
->id() === 'user' && !empty($this->entityType
->getKey('id'))) {
$query
->condition($this->entityType
->getKey('id'), 0, '>');
}
return $query;
}
public function count($refresh = FALSE) {
if (!$this->configuration['include_translations']) {
return parent::count($refresh);
}
return MigrateSourceInterface::NOT_COUNTABLE;
}
protected function doCount() {
return $this
->query()
->count()
->execute();
}
public function fields() {
if (!$this->entityType
->entityClassImplements('Drupal\\Core\\Entity\\FieldableEntityInterface')) {
return [];
}
$field_definitions = $this->entityFieldManager
->getBaseFieldDefinitions($this->entityType
->id());
if (!empty($this->configuration['bundle'])) {
$field_definitions += $this->entityFieldManager
->getFieldDefinitions($this->entityType
->id(), $this->configuration['bundle']);
}
$fields = array_map(function ($definition) {
return (string) $definition
->getLabel();
}, $field_definitions);
return $fields;
}
public function getIds() {
$id_key = $this->entityType
->getKey('id');
$ids[$id_key] = $this
->getDefinitionFromEntity($id_key);
if ($this->configuration['add_revision_id'] && $this->entityType
->isRevisionable()) {
$revision_key = $this->entityType
->getKey('revision');
$ids[$revision_key] = $this
->getDefinitionFromEntity($revision_key);
}
if ($this->entityType
->isTranslatable()) {
$langcode_key = $this->entityType
->getKey('langcode');
$ids[$langcode_key] = $this
->getDefinitionFromEntity($langcode_key);
}
return $ids;
}
}