ContentEntity.php in Workbench Moderation to Content Moderation 8.2
File
src/Plugin/migrate/source/ContentEntity.php
View source
<?php
namespace Drupal\wbm2cm\Plugin\migrate\source;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContentEntity extends SourcePluginBase implements ContainerFactoryPluginInterface {
protected $storage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityTypeManagerInterface $entity_type_manager) {
$configuration += [
'fields' => [],
'keys' => [
'id',
],
'include_translations' => TRUE,
];
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
$this->storage = $entity_type_manager
->getStorage($this
->getDerivativeId());
}
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'));
}
protected function load() {
foreach ($this->storage
->getQuery()
->execute() as $id) {
(yield $this->storage
->load($id));
}
}
protected function buildRow(ContentEntityInterface $entity) {
$row = [];
foreach ($this
->fields() as $field) {
$items = $entity
->get($field);
$property = $items
->getFieldDefinition()
->getFieldStorageDefinition()
->getMainPropertyName();
$row[$field] = $items->{$property};
}
return $row;
}
protected function initializeIterator() {
$rows = [];
foreach ($this
->load() as $entity) {
array_push($rows, $this
->buildRow($entity));
if ($this->configuration['include_translations']) {
$languages = array_keys($entity
->getTranslationLanguages(FALSE));
foreach ($languages as $language) {
$translation = $entity
->getTranslation($language);
array_push($rows, $this
->buildRow($translation));
}
}
}
return new \ArrayIterator($rows);
}
public function __toString() {
return $this->storage
->getEntityType()
->getPluralLabel();
}
public function fields() {
$fields = [];
$entity_type = $this->storage
->getEntityType();
foreach ($this->configuration['keys'] as $key) {
$key = $entity_type
->getKey($key);
$fields[$key] = $key;
}
return $fields + array_combine($this->configuration['fields'], $this->configuration['fields']);
}
public function getIds() {
$entity_type = $this->storage
->getEntityType();
$ids = [
$entity_type
->getKey('id') => [
'type' => 'integer',
],
];
if ($entity_type
->isRevisionable()) {
$revision = $entity_type
->getKey('revision');
$ids[$revision]['type'] = 'integer';
}
if ($entity_type
->isTranslatable()) {
$langcode = $entity_type
->getKey('langcode');
$ids[$langcode]['type'] = 'string';
}
return $ids;
}
}
Classes
Name |
Description |
ContentEntity |
Loads certain fields from all content entities of a specific type. |